chore: cancel sign if no password

This commit is contained in:
Noah Hsu 2022-09-11 14:14:14 +08:00
parent fd99c2197b
commit 77a6aa487b
2 changed files with 20 additions and 8 deletions

View File

@ -5,8 +5,8 @@ import (
"github.com/alist-org/alist/v3/internal/sign" "github.com/alist-org/alist/v3/internal/sign"
) )
func Sign(obj model.Obj) string { func Sign(obj model.Obj, encrypt bool) string {
if obj.IsDir() { if obj.IsDir() || !encrypt {
return "" return ""
} }
return sign.Sign(obj.GetName()) return sign.Sign(obj.GetName())

View File

@ -79,7 +79,7 @@ func FsList(c *gin.Context) {
} }
total, objs := pagination(objs, &req.PageReq) total, objs := pagination(objs, &req.PageReq)
common.SuccessResp(c, FsListResp{ common.SuccessResp(c, FsListResp{
Content: toObjResp(objs), Content: toObjResp(objs, isEncrypt(meta, req.Path)),
Total: int64(total), Total: int64(total),
Readme: getReadme(meta, req.Path), Readme: getReadme(meta, req.Path),
Write: user.CanWrite() || canWrite(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 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) { func pagination(objs []model.Obj, req *common.PageReq) (int, []model.Obj) {
pageIndex, pageSize := req.Page, req.PerPage pageIndex, pageSize := req.Page, req.PerPage
total := len(objs) total := len(objs)
@ -171,7 +181,7 @@ func pagination(objs []model.Obj, req *common.PageReq) (int, []model.Obj) {
return total, objs[start:end] return total, objs[start:end]
} }
func toObjResp(objs []model.Obj) []ObjResp { func toObjResp(objs []model.Obj, encrypt bool) []ObjResp {
var resp []ObjResp var resp []ObjResp
for _, obj := range objs { for _, obj := range objs {
thumb := "" thumb := ""
@ -187,7 +197,7 @@ func toObjResp(objs []model.Obj) []ObjResp {
Size: obj.GetSize(), Size: obj.GetSize(),
IsDir: obj.IsDir(), IsDir: obj.IsDir(),
Modified: obj.ModTime(), Modified: obj.ModTime(),
Sign: common.Sign(obj), Sign: common.Sign(obj, encrypt),
Thumb: thumb, Thumb: thumb,
Type: tp, Type: tp,
}) })
@ -270,23 +280,25 @@ func FsGet(c *gin.Context) {
} }
} }
var related []model.Obj 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 { if err == nil {
related = filterRelated(sameLevelFiles, obj) related = filterRelated(sameLevelFiles, obj)
} }
parentMeta, _ := db.GetNearestMeta(parentPath)
common.SuccessResp(c, FsGetResp{ common.SuccessResp(c, FsGetResp{
ObjResp: ObjResp{ ObjResp: ObjResp{
Name: obj.GetName(), Name: obj.GetName(),
Size: obj.GetSize(), Size: obj.GetSize(),
IsDir: obj.IsDir(), IsDir: obj.IsDir(),
Modified: obj.ModTime(), Modified: obj.ModTime(),
Sign: common.Sign(obj), Sign: common.Sign(obj, isEncrypt(meta, req.Path)),
Type: utils.GetFileType(obj.GetName()), Type: utils.GetFileType(obj.GetName()),
}, },
RawURL: rawURL, RawURL: rawURL,
Readme: getReadme(meta, req.Path), Readme: getReadme(meta, req.Path),
Provider: provider, Provider: provider,
Related: toObjResp(related), Related: toObjResp(related, isEncrypt(parentMeta, parentPath)),
}) })
} }