diff --git a/bootstrap/setting.go b/bootstrap/setting.go index 030ee460..11e067db 100644 --- a/bootstrap/setting.go +++ b/bootstrap/setting.go @@ -203,6 +203,21 @@ func InitSettings() { Access: model.PRIVATE, Group: model.BACK, }, + { + Key: "load type", + Value: "all", + Type: "select", + Values: "all,load more,auto load more,pagination", + Access: model.PUBLIC, + Group: model.FRONT, + }, + { + Key: "default page size", + Value: "30", + Type: "number", + Access: model.PUBLIC, + Group: model.FRONT, + }, } for i, _ := range settings { v := settings[i] diff --git a/conf/var.go b/conf/var.go index 07fdba9a..93572e6e 100644 --- a/conf/var.go +++ b/conf/var.go @@ -5,6 +5,7 @@ import ( "github.com/eko/gocache/v2/cache" "github.com/robfig/cron/v3" "gorm.io/gorm" + "strconv" ) var ( @@ -38,16 +39,57 @@ var ( ImageTypes = []string{"jpg", "tiff", "jpeg", "png", "gif", "bmp", "svg", "ico"} ) -// settings +var settingsMap = make(map[string]string, 0) + +func Set(key string, value string) { + settingsMap[key] = value +} + +func GetStr(key string) string { + value, ok := settingsMap[key] + if !ok { + return "" + } + return value +} + +func GetBool(key string) bool { + value, ok := settingsMap[key] + if !ok { + return false + } + return value == "true" +} + +func GetInt(key string, defaultV int) int { + value, ok := settingsMap[key] + if !ok { + return defaultV + } + v, err := strconv.Atoi(value) + if err != nil { + return defaultV + } + return v +} + +var ( + LoadSettings = []string{ + "check parent folder", "check down link", "WebDAV username", "WebDAV password", + "Visitor WebDAV username", "Visitor WebDAV password", + "default page size", "load type", + } +) + var ( RawIndexHtml string IndexHtml string - CheckParent bool - CheckDown bool + Token string - Token string - DavUsername string - DavPassword string - VisitorDavUsername string - VisitorDavPassword string + //CheckParent bool + //CheckDown bool + //DavUsername string + //DavPassword string + //VisitorDavUsername string + //VisitorDavPassword string ) diff --git a/model/setting.go b/model/setting.go index 37233e82..9e7cf5e8 100644 --- a/model/setting.go +++ b/model/setting.go @@ -93,14 +93,7 @@ func LoadSettings() { if err == nil { conf.TextTypes = strings.Split(textTypes.Value, ",") } - checkParent, err := GetSettingByKey("check parent folder") - if err == nil { - conf.CheckParent = checkParent.Value == "true" - } - checkDown, err := GetSettingByKey("check down link") - if err == nil { - conf.CheckDown = checkDown.Value == "true" - } + // html favicon, err := GetSettingByKey("favicon") if err == nil { //conf.Favicon = favicon.Value @@ -118,26 +111,16 @@ func LoadSettings() { if err == nil { conf.IndexHtml = strings.Replace(conf.IndexHtml, "", customizeBody.Value, 1) } - + // token adminPassword, err := GetSettingByKey("password") if err == nil { conf.Token = utils.GetMD5Encode(fmt.Sprintf("https://github.com/Xhofe/alist-%s", adminPassword.Value)) } - - davUsername, err := GetSettingByKey("WebDAV username") - if err == nil { - conf.DavUsername = davUsername.Value - } - davPassword, err := GetSettingByKey("WebDAV password") - if err == nil { - conf.DavPassword = davPassword.Value - } - visitorDavUsername, err := GetSettingByKey("Visitor WebDAV username") - if err == nil { - conf.VisitorDavUsername = visitorDavUsername.Value - } - visitorDavPassword, err := GetSettingByKey("Visitor WebDAV password") - if err == nil { - conf.VisitorDavPassword = visitorDavPassword.Value + // load settings + for _, key := range conf.LoadSettings { + vm, err := GetSettingByKey(key) + if err == nil { + conf.Set(key, vm.Value) + } } } diff --git a/server/common/check.go b/server/common/check.go index e7959bc5..23f22c40 100644 --- a/server/common/check.go +++ b/server/common/check.go @@ -28,7 +28,7 @@ func CheckParent(path string, password string) bool { } func CheckDownLink(path string, passwordMd5 string, name string) bool { - if !conf.CheckDown { + if !conf.GetBool("check down link") { return true } meta, err := model.GetMetaByPath(path) @@ -40,7 +40,7 @@ func CheckDownLink(path string, passwordMd5 string, name string) bool { } return true } else { - if !conf.CheckParent { + if !conf.GetBool("check parent folder") { return true } if path == "/" { diff --git a/server/controllers/path.go b/server/controllers/path.go index bf7a8ce8..8c300857 100644 --- a/server/controllers/path.go +++ b/server/controllers/path.go @@ -30,6 +30,12 @@ func Hide(meta *model.Meta, files []model.File) []model.File { func Pagination(files []model.File, pageNum, pageSize int) (int, []model.File) { total := len(files) + switch conf.GetStr("load type") { + case "all": + return total, files + //case "pagination": + // + } start := (pageNum - 1) * pageSize if start > total { return total, []model.File{} @@ -41,10 +47,16 @@ func Pagination(files []model.File, pageNum, pageSize int) (int, []model.File) { return total, files[start:end] } -func CheckPagination(req common.PathReq) error { +func CheckPagination(req *common.PathReq) error { + if conf.GetStr("loading type") == "all" { + return nil + } if req.PageNum < 1 { return errors.New("page_num can't be less than 1") } + if req.PageSize == 0 { + req.PageSize = conf.GetInt("default page size", 30) + } return nil } @@ -89,7 +101,7 @@ func Path(c *gin.Context) { }) return } - err := CheckPagination(req) + err := CheckPagination(&req) if err != nil { common.ErrorResp(c, err, 400) return diff --git a/server/middlewares/path.go b/server/middlewares/path.go index d7be49d3..d73097d0 100644 --- a/server/middlewares/path.go +++ b/server/middlewares/path.go @@ -16,7 +16,7 @@ func PathCheck(c *gin.Context) { return } req.Path = utils.ParsePath(req.Path) - c.Set("req",req) + c.Set("req", req) token := c.GetHeader("Authorization") if token == conf.Token { c.Next() @@ -29,7 +29,7 @@ func PathCheck(c *gin.Context) { c.Abort() return } - } else if conf.CheckParent { + } else if conf.GetBool("check parent folder") { if !common.CheckParent(utils.Dir(req.Path), req.Password) { common.ErrorResp(c, fmt.Errorf("wrong password"), 401) c.Abort() @@ -37,4 +37,4 @@ func PathCheck(c *gin.Context) { } } c.Next() -} \ No newline at end of file +} diff --git a/server/webdav.go b/server/webdav.go index f19095be..8bf9fbf9 100644 --- a/server/webdav.go +++ b/server/webdav.go @@ -49,11 +49,14 @@ func WebDAVAuth(c *gin.Context) { c.Abort() return } - if conf.DavUsername == username && conf.DavPassword == password { + if conf.GetStr("WebDAV username") == username && conf.GetStr("WebDAV password") == password { c.Next() return } - if (conf.VisitorDavUsername == username && conf.VisitorDavPassword == password) || (conf.VisitorDavUsername == "" && conf.VisitorDavPassword == "") { + if (conf.GetStr("Visitor WebDAV username") == username && + conf.GetStr("Visitor WebDAV password") == password) || + (conf.GetStr("Visitor WebDAV username") == "" && + conf.GetStr("Visitor WebDAV password") == "") { if !utils.IsContain([]string{"PUT", "DELETE", "PROPPATCH", "MKCOL", "COPY", "MOVE"}, c.Request.Method) { c.Next() return diff --git a/server/webdav/file.go b/server/webdav/file.go index 8a68de80..8db172e7 100644 --- a/server/webdav/file.go +++ b/server/webdav/file.go @@ -95,7 +95,7 @@ func (fs *FileSystem) Link(r *http.Request, rawPath string) (string, error) { } if driver.Config().OnlyProxy || account.WebdavProxy { link = fmt.Sprintf("%s://%s/p%s", protocol, r.Host, rawPath) - if conf.CheckDown { + if conf.GetBool("check down link") { sign := utils.SignWithToken(utils.Base(rawPath), conf.Token) link += "?sign" + sign }