feat: optimize file operation interface (#2757)
* feat: optimize file operation interface * chore: fix typo Co-authored-by: Noah Hsu <i@nn.ci>
This commit is contained in:
@ -48,7 +48,6 @@ func FsMkdir(c *gin.Context) {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
fs.ClearCache(stdpath.Dir(reqPath))
|
||||
common.SuccessResp(c)
|
||||
}
|
||||
|
||||
@ -83,15 +82,13 @@ func FsMove(c *gin.Context) {
|
||||
common.ErrorResp(c, err, 403)
|
||||
return
|
||||
}
|
||||
for _, name := range req.Names {
|
||||
err := fs.Move(c, stdpath.Join(srcDir, name), dstDir)
|
||||
for i, name := range req.Names {
|
||||
err := fs.Move(c, stdpath.Join(srcDir, name), dstDir, len(req.Names) > i+1)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
}
|
||||
fs.ClearCache(srcDir)
|
||||
fs.ClearCache(dstDir)
|
||||
common.SuccessResp(c)
|
||||
}
|
||||
|
||||
@ -121,8 +118,8 @@ func FsCopy(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
var addedTask []string
|
||||
for _, name := range req.Names {
|
||||
ok, err := fs.Copy(c, stdpath.Join(srcDir, name), dstDir)
|
||||
for i, name := range req.Names {
|
||||
ok, err := fs.Copy(c, stdpath.Join(srcDir, name), dstDir, len(req.Names) > i+1)
|
||||
if ok {
|
||||
addedTask = append(addedTask, name)
|
||||
}
|
||||
@ -131,9 +128,6 @@ func FsCopy(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
}
|
||||
if len(req.Names) != len(addedTask) {
|
||||
fs.ClearCache(dstDir)
|
||||
}
|
||||
if len(addedTask) > 0 {
|
||||
common.SuccessResp(c, fmt.Sprintf("Added %d tasks", len(addedTask)))
|
||||
} else {
|
||||
@ -166,7 +160,6 @@ func FsRename(c *gin.Context) {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
fs.ClearCache(stdpath.Dir(reqPath))
|
||||
common.SuccessResp(c)
|
||||
}
|
||||
|
||||
|
@ -191,10 +191,7 @@ func pagination(objs []model.Obj, req *model.PageReq) (int, []model.Obj) {
|
||||
func toObjsResp(objs []model.Obj, parent string, encrypt bool) []ObjResp {
|
||||
var resp []ObjResp
|
||||
for _, obj := range objs {
|
||||
thumb := ""
|
||||
if t, ok := obj.(model.Thumb); ok {
|
||||
thumb = t.Thumb()
|
||||
}
|
||||
thumb, _ := model.GetThumb(obj)
|
||||
resp = append(resp, ObjResp{
|
||||
Name: obj.GetName(),
|
||||
Size: obj.GetSize(),
|
||||
@ -276,8 +273,8 @@ func FsGet(c *gin.Context) {
|
||||
}
|
||||
} else {
|
||||
// file have raw url
|
||||
if u, ok := obj.(model.URL); ok {
|
||||
rawURL = u.URL()
|
||||
if url, ok := model.GetUrl(obj); ok {
|
||||
rawURL = url
|
||||
} else {
|
||||
// if storage is not proxy, use raw url by fs.Link
|
||||
link, _, err := fs.Link(c, reqPath, model.LinkArgs{IP: c.ClientIP(), Header: c.Request.Header})
|
||||
|
@ -46,7 +46,7 @@ func FsStream(c *gin.Context) {
|
||||
if asTask {
|
||||
err = fs.PutAsTask(dir, stream)
|
||||
} else {
|
||||
err = fs.PutDirectly(c, dir, stream)
|
||||
err = fs.PutDirectly(c, dir, stream, true)
|
||||
}
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
@ -102,7 +102,7 @@ func FsForm(c *gin.Context) {
|
||||
if asTask {
|
||||
err = fs.PutAsTask(dir, stream)
|
||||
} else {
|
||||
err = fs.PutDirectly(c, dir, stream)
|
||||
err = fs.PutDirectly(c, dir, stream, true)
|
||||
}
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
|
@ -46,8 +46,6 @@ func moveFiles(ctx context.Context, src, dst string, overwrite bool) (status int
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
fs.ClearCache(srcDir)
|
||||
fs.ClearCache(dstDir)
|
||||
// TODO if there are no files copy, should return 204
|
||||
return http.StatusCreated, nil
|
||||
}
|
||||
@ -60,7 +58,6 @@ func copyFiles(ctx context.Context, src, dst string, overwrite bool) (status int
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
fs.ClearCache(path.Dir(dst))
|
||||
// TODO if there are no files copy, should return 204
|
||||
return http.StatusCreated, nil
|
||||
}
|
||||
|
@ -337,7 +337,6 @@ func (h *Handler) handlePut(w http.ResponseWriter, r *http.Request) (status int,
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
w.Header().Set("ETag", etag)
|
||||
fs.ClearCache(path.Dir(reqPath))
|
||||
return http.StatusCreated, nil
|
||||
}
|
||||
|
||||
@ -368,7 +367,6 @@ func (h *Handler) handleMkcol(w http.ResponseWriter, r *http.Request) (status in
|
||||
}
|
||||
return http.StatusMethodNotAllowed, err
|
||||
}
|
||||
fs.ClearCache(path.Dir(reqPath))
|
||||
return http.StatusCreated, nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user