Move DownloadFile into utils package
This commit is contained in:
parent
a4027c4271
commit
d3f19c58d7
18
cmd/run.go
18
cmd/run.go
|
@ -1,10 +1,7 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
|
||||||
|
|
||||||
mastodonapi "github.com/cking/go-mastodon"
|
mastodonapi "github.com/cking/go-mastodon"
|
||||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
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)
|
url, _ := bot.GetFileDirectURL(biggest_photo.FileID)
|
||||||
log.Printf("Downloading: %s\n", url)
|
log.Printf("Downloading: %s\n", url)
|
||||||
file, err := downloadFile(url)
|
file, err := utils.DownloadFile(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Could not download file: %v", err)
|
log.Printf("Could not download file: %v", err)
|
||||||
continue
|
continue
|
||||||
|
@ -105,16 +102,3 @@ the specified Mastodon account.`,
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(runCmd)
|
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue