Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃殌 SaveFile to default External Storage #1557

Merged
merged 6 commits into from Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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