Skip to content

Commit

Permalink
🎇 Pagination AlistGo#257
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jan 4, 2022
1 parent b60c7ec commit ef5cad1
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 44 deletions.
15 changes: 15 additions & 0 deletions bootstrap/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
60 changes: 51 additions & 9 deletions conf/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/eko/gocache/v2/cache"
"github.com/robfig/cron/v3"
"gorm.io/gorm"
"strconv"
)

var (
Expand Down Expand Up @@ -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
DavUsername string
DavPassword string
VisitorDavUsername string
VisitorDavPassword string
Token string

//CheckParent bool
//CheckDown bool
//DavUsername string
//DavPassword string
//VisitorDavUsername string
//VisitorDavPassword string
)
33 changes: 8 additions & 25 deletions model/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -118,26 +111,16 @@ func LoadSettings() {
if err == nil {
conf.IndexHtml = strings.Replace(conf.IndexHtml, "<!-- customize body -->", 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)
}
}
}
4 changes: 2 additions & 2 deletions server/common/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 == "/" {
Expand Down
16 changes: 14 additions & 2 deletions server/controllers/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions server/middlewares/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -29,12 +29,12 @@ 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()
return
}
}
c.Next()
}
}
7 changes: 5 additions & 2 deletions server/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server/webdav/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit ef5cad1

Please sign in to comment.