Skip to content

Commit

Permalink
refactor(context): add an optional permission parameter to the SaveUp…
Browse files Browse the repository at this point in the history
…loadedFile method (#4068) (#4088)

Co-authored-by: hso <[email protected]>
  • Loading branch information
haesuo566 and hso authored Nov 15, 2024
1 parent e8d34d0 commit e46bd52
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
13 changes: 11 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package gin
import (
"errors"
"io"
"io/fs"
"log"
"math"
"mime/multipart"
Expand Down Expand Up @@ -676,14 +677,22 @@ func (c *Context) MultipartForm() (*multipart.Form, error) {
}

// SaveUploadedFile uploads the form file to specific dst.
func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dst string) error {
func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dst string, perm ...fs.FileMode) error {
src, err := file.Open()
if err != nil {
return err
}
defer src.Close()

if err = os.MkdirAll(filepath.Dir(dst), 0o750); err != nil {
if len(perm) <= 0 {
perm = append(perm, 0o750)
}

if err = os.MkdirAll(filepath.Dir(dst), perm[0]); err != nil {
return err
}

if err = os.Chmod(filepath.Dir(dst), perm[0]); err != nil {
return err
}

Expand Down
41 changes: 41 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import (
"fmt"
"html/template"
"io"
"io/fs"
"mime/multipart"
"net"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -155,6 +157,45 @@ func TestSaveUploadedCreateFailed(t *testing.T) {
require.Error(t, c.SaveUploadedFile(f, "/"))
}

func TestSaveUploadedFileWithPermission(t *testing.T) {
buf := new(bytes.Buffer)
mw := multipart.NewWriter(buf)
w, err := mw.CreateFormFile("file", "permission_test")
require.NoError(t, err)
_, err = w.Write([]byte("permission_test"))
require.NoError(t, err)
mw.Close()
c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("POST", "/", buf)

Check failure on line 169 in context_test.go

View workflow job for this annotation

GitHub Actions / lint

"POST" can be replaced by http.MethodPost (usestdlibvars)
c.Request.Header.Set("Content-Type", mw.FormDataContentType())
f, err := c.FormFile("file")
require.NoError(t, err)
assert.Equal(t, "permission_test", f.Filename)
var mode fs.FileMode = 0o755
require.NoError(t, c.SaveUploadedFile(f, "permission_test", mode))
info, err := os.Stat(filepath.Dir("permission_test"))
require.NoError(t, err)
assert.Equal(t, info.Mode().Perm(), mode)
}

func TestSaveUploadedFileWithPermissionFailed(t *testing.T) {
buf := new(bytes.Buffer)
mw := multipart.NewWriter(buf)
w, err := mw.CreateFormFile("file", "permission_test")
require.NoError(t, err)
_, err = w.Write([]byte("permission_test"))
require.NoError(t, err)
mw.Close()
c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("POST", "/", buf)

Check failure on line 190 in context_test.go

View workflow job for this annotation

GitHub Actions / lint

"POST" can be replaced by http.MethodPost (usestdlibvars)
c.Request.Header.Set("Content-Type", mw.FormDataContentType())
f, err := c.FormFile("file")
require.NoError(t, err)
assert.Equal(t, "permission_test", f.Filename)
var mode fs.FileMode = 0o644
require.Error(t, c.SaveUploadedFile(f, "test/permission_test", mode))
}

func TestContextReset(t *testing.T) {
router := New()
c := router.allocateContext(0)
Expand Down

0 comments on commit e46bd52

Please sign in to comment.