mirror of https://github.com/gofiber/fiber.git
🚀 SaveFile to default External Storage (#1557)
* 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>pull/1748/head
parent
6b29698d91
commit
39a503cc1d
2
app.go
2
app.go
|
@ -359,7 +359,7 @@ type Config struct {
|
||||||
trustedProxiesMap map[string]struct{}
|
trustedProxiesMap map[string]struct{}
|
||||||
trustedProxyRanges []*net.IPNet
|
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
|
// Default: false
|
||||||
EnablePrintRoutes bool `json:"enable_print_routes"`
|
EnablePrintRoutes bool `json:"enable_print_routes"`
|
||||||
}
|
}
|
||||||
|
|
16
ctx.go
16
ctx.go
|
@ -11,6 +11,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -1128,6 +1129,21 @@ func (c *Ctx) SaveFile(fileheader *multipart.FileHeader, path string) error {
|
||||||
return fasthttp.SaveMultipartFile(fileheader, path)
|
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.
|
// Secure returns a boolean property, that is true, if a TLS connection is established.
|
||||||
func (c *Ctx) Secure() bool {
|
func (c *Ctx) Secure() bool {
|
||||||
return c.fasthttp.IsTLS()
|
return c.fasthttp.IsTLS()
|
||||||
|
|
43
ctx_test.go
43
ctx_test.go
|
@ -27,6 +27,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2/internal/bytebufferpool"
|
"github.com/gofiber/fiber/v2/internal/bytebufferpool"
|
||||||
|
"github.com/gofiber/fiber/v2/internal/storage/memory"
|
||||||
"github.com/gofiber/fiber/v2/utils"
|
"github.com/gofiber/fiber/v2/utils"
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
@ -1614,6 +1615,48 @@ func Test_Ctx_SaveFile(t *testing.T) {
|
||||||
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")
|
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
|
// go test -run Test_Ctx_Secure
|
||||||
func Test_Ctx_Secure(t *testing.T) {
|
func Test_Ctx_Secure(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
Loading…
Reference in New Issue