mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
improve uploading file feature for ghttp.Server
This commit is contained in:
@ -26,34 +26,38 @@ type UploadFile struct {
|
||||
// UploadFiles is array type for *UploadFile.
|
||||
type UploadFiles []*UploadFile
|
||||
|
||||
// Save saves the single uploading file to specified path and returns the saved file name.
|
||||
// The parameter path can be either a directory or a file path. If <path> is a directory,
|
||||
// it saves the uploading file to the directory using its original name. If <path> is a
|
||||
// file path, it saves the uploading file to the file path.
|
||||
// Save saves the single uploading file to directory path and returns the saved file name.
|
||||
//
|
||||
// The parameter <dirPath> should be a directory path or it returns error.
|
||||
//
|
||||
// The parameter <randomlyRename> specifies whether randomly renames the file name, which
|
||||
// make sense if the <path> is a directory.
|
||||
//
|
||||
// Note that it will overwrite the target file if there's already a same name file exist.
|
||||
func (f *UploadFile) Save(path string, randomlyRename ...bool) (filename string, err error) {
|
||||
func (f *UploadFile) Save(dirPath string, randomlyRename ...bool) (filename string, err error) {
|
||||
if f == nil {
|
||||
return
|
||||
}
|
||||
if !gfile.Exists(dirPath) {
|
||||
if err = gfile.Mkdir(dirPath); err != nil {
|
||||
return
|
||||
}
|
||||
} else if !gfile.IsDir(dirPath) {
|
||||
return "", errors.New(`parameter "dirPath" should be a directory path`)
|
||||
}
|
||||
|
||||
file, err := f.Open()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
filePath := path
|
||||
if gfile.IsDir(path) {
|
||||
name := gfile.Basename(f.Filename)
|
||||
if len(randomlyRename) > 0 && randomlyRename[0] {
|
||||
name = strings.ToLower(strconv.FormatInt(gtime.TimestampNano(), 36) + grand.S(6))
|
||||
name = name + gfile.Ext(f.Filename)
|
||||
}
|
||||
filePath = gfile.Join(path, name)
|
||||
name := gfile.Basename(f.Filename)
|
||||
if len(randomlyRename) > 0 && randomlyRename[0] {
|
||||
name = strings.ToLower(strconv.FormatInt(gtime.TimestampNano(), 36) + grand.S(6))
|
||||
name = name + gfile.Ext(f.Filename)
|
||||
}
|
||||
filePath := gfile.Join(dirPath, name)
|
||||
newFile, err := gfile.Create(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@ -75,13 +79,6 @@ func (fs UploadFiles) Save(dirPath string, randomlyRename ...bool) (filenames []
|
||||
if len(fs) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
if !gfile.Exists(dirPath) {
|
||||
if err = gfile.Mkdir(dirPath); err != nil {
|
||||
return
|
||||
}
|
||||
} else if !gfile.IsDir(dirPath) {
|
||||
return nil, errors.New(`parameter "dirPath" should be a directory path`)
|
||||
}
|
||||
for _, f := range fs {
|
||||
if filename, err := f.Save(dirPath, randomlyRename...); err != nil {
|
||||
return filenames, err
|
||||
|
||||
@ -22,9 +22,6 @@ import (
|
||||
|
||||
func Test_Params_File_Single(t *testing.T) {
|
||||
dstDirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
|
||||
err := gfile.Mkdir(dstDirPath)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/upload/single", func(r *ghttp.Request) {
|
||||
@ -77,11 +74,45 @@ func Test_Params_File_Single(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Params_File_CustomName(t *testing.T) {
|
||||
dstDirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/upload/single", func(r *ghttp.Request) {
|
||||
file := r.GetUploadFile("file")
|
||||
if file == nil {
|
||||
r.Response.WriteExit("upload file cannot be empty")
|
||||
}
|
||||
file.Filename = "my.txt"
|
||||
if name, err := file.Save(dstDirPath, r.GetBool("randomlyRename")); err == nil {
|
||||
r.Response.WriteExit(name)
|
||||
}
|
||||
r.Response.WriteExit("upload failed")
|
||||
})
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouterMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
srcPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "upload", "file1.txt")
|
||||
dstPath := gfile.Join(dstDirPath, "my.txt")
|
||||
content := client.PostContent("/upload/single", g.Map{
|
||||
"file": "@file:" + srcPath,
|
||||
})
|
||||
gtest.AssertNE(content, "")
|
||||
gtest.AssertNE(content, "upload file cannot be empty")
|
||||
gtest.AssertNE(content, "upload failed")
|
||||
gtest.Assert(content, "my.txt")
|
||||
gtest.Assert(gfile.GetContents(dstPath), gfile.GetContents(srcPath))
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Params_File_Batch(t *testing.T) {
|
||||
dstDirPath := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr())
|
||||
err := gfile.Mkdir(dstDirPath)
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/upload/batch", func(r *ghttp.Request) {
|
||||
@ -89,7 +120,6 @@ func Test_Params_File_Batch(t *testing.T) {
|
||||
if files == nil {
|
||||
r.Response.WriteExit("upload file cannot be empty")
|
||||
}
|
||||
|
||||
if names, err := files.Save(dstDirPath, r.GetBool("randomlyRename")); err == nil {
|
||||
r.Response.WriteExit(gstr.Join(names, ","))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user