From 2dcc9db8b1a0016a1236140bc341a411fa042082 Mon Sep 17 00:00:00 2001 From: Aliqyan Tapia Date: Thu, 3 Feb 2022 08:09:11 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20SaveFile=20to=20default=20Extern?= =?UTF-8?q?al=20Storage=20(#1557)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fiber change * reference correct storage in SaveFile * seperate logic to new function * Change storage defination style, fix tests on go1.14 * Add unit test. Co-authored-by: Alex Bakker Co-authored-by: M. Efe Çetin --- app.go | 2 +- ctx.go | 16 ++++++++++++++++ ctx_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/app.go b/app.go index 49d190fae0e..493621fdd12 100644 --- a/app.go +++ b/app.go @@ -359,7 +359,7 @@ type Config struct { trustedProxiesMap map[string]struct{} trustedProxyRanges []*net.IPNet - //If set to true, will print all routes with their method, path and handler. + // If set to true, will print all routes with their method, path and handler. // Default: false EnablePrintRoutes bool `json:"enable_print_routes"` } diff --git a/ctx.go b/ctx.go index af2032ac080..c14b3a2773a 100644 --- a/ctx.go +++ b/ctx.go @@ -11,6 +11,7 @@ import ( "errors" "fmt" "io" + "io/ioutil" "mime/multipart" "net" "net/http" @@ -1128,6 +1129,21 @@ func (c *Ctx) SaveFile(fileheader *multipart.FileHeader, path string) error { return fasthttp.SaveMultipartFile(fileheader, path) } +// SaveFileToStorage saves any multipart file to an external storage system. +func (c *Ctx) SaveFileToStorage(fileheader *multipart.FileHeader, path string, storage Storage) error { + file, err := fileheader.Open() + if err != nil { + return err + } + + content, err := ioutil.ReadAll(file) + if err != nil { + return err + } + + return storage.Set(path, content, 0) +} + // Secure returns a boolean property, that is true, if a TLS connection is established. func (c *Ctx) Secure() bool { return c.fasthttp.IsTLS() diff --git a/ctx_test.go b/ctx_test.go index f5149dbb02d..3a790bf315a 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -27,6 +27,7 @@ import ( "time" "github.com/gofiber/fiber/v2/internal/bytebufferpool" + "github.com/gofiber/fiber/v2/internal/storage/memory" "github.com/gofiber/fiber/v2/utils" "github.com/valyala/fasthttp" ) @@ -1614,6 +1615,48 @@ func Test_Ctx_SaveFile(t *testing.T) { utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code") } +// go test -run Test_Ctx_SaveFileToStorage +func Test_Ctx_SaveFileToStorage(t *testing.T) { + t.Parallel() + app := New() + storage := memory.New() + + app.Post("/test", func(c *Ctx) error { + fh, err := c.FormFile("file") + utils.AssertEqual(t, nil, err) + + err = c.SaveFileToStorage(fh, "test", storage) + utils.AssertEqual(t, nil, err) + + file, err := storage.Get("test") + utils.AssertEqual(t, []byte("hello world"), file) + utils.AssertEqual(t, nil, err) + + err = storage.Delete("test") + utils.AssertEqual(t, nil, err) + + return nil + }) + + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + + ioWriter, err := writer.CreateFormFile("file", "test") + utils.AssertEqual(t, nil, err) + + _, err = ioWriter.Write([]byte("hello world")) + utils.AssertEqual(t, nil, err) + writer.Close() + + req := httptest.NewRequest(MethodPost, "/test", body) + req.Header.Set("Content-Type", writer.FormDataContentType()) + req.Header.Set("Content-Length", strconv.Itoa(len(body.Bytes()))) + + resp, err := app.Test(req) + utils.AssertEqual(t, nil, err, "app.Test(req)") + utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code") +} + // go test -run Test_Ctx_Secure func Test_Ctx_Secure(t *testing.T) { t.Parallel()