Skip to content

Commit

Permalink
🚀 SaveFile to default External Storage (#1557)
Browse files Browse the repository at this point in the history
* 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 <abakks@hotmail.com>
Co-authored-by: M. Efe Çetin <efectn@protonmail.com>
  • Loading branch information
3 people committed Feb 3, 2022
1 parent 6b29698 commit 39a503c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app.go
Expand Up @@ -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"`
}
Expand Down
16 changes: 16 additions & 0 deletions ctx.go
Expand Up @@ -11,6 +11,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net"
"net/http"
Expand Down Expand Up @@ -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()
Expand Down
43 changes: 43 additions & 0 deletions ctx_test.go
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 39a503c

Please sign in to comment.