From 77a6aa487bb7c45d093228d36da83fb50ade850f Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Sun, 11 Sep 2022 14:14:14 +0800 Subject: [PATCH] chore: cancel sign if no password --- server/common/sign.go | 4 ++-- server/handles/fsread.go | 24 ++++++++++++++++++------ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/server/common/sign.go b/server/common/sign.go index 5b509eda..2a1cdb9e 100644 --- a/server/common/sign.go +++ b/server/common/sign.go @@ -5,8 +5,8 @@ import ( "github.com/alist-org/alist/v3/internal/sign" ) -func Sign(obj model.Obj) string { - if obj.IsDir() { +func Sign(obj model.Obj, encrypt bool) string { + if obj.IsDir() || !encrypt { return "" } return sign.Sign(obj.GetName()) diff --git a/server/handles/fsread.go b/server/handles/fsread.go index 9f6b4e90..0c09cf47 100644 --- a/server/handles/fsread.go +++ b/server/handles/fsread.go @@ -79,7 +79,7 @@ func FsList(c *gin.Context) { } total, objs := pagination(objs, &req.PageReq) common.SuccessResp(c, FsListResp{ - Content: toObjResp(objs), + Content: toObjResp(objs, isEncrypt(meta, req.Path)), Total: int64(total), Readme: getReadme(meta, req.Path), Write: user.CanWrite() || canWrite(meta, req.Path), @@ -157,6 +157,16 @@ func canAccess(user *model.User, meta *model.Meta, path string, password string) return meta.Password == password } +func isEncrypt(meta *model.Meta, path string) bool { + if meta == nil || meta.Password == "" { + return false + } + if !utils.PathEqual(meta.Path, path) && !meta.PSub { + return false + } + return true +} + func pagination(objs []model.Obj, req *common.PageReq) (int, []model.Obj) { pageIndex, pageSize := req.Page, req.PerPage total := len(objs) @@ -171,7 +181,7 @@ func pagination(objs []model.Obj, req *common.PageReq) (int, []model.Obj) { return total, objs[start:end] } -func toObjResp(objs []model.Obj) []ObjResp { +func toObjResp(objs []model.Obj, encrypt bool) []ObjResp { var resp []ObjResp for _, obj := range objs { thumb := "" @@ -187,7 +197,7 @@ func toObjResp(objs []model.Obj) []ObjResp { Size: obj.GetSize(), IsDir: obj.IsDir(), Modified: obj.ModTime(), - Sign: common.Sign(obj), + Sign: common.Sign(obj, encrypt), Thumb: thumb, Type: tp, }) @@ -270,23 +280,25 @@ func FsGet(c *gin.Context) { } } var related []model.Obj - sameLevelFiles, err := fs.List(c, stdpath.Dir(req.Path)) + parentPath := stdpath.Dir(req.Path) + sameLevelFiles, err := fs.List(c, parentPath) if err == nil { related = filterRelated(sameLevelFiles, obj) } + parentMeta, _ := db.GetNearestMeta(parentPath) common.SuccessResp(c, FsGetResp{ ObjResp: ObjResp{ Name: obj.GetName(), Size: obj.GetSize(), IsDir: obj.IsDir(), Modified: obj.ModTime(), - Sign: common.Sign(obj), + Sign: common.Sign(obj, isEncrypt(meta, req.Path)), Type: utils.GetFileType(obj.GetName()), }, RawURL: rawURL, Readme: getReadme(meta, req.Path), Provider: provider, - Related: toObjResp(related), + Related: toObjResp(related, isEncrypt(parentMeta, parentPath)), }) }