From b2c6b868c492c047c4320d37ff59738e304cc26f Mon Sep 17 00:00:00 2001 From: Vincent Bernardoff Date: Wed, 1 Oct 2025 15:00:32 +0200 Subject: [PATCH 1/2] fix deprecated ioutil -> io --- test/zip_streamer_test.go | 6 +++--- zip_streamer/server.go | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/zip_streamer_test.go b/test/zip_streamer_test.go index 870e0f9..c7363ec 100644 --- a/test/zip_streamer_test.go +++ b/test/zip_streamer_test.go @@ -1,7 +1,7 @@ package testing import ( - "io/ioutil" + "io" "os" "testing" @@ -13,7 +13,7 @@ var validFileEntry2, _ = zip_streamer.NewFileEntry("https://gist.githubuserconte var invalidFileEntry, _ = zip_streamer.NewFileEntry("https://gist.githubusercontent.com/scosman/265617b16bb395850d1f0eefd25cc8e3/raw/ade5a9006493a16a57c6d24884b1e1d3978800eksfjmsdklfjsdlkfjsdlkfj/fake.jpg", "invalid.jpg") func TestZipStreamCosntructorEmpty(t *testing.T) { - z, err := zip_streamer.NewZipStream(make([]*zip_streamer.FileEntry, 0), ioutil.Discard) + z, err := zip_streamer.NewZipStream(make([]*zip_streamer.FileEntry, 0), io.Discard) if err == nil || z != nil { t.Fatal("allowed empty streamer") @@ -21,7 +21,7 @@ func TestZipStreamCosntructorEmpty(t *testing.T) { } func TestZipStreamCosntructor(t *testing.T) { - z, err := zip_streamer.NewZipStream([]*zip_streamer.FileEntry{validFileEntry}, ioutil.Discard) + z, err := zip_streamer.NewZipStream([]*zip_streamer.FileEntry{validFileEntry}, io.Discard) if err != nil || z == nil { t.Fatal("constructor failed") diff --git a/zip_streamer/server.go b/zip_streamer/server.go index 14b4afb..38e038b 100644 --- a/zip_streamer/server.go +++ b/zip_streamer/server.go @@ -4,7 +4,7 @@ import ( "archive/zip" "errors" "fmt" - "io/ioutil" + "io" "net/http" "time" @@ -63,7 +63,7 @@ func (s *Server) HandleCreateLink(w http.ResponseWriter, req *http.Request) { } func (s *Server) parseZipRequest(w http.ResponseWriter, req *http.Request) (*ZipDescriptor, error) { - body, err := ioutil.ReadAll(req.Body) + body, err := io.ReadAll(req.Body) if err != nil { w.WriteHeader(http.StatusBadRequest) w.Write([]byte(`{"status":"error","error":"missing body"}`)) @@ -121,7 +121,7 @@ func retrieveZipDescriptorFromUrl(listfileUrl string) (*ZipDescriptor, error) { if listfileResp.StatusCode != http.StatusOK { return nil, errors.New("List File Server Error") } - body, err := ioutil.ReadAll(listfileResp.Body) + body, err := io.ReadAll(listfileResp.Body) if err != nil { return nil, err } From 7aa7653a2763365eb0c09a1f8efcf7e40a2ad239 Mon Sep 17 00:00:00 2001 From: Vincent Bernardoff Date: Wed, 1 Oct 2025 15:18:53 +0200 Subject: [PATCH 2/2] FileEntry: use abstract source --- zip_streamer/file_entry.go | 22 ++++++++++++++++------ zip_streamer/zip_streamer.go | 9 +++------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/zip_streamer/file_entry.go b/zip_streamer/file_entry.go index d835e83..8a78cc7 100644 --- a/zip_streamer/file_entry.go +++ b/zip_streamer/file_entry.go @@ -2,17 +2,31 @@ package zip_streamer import ( "errors" + "io" + "net/http" "net/url" "os" "path" "strings" ) +type ReaderFunc func() (io.ReadCloser, error) + type FileEntry struct { - url *url.URL + reader ReaderFunc zipPath string } +func SimpleGet(url string) ReaderFunc { + return func() (io.ReadCloser, error) { + resp, err := http.Get(url) + if err != nil { + return nil, err + } + return resp.Body, nil + } +} + const UrlPrefixEnvVar = "ZS_URL_PREFIX" func NewFileEntry(urlString string, zipPath string) (*FileEntry, error) { @@ -37,16 +51,12 @@ func NewFileEntry(urlString string, zipPath string) (*FileEntry, error) { } f := FileEntry{ - url: url, + reader: SimpleGet(urlString), zipPath: zipPath, } return &f, nil } -func (f *FileEntry) Url() *url.URL { - return f.url -} - func (f *FileEntry) ZipPath() string { return f.zipPath } diff --git a/zip_streamer/zip_streamer.go b/zip_streamer/zip_streamer.go index 5ce343f..89e097c 100644 --- a/zip_streamer/zip_streamer.go +++ b/zip_streamer/zip_streamer.go @@ -34,14 +34,11 @@ func (z *ZipStream) StreamAllFiles() error { success := 0 for _, entry := range z.entries { - resp, err := http.Get(entry.Url().String()) + r, err := entry.reader() if err != nil { continue } - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - continue - } + defer r.Close() header := &zip.FileHeader{ Name: entry.ZipPath(), @@ -53,7 +50,7 @@ func (z *ZipStream) StreamAllFiles() error { return err } - _, err = io.Copy(entryWriter, resp.Body) + _, err = io.Copy(entryWriter, r) if err != nil { return err }