🎇 Pagination #257

pull/548/head
微凉 2022-01-04 21:21:27 +08:00
parent b60c7ecd9e
commit ef5cad1bf0
8 changed files with 98 additions and 43 deletions

View File

@ -203,6 +203,21 @@ func InitSettings() {
Access: model.PRIVATE, Access: model.PRIVATE,
Group: model.BACK, 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 { for i, _ := range settings {
v := settings[i] v := settings[i]

View File

@ -5,6 +5,7 @@ import (
"github.com/eko/gocache/v2/cache" "github.com/eko/gocache/v2/cache"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
"gorm.io/gorm" "gorm.io/gorm"
"strconv"
) )
var ( var (
@ -38,16 +39,57 @@ var (
ImageTypes = []string{"jpg", "tiff", "jpeg", "png", "gif", "bmp", "svg", "ico"} 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 ( var (
RawIndexHtml string RawIndexHtml string
IndexHtml string IndexHtml string
CheckParent bool Token string
CheckDown bool
Token string //CheckParent bool
DavUsername string //CheckDown bool
DavPassword string //DavUsername string
VisitorDavUsername string //DavPassword string
VisitorDavPassword string //VisitorDavUsername string
//VisitorDavPassword string
) )

View File

@ -93,14 +93,7 @@ func LoadSettings() {
if err == nil { if err == nil {
conf.TextTypes = strings.Split(textTypes.Value, ",") conf.TextTypes = strings.Split(textTypes.Value, ",")
} }
checkParent, err := GetSettingByKey("check parent folder") // html
if err == nil {
conf.CheckParent = checkParent.Value == "true"
}
checkDown, err := GetSettingByKey("check down link")
if err == nil {
conf.CheckDown = checkDown.Value == "true"
}
favicon, err := GetSettingByKey("favicon") favicon, err := GetSettingByKey("favicon")
if err == nil { if err == nil {
//conf.Favicon = favicon.Value //conf.Favicon = favicon.Value
@ -118,26 +111,16 @@ func LoadSettings() {
if err == nil { if err == nil {
conf.IndexHtml = strings.Replace(conf.IndexHtml, "<!-- customize body -->", customizeBody.Value, 1) conf.IndexHtml = strings.Replace(conf.IndexHtml, "<!-- customize body -->", customizeBody.Value, 1)
} }
// token
adminPassword, err := GetSettingByKey("password") adminPassword, err := GetSettingByKey("password")
if err == nil { if err == nil {
conf.Token = utils.GetMD5Encode(fmt.Sprintf("https://github.com/Xhofe/alist-%s", adminPassword.Value)) conf.Token = utils.GetMD5Encode(fmt.Sprintf("https://github.com/Xhofe/alist-%s", adminPassword.Value))
} }
// load settings
davUsername, err := GetSettingByKey("WebDAV username") for _, key := range conf.LoadSettings {
if err == nil { vm, err := GetSettingByKey(key)
conf.DavUsername = davUsername.Value if err == nil {
} conf.Set(key, vm.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
} }
} }

View File

@ -28,7 +28,7 @@ func CheckParent(path string, password string) bool {
} }
func CheckDownLink(path string, passwordMd5 string, name string) bool { func CheckDownLink(path string, passwordMd5 string, name string) bool {
if !conf.CheckDown { if !conf.GetBool("check down link") {
return true return true
} }
meta, err := model.GetMetaByPath(path) meta, err := model.GetMetaByPath(path)
@ -40,7 +40,7 @@ func CheckDownLink(path string, passwordMd5 string, name string) bool {
} }
return true return true
} else { } else {
if !conf.CheckParent { if !conf.GetBool("check parent folder") {
return true return true
} }
if path == "/" { if path == "/" {

View File

@ -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) { func Pagination(files []model.File, pageNum, pageSize int) (int, []model.File) {
total := len(files) total := len(files)
switch conf.GetStr("load type") {
case "all":
return total, files
//case "pagination":
//
}
start := (pageNum - 1) * pageSize start := (pageNum - 1) * pageSize
if start > total { if start > total {
return total, []model.File{} return total, []model.File{}
@ -41,10 +47,16 @@ func Pagination(files []model.File, pageNum, pageSize int) (int, []model.File) {
return total, files[start:end] 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 { if req.PageNum < 1 {
return errors.New("page_num can't be less than 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 return nil
} }
@ -89,7 +101,7 @@ func Path(c *gin.Context) {
}) })
return return
} }
err := CheckPagination(req) err := CheckPagination(&req)
if err != nil { if err != nil {
common.ErrorResp(c, err, 400) common.ErrorResp(c, err, 400)
return return

View File

@ -16,7 +16,7 @@ func PathCheck(c *gin.Context) {
return return
} }
req.Path = utils.ParsePath(req.Path) req.Path = utils.ParsePath(req.Path)
c.Set("req",req) c.Set("req", req)
token := c.GetHeader("Authorization") token := c.GetHeader("Authorization")
if token == conf.Token { if token == conf.Token {
c.Next() c.Next()
@ -29,7 +29,7 @@ func PathCheck(c *gin.Context) {
c.Abort() c.Abort()
return return
} }
} else if conf.CheckParent { } else if conf.GetBool("check parent folder") {
if !common.CheckParent(utils.Dir(req.Path), req.Password) { if !common.CheckParent(utils.Dir(req.Path), req.Password) {
common.ErrorResp(c, fmt.Errorf("wrong password"), 401) common.ErrorResp(c, fmt.Errorf("wrong password"), 401)
c.Abort() c.Abort()
@ -37,4 +37,4 @@ func PathCheck(c *gin.Context) {
} }
} }
c.Next() c.Next()
} }

View File

@ -49,11 +49,14 @@ func WebDAVAuth(c *gin.Context) {
c.Abort() c.Abort()
return return
} }
if conf.DavUsername == username && conf.DavPassword == password { if conf.GetStr("WebDAV username") == username && conf.GetStr("WebDAV password") == password {
c.Next() c.Next()
return 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) { if !utils.IsContain([]string{"PUT", "DELETE", "PROPPATCH", "MKCOL", "COPY", "MOVE"}, c.Request.Method) {
c.Next() c.Next()
return return

View File

@ -95,7 +95,7 @@ func (fs *FileSystem) Link(r *http.Request, rawPath string) (string, error) {
} }
if driver.Config().OnlyProxy || account.WebdavProxy { if driver.Config().OnlyProxy || account.WebdavProxy {
link = fmt.Sprintf("%s://%s/p%s", protocol, r.Host, rawPath) 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) sign := utils.SignWithToken(utils.Base(rawPath), conf.Token)
link += "?sign" + sign link += "?sign" + sign
} }