From 96cf74887448c45c3d5a269182bc41c632e0e8ce Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Thu, 20 Jun 2024 11:03:05 +0100 Subject: [PATCH] moved download func over to client --- internal/client/client.go | 38 ++++++++++++++++++++++++++++++++++++-- internal/executor/show.go | 2 +- internal/utilities/file.go | 33 --------------------------------- 3 files changed, 37 insertions(+), 36 deletions(-) diff --git a/internal/client/client.go b/internal/client/client.go index 71c376d..27ece40 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -11,6 +11,7 @@ import ( "io" "net/http" "net/url" + "os" "time" "codeflow.dananglin.me.uk/apollo/enbas/internal" @@ -60,7 +61,40 @@ func (g *Client) AuthCodeURL() string { ) } -func (g *Client) DownloadFile(url, path string) error { +func (g *Client) DownloadMedia(url, path string) error { + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + + request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + if err != nil { + return fmt.Errorf("unable to create the HTTP request: %w", err) + } + + request.Header.Set("User-Agent", g.UserAgent) + + response, err := g.HTTPClient.Do(request) + if err != nil { + return fmt.Errorf("received an error after attempting the download: %w", err) + } + defer response.Body.Close() + + if response.StatusCode != http.StatusOK { + return fmt.Errorf( + "did not receive an OK response from the GoToSocial server: got %d", + response.StatusCode, + ) + } + + file, err := os.Create(path) + if err != nil { + return fmt.Errorf("unable to create %s: %w", path, err) + } + defer file.Close() + + if _, err = io.Copy(file, response.Body); err != nil { + return fmt.Errorf("unable to save the download to %s: %w", path, err) + } + return nil } @@ -70,7 +104,7 @@ func (g *Client) sendRequest(method string, url string, requestBody io.Reader, o request, err := http.NewRequestWithContext(ctx, method, url, requestBody) if err != nil { - return fmt.Errorf("unable to create the HTTP request, %w", err) + return fmt.Errorf("unable to create the HTTP request: %w", err) } request.Header.Set("Content-Type", "application/json; charset=utf-8") diff --git a/internal/executor/show.go b/internal/executor/show.go index 51ff058..eeb23ce 100644 --- a/internal/executor/show.go +++ b/internal/executor/show.go @@ -499,7 +499,7 @@ func (s *ShowExecutor) showMediaFromStatus(gtsClient *client.Client) error { } if !fileExists { - if err := utilities.DownloadFile(mediaURL, mediaFilePath); err != nil { + if err := gtsClient.DownloadMedia(mediaURL, mediaFilePath); err != nil { return fmt.Errorf("unable to download the media attachment: %w", err) } } diff --git a/internal/utilities/file.go b/internal/utilities/file.go index cdc7aef..decce89 100644 --- a/internal/utilities/file.go +++ b/internal/utilities/file.go @@ -7,10 +7,7 @@ package utilities import ( "errors" "fmt" - "io" - "net/http" "os" - "time" ) func ReadFile(path string) (string, error) { @@ -33,33 +30,3 @@ func FileExists(path string) (bool, error) { return true, nil } - -// TODO: move to client package -func DownloadFile(url, path string) error { - client := http.Client{ - CheckRedirect: func(r *http.Request, _ []*http.Request) error { - r.URL.Opaque = r.URL.Path - - return nil - }, - Timeout: time.Second * 30, - } - - resp, err := client.Get(url) - if err != nil { - return fmt.Errorf("unable to download the file: %w", err) - } - defer resp.Body.Close() - - file, err := os.Create(path) - if err != nil { - return fmt.Errorf("unable to create %s: %w", path, err) - } - defer file.Close() - - if _, err = io.Copy(file, resp.Body); err != nil { - return fmt.Errorf("unable to save the download to %s: %w", path, err) - } - - return nil -}