remove 5 second wait before uploading

This commit is contained in:
lyx0 2023-10-05 11:58:19 +02:00
parent 80d9ed9fdc
commit 500e498524

View file

@ -64,9 +64,9 @@ func (dl *downloader) dlxd(target, link string) {
downloadResult.Close() downloadResult.Close()
f.Close() f.Close()
duration := 5 * time.Second // duration := 5 * time.Second
dl.twitchClient.Say(target, "ResidentSleeper ..") // dl.twitchClient.Say(target, "ResidentSleeper ..")
time.Sleep(duration) // time.Sleep(duration)
dl.upload(target, fmt.Sprintf("%s.%s", fn, rExt)) dl.upload(target, fmt.Sprintf("%s.%s", fn, rExt))
@ -83,24 +83,28 @@ func (dl *downloader) upload(target, path string) {
err := form.WriteField("name", "xd") err := form.WriteField("name", "xd")
if err != nil { if err != nil {
dl.twitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) dl.twitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path)
return return
} }
file, err := os.Open(path) // path to image file file, err := os.Open(path) // path to image file
if err != nil { if err != nil {
dl.twitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) dl.twitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path)
return return
} }
w, err := form.CreateFormFile("file", path) w, err := form.CreateFormFile("file", path)
if err != nil { if err != nil {
dl.twitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) dl.twitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path)
return return
} }
_, err = io.Copy(w, file) _, err = io.Copy(w, file)
if err != nil { if err != nil {
dl.twitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) dl.twitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path)
return return
} }
@ -110,14 +114,18 @@ func (dl *downloader) upload(target, path string) {
req, err := http.NewRequest(http.MethodPost, dl.URL, pr) req, err := http.NewRequest(http.MethodPost, dl.URL, pr)
if err != nil { if err != nil {
dl.twitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) dl.twitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path)
return return
} }
req.Header.Set("Content-Type", form.FormDataContentType()) req.Header.Set("Content-Type", form.FormDataContentType())
httpClient := http.DefaultClient httpClient := http.Client{
Timeout: 300 * time.Second,
}
resp, err := httpClient.Do(req) resp, err := httpClient.Do(req)
if err != nil { if err != nil {
dl.twitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) dl.twitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path)
dl.Log.Errorln("Error while sending HTTP request:", err) dl.Log.Errorln("Error while sending HTTP request:", err)
return return
@ -128,6 +136,7 @@ func (dl *downloader) upload(target, path string) {
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
dl.twitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) dl.twitchClient.Say(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err))
os.Remove(path)
dl.Log.Errorln("Error while reading response:", err) dl.Log.Errorln("Error while reading response:", err)
return return
} }