1
0
Fork 0

Move DownloadFile into utils package

This commit is contained in:
Daniele Tricoli 2022-03-13 03:13:42 +01:00
parent a4027c4271
commit d3f19c58d7
2 changed files with 21 additions and 17 deletions

View File

@ -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
}

20
utils/http.go Normal file
View File

@ -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
}