From 76f0f3ff3d7f8e8423c77f86a3ace73532537ba2 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Mon, 29 Jan 2024 23:37:59 +0800 Subject: [PATCH] refactor(backend): move from io/ioutil to io and os packages (#1784) The io/ioutil package has been deprecated as of Go 1.16 [1]. This commit replaces the existing io/ioutil functions with their new definitions in io and os packages. [1]: https://golang.org/doc/go1.16#ioutil Signed-off-by: Eng Zer Jun --- backend/internal/assets/cacher/cacher.go | 6 +++--- backend/internal/http/router/handlers-mobile.go | 4 ++-- backend/internal/http/router/handlers-web.go | 3 +-- backend/internal/http/router/handlers.go | 3 +-- backend/internal/videostorage/service.go | 13 +++++++------ backend/pkg/integrations/clients/bugsnag.go | 3 +-- backend/pkg/integrations/clients/datadog.go | 3 +-- backend/pkg/integrations/clients/newrelic.go | 3 +-- backend/pkg/integrations/clients/rollbar.go | 3 +-- backend/pkg/integrations/clients/sentry.go | 3 +-- backend/pkg/integrations/clients/sumologic.go | 5 ++--- 11 files changed, 21 insertions(+), 28 deletions(-) diff --git a/backend/internal/assets/cacher/cacher.go b/backend/internal/assets/cacher/cacher.go index c6daecd77..795ebad78 100644 --- a/backend/internal/assets/cacher/cacher.go +++ b/backend/internal/assets/cacher/cacher.go @@ -5,10 +5,10 @@ import ( "crypto/x509" "fmt" "io" - "io/ioutil" "log" "mime" "net/http" + "os" "path/filepath" "strings" "time" @@ -62,7 +62,7 @@ func NewCacher(cfg *config.Config, store objectstorage.ObjectStorage) (*cacher, log.Fatalf("Error creating x509 keypair from the client cert file %s and client key file %s , Error: %s", err, cfg.ClientCertFilePath, cfg.ClientKeyFilePath) } - caCert, err := ioutil.ReadFile(cfg.CaCertFilePath) + caCert, err := os.ReadFile(cfg.CaCertFilePath) if err != nil { log.Fatalf("Error opening cert file %s, Error: %s", cfg.CaCertFilePath, err) } @@ -128,7 +128,7 @@ func (c *cacher) cacheURL(t *Task) { } return } - data, err := ioutil.ReadAll(io.LimitReader(res.Body, int64(c.sizeLimit+1))) + data, err := io.ReadAll(io.LimitReader(res.Body, int64(c.sizeLimit+1))) if err != nil { c.Errors <- errors.Wrap(err, t.urlContext) return diff --git a/backend/internal/http/router/handlers-mobile.go b/backend/internal/http/router/handlers-mobile.go index 123ecf899..64c024625 100644 --- a/backend/internal/http/router/handlers-mobile.go +++ b/backend/internal/http/router/handlers-mobile.go @@ -3,7 +3,7 @@ package router import ( "encoding/json" "errors" - "io/ioutil" + "io" "log" "math/rand" "net/http" @@ -200,7 +200,7 @@ func (e *Router) imagesUploadHandlerIOS(w http.ResponseWriter, r *http.Request) continue } - data, err := ioutil.ReadAll(file) + data, err := io.ReadAll(file) if err != nil { log.Fatalf("failed reading data: %s", err) } diff --git a/backend/internal/http/router/handlers-web.go b/backend/internal/http/router/handlers-web.go index 21f0f3b3c..c34b62153 100644 --- a/backend/internal/http/router/handlers-web.go +++ b/backend/internal/http/router/handlers-web.go @@ -6,7 +6,6 @@ import ( "fmt" "github.com/gorilla/mux" "io" - "io/ioutil" "log" "math/rand" "net/http" @@ -586,7 +585,7 @@ func (e *Router) imagesUploaderHandlerWeb(w http.ResponseWriter, r *http.Request } // Read the file content - fileBytes, err := ioutil.ReadAll(file) + fileBytes, err := io.ReadAll(file) if err != nil { file.Close() http.Error(w, err.Error(), http.StatusInternalServerError) diff --git a/backend/internal/http/router/handlers.go b/backend/internal/http/router/handlers.go index 6250b378d..432ad054e 100644 --- a/backend/internal/http/router/handlers.go +++ b/backend/internal/http/router/handlers.go @@ -3,7 +3,6 @@ package router import ( gzip "github.com/klauspost/pgzip" "io" - "io/ioutil" "log" "net/http" "time" @@ -32,7 +31,7 @@ func (e *Router) pushMessages(w http.ResponseWriter, r *http.Request, sessionID reader = body } log.Println("Reader after switch:", reader) - buf, err := ioutil.ReadAll(reader) + buf, err := io.ReadAll(reader) if err != nil { ResponseWithError(w, http.StatusInternalServerError, err, start, r.URL.Path, 0) return diff --git a/backend/internal/videostorage/service.go b/backend/internal/videostorage/service.go index e5e6ddd11..8f5d4df14 100644 --- a/backend/internal/videostorage/service.go +++ b/backend/internal/videostorage/service.go @@ -3,14 +3,15 @@ package videostorage import ( "bytes" "fmt" - "io/ioutil" "log" - config "openreplay/backend/internal/config/videostorage" - "openreplay/backend/pkg/objectstorage" + "os" "os/exec" "strconv" "strings" "time" + + config "openreplay/backend/internal/config/videostorage" + "openreplay/backend/pkg/objectstorage" ) type Task struct { @@ -53,7 +54,7 @@ func New(cfg *config.Config, objStorage objectstorage.ObjectStorage) (*VideoStor } func (v *VideoStorage) makeVideo(sessID uint64, filesPath string) error { - files, err := ioutil.ReadDir(filesPath) + files, err := os.ReadDir(filesPath) if err != nil || len(files) == 0 { return err // nil error is there is no screenshots } @@ -85,7 +86,7 @@ func (v *VideoStorage) makeCanvasVideo(sessID uint64, filesPath, canvasMix strin name := strings.TrimSuffix(canvasMix, "-list") mixList := fmt.Sprintf("%s%s", filesPath, canvasMix) // check that mixList exists - if _, err := ioutil.ReadFile(mixList); err != nil { + if _, err := os.ReadFile(mixList); err != nil { return err } videoPath := fmt.Sprintf("%s%s.mp4", filesPath, name) @@ -114,7 +115,7 @@ func (v *VideoStorage) makeCanvasVideo(sessID uint64, filesPath, canvasMix strin func (v *VideoStorage) sendToS3(task *Task) { start := time.Now() // Read video file from disk - video, err := ioutil.ReadFile(task.path) + video, err := os.ReadFile(task.path) if err != nil { log.Fatalf("Failed to read video file: %v", err) } diff --git a/backend/pkg/integrations/clients/bugsnag.go b/backend/pkg/integrations/clients/bugsnag.go index eb636eb10..57b6d8008 100644 --- a/backend/pkg/integrations/clients/bugsnag.go +++ b/backend/pkg/integrations/clients/bugsnag.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "net/url" "time" @@ -66,7 +65,7 @@ func (b *bugsnag) Request(c *client) error { // Status code // 401 (unauthorised) if resp.StatusCode >= 400 { - io.Copy(ioutil.Discard, resp.Body) // Read the body to free socket + io.Copy(io.Discard, resp.Body) // Read the body to free socket return fmt.Errorf("Bugsnag: server respond with the code %v | data: %v ", resp.StatusCode, *b) } diff --git a/backend/pkg/integrations/clients/datadog.go b/backend/pkg/integrations/clients/datadog.go index ca0ef166c..868fdfde8 100644 --- a/backend/pkg/integrations/clients/datadog.go +++ b/backend/pkg/integrations/clients/datadog.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "time" @@ -85,7 +84,7 @@ func (d *datadog) Request(c *client) error { } defer resp.Body.Close() if resp.StatusCode >= 400 { - io.Copy(ioutil.Discard, resp.Body) // Read the body to free socket + io.Copy(io.Discard, resp.Body) // Read the body to free socket return fmt.Errorf("Datadog: server respond with the code %v", resp.StatusCode) } var ddResp datadogResponce diff --git a/backend/pkg/integrations/clients/newrelic.go b/backend/pkg/integrations/clients/newrelic.go index bc0a3e5ad..ff8501fc7 100644 --- a/backend/pkg/integrations/clients/newrelic.go +++ b/backend/pkg/integrations/clients/newrelic.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/http" "time" @@ -64,7 +63,7 @@ func (nr *newrelic) Request(c *client) error { // 401 (unauthorised) if wrong XQueryKey/deploymentServer is wrong or 403 (Forbidden) if ApplicationId is wrong // 400 if Query has problems if resp.StatusCode >= 400 { - io.Copy(ioutil.Discard, resp.Body) // Read the body to free socket + io.Copy(io.Discard, resp.Body) // Read the body to free socket return fmt.Errorf("Newrelic: server respond with the code %v| Request: ", resp.StatusCode, *req) } // Pagination depending on returning metadata ? diff --git a/backend/pkg/integrations/clients/rollbar.go b/backend/pkg/integrations/clients/rollbar.go index c7acaabe4..481ac425b 100644 --- a/backend/pkg/integrations/clients/rollbar.go +++ b/backend/pkg/integrations/clients/rollbar.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/http" "strconv" "strings" @@ -87,7 +86,7 @@ func (rb *rollbar) Request(c *client) error { // status != 200 || 201 // status can be 403 then should report about wrong token if resp.StatusCode >= 400 { - io.Copy(ioutil.Discard, resp.Body) // Read the body to free socket + io.Copy(io.Discard, resp.Body) // Read the body to free socket return fmt.Errorf("Rollbar: server respond with the code %v", resp.StatusCode) } diff --git a/backend/pkg/integrations/clients/sentry.go b/backend/pkg/integrations/clients/sentry.go index bf62aa88b..6c97318e1 100644 --- a/backend/pkg/integrations/clients/sentry.go +++ b/backend/pkg/integrations/clients/sentry.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "net/url" "strconv" @@ -58,7 +57,7 @@ PageLoop: defer resp.Body.Close() if resp.StatusCode >= 400 { - io.Copy(ioutil.Discard, resp.Body) // Read the body to free socket + io.Copy(io.Discard, resp.Body) // Read the body to free socket return fmt.Errorf("Sentry: server respond with the code %v", resp.StatusCode) } diff --git a/backend/pkg/integrations/clients/sumologic.go b/backend/pkg/integrations/clients/sumologic.go index d2fa6eb12..d189da8bc 100644 --- a/backend/pkg/integrations/clients/sumologic.go +++ b/backend/pkg/integrations/clients/sumologic.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "strings" "time" @@ -62,7 +61,7 @@ func (sl *sumologic) deleteJob(jobId string, errChan chan<- error) { errChan <- fmt.Errorf("Error on DELETE request: %v", err) return } - io.Copy(ioutil.Discard, resp.Body) + io.Copy(io.Discard, resp.Body) resp.Body.Close() } @@ -104,7 +103,7 @@ func (sl *sumologic) Request(c *client) error { // https://help.sumologic.com/APIs/Search-Job-API/About-the-Search-Job-API#status-codes // responce body is NOT the same as in docs (look at the sumologic_job_start.json) if resp.StatusCode >= 400 { - io.Copy(ioutil.Discard, resp.Body) // Read the body to free socket + io.Copy(io.Discard, resp.Body) // Read the body to free socket return fmt.Errorf("Sumologic: server respond with the code %v | req %v |Resp: %v", resp.StatusCode, *req, *resp) } sl.cookies = resp.Cookies()