From 22c23cc2eef6eb092989c4c59ce97efd8c12fae2 Mon Sep 17 00:00:00 2001 From: minoplhy Date: Sun, 14 Apr 2024 02:59:55 +0700 Subject: [PATCH] enhance: memory handling --- main.go | 2 -- src/handler/chibisafe.go | 19 ++++++++++++------- src/handler/httpdo.go | 2 +- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 0bc9550..9ad60e7 100644 --- a/main.go +++ b/main.go @@ -77,7 +77,6 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) { tempfilepath := handler.GetTempFilename(fileHeader.Filename) handler.InfoLogBuilder([]string{r.RemoteAddr, tempfilepath}, "Successfully obtained temporary Filename") handler.SaveFile(tempfilepath, file) - handler.DiscardFile(file) PostData := handler.UploadPostMeta{ ContentType: fileHeader.Header.Get("Content-Type"), @@ -152,7 +151,6 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) { if err != nil { http.Error(w, handler.ErrorResponseBuild(http.StatusInternalServerError, "Something went wrong!"), http.StatusInternalServerError) handler.ErrorLogBuilder([]string{}, err.Error()) - return } handler.InfoLogBuilder([]string{r.RemoteAddr, PostProcessResponse.Name, tempfilepath}, fmt.Sprintf("Successfully Processed Response with UUID: %s", PostProcessResponse.UUID)) diff --git a/src/handler/chibisafe.go b/src/handler/chibisafe.go index c5c20b9..b1d1cab 100644 --- a/src/handler/chibisafe.go +++ b/src/handler/chibisafe.go @@ -53,11 +53,13 @@ func UploadPost(BasePath string, headers map[string]string, PostData UploadPostM defer resp.Body.Close() if resp.StatusCode == http.StatusOK { - BodyRead, err := io.ReadAll(resp.Body) + buffer := bytes.NewBuffer(nil) + _, err := io.Copy(buffer, resp.Body) + defer resp.Body.Close() if err != nil { return nil, err } - return BodyRead, nil + return buffer.Bytes(), nil } else { return nil, err } @@ -96,14 +98,15 @@ func NetworkStoragePut(URL string, ContentType string, filepath string) ([]byte, if err != nil { return nil, err } - defer resp.Body.Close() if resp.StatusCode == http.StatusOK { - BodyRead, err := io.ReadAll(resp.Body) + buffer := bytes.NewBuffer(nil) + _, err := io.Copy(buffer, resp.Body) + defer resp.Body.Close() if err != nil { return nil, err } - return BodyRead, nil + return buffer.Bytes(), nil } else { return nil, err } @@ -129,11 +132,13 @@ func UploadProcessPost(BasePath string, headers map[string]string, PostData Uplo } if resp.StatusCode == http.StatusOK { - BodyRead, err := io.ReadAll(resp.Body) + buffer := bytes.NewBuffer(nil) + _, err := io.Copy(buffer, resp.Body) + defer resp.Body.Close() if err != nil { return nil, err } - return BodyRead, nil + return buffer.Bytes(), nil } else { return nil, err } diff --git a/src/handler/httpdo.go b/src/handler/httpdo.go index 656ec76..9e81634 100644 --- a/src/handler/httpdo.go +++ b/src/handler/httpdo.go @@ -75,5 +75,5 @@ func HTTPClientDo(Request *http.Request) (*http.Response, error) { if err != nil { return nil, err } - return response, err + return response, nil }