diff --git a/cmd/run.go b/cmd/run.go index 557b13f..0bf6764 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -1,10 +1,7 @@ package cmd import ( - "fmt" - "io" "log" - "net/http" mastodonapi "github.com/cking/go-mastodon" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" @@ -83,7 +80,7 @@ the specified Mastodon account.`, } url, _ := bot.GetFileDirectURL(biggest_photo.FileID) log.Printf("Downloading: %s\n", url) - file, err := downloadFile(url) + file, err := utils.DownloadFile(url) if err != nil { log.Printf("Could not download file: %v", err) continue @@ -105,16 +102,3 @@ the specified Mastodon account.`, func init() { rootCmd.AddCommand(runCmd) } - -func downloadFile(url string) (io.ReadCloser, error) { - response, err := http.Get(url) - if err != nil { - return nil, err - } - - if response.StatusCode != 200 { - return nil, fmt.Errorf("Not able to download %s", url) - } - - return response.Body, nil -} diff --git a/utils/http.go b/utils/http.go new file mode 100644 index 0000000..364a6dc --- /dev/null +++ b/utils/http.go @@ -0,0 +1,20 @@ +package utils + +import ( + "fmt" + "io" + "net/http" +) + +func DownloadFile(url string) (io.ReadCloser, error) { + response, err := http.Get(url) + if err != nil { + return nil, err + } + + if response.StatusCode != 200 { + return nil, fmt.Errorf("Not able to download %s", url) + } + + return response.Body, nil +}