alist/server/controllers/path.go

203 lines
4.4 KiB
Go
Raw Normal View History

2021-12-07 07:56:43 +00:00
package controllers
2021-10-26 14:28:37 +00:00
import (
2022-01-03 15:30:57 +00:00
"errors"
2021-10-28 14:50:09 +00:00
"fmt"
2021-12-08 12:00:52 +00:00
"github.com/Xhofe/alist/conf"
2021-12-09 11:24:34 +00:00
"github.com/Xhofe/alist/drivers/base"
2021-10-26 14:28:37 +00:00
"github.com/Xhofe/alist/model"
2021-12-07 07:56:43 +00:00
"github.com/Xhofe/alist/server/common"
2021-10-28 14:50:09 +00:00
"github.com/Xhofe/alist/utils"
2021-11-13 07:53:26 +00:00
"github.com/gin-gonic/gin"
2021-10-28 14:50:09 +00:00
log "github.com/sirupsen/logrus"
2021-11-02 11:25:54 +00:00
"strings"
2021-10-26 14:28:37 +00:00
)
2022-01-03 15:30:57 +00:00
func Hide(meta *model.Meta, files []model.File) []model.File {
2021-12-31 12:33:39 +00:00
//meta, _ := model.GetMetaByPath(path)
2021-12-25 09:53:46 +00:00
if meta != nil && meta.Hide != "" {
tmpFiles := make([]model.File, 0)
hideFiles := strings.Split(meta.Hide, ",")
for _, item := range files {
if !utils.IsContain(hideFiles, item.Name) {
tmpFiles = append(tmpFiles, item)
}
}
files = tmpFiles
}
return files
}
2022-01-11 16:37:44 +00:00
func Pagination(files []model.File, req *common.PathReq) (int, []model.File) {
pageNum, pageSize := req.PageNum, req.PageSize
2022-01-03 15:30:57 +00:00
total := len(files)
2022-01-11 16:37:44 +00:00
if isAll(req) {
return total, files
}
2022-01-04 13:21:27 +00:00
switch conf.GetStr("load type") {
case "all":
return total, files
//case "pagination":
//
}
2022-01-03 15:30:57 +00:00
start := (pageNum - 1) * pageSize
if start > total {
return total, []model.File{}
}
end := start + pageSize
if end > total {
end = total
}
return total, files[start:end]
}
2022-01-11 16:37:44 +00:00
func isAll(req *common.PathReq) bool {
return req.PageNum == 0 && req.PageSize == 0
}
2022-01-04 13:21:27 +00:00
func CheckPagination(req *common.PathReq) error {
2022-01-11 16:37:44 +00:00
if isAll(req) {
return nil
}
2022-01-04 13:21:27 +00:00
if conf.GetStr("loading type") == "all" {
return nil
}
2022-01-03 15:30:57 +00:00
if req.PageNum < 1 {
return errors.New("page_num can't be less than 1")
}
2022-01-04 13:21:27 +00:00
if req.PageSize == 0 {
req.PageSize = conf.GetInt("default page size", 30)
}
2022-01-03 15:30:57 +00:00
return nil
}
2021-12-31 12:33:39 +00:00
type Meta struct {
Driver string `json:"driver"`
Upload bool `json:"upload"`
2022-01-03 15:30:57 +00:00
Total int `json:"total"`
//Pages int `json:"pages"`
2021-12-31 12:33:39 +00:00
}
type PathResp struct {
2021-12-31 12:33:39 +00:00
Type string `json:"type"`
Meta Meta `json:"meta"`
Files []model.File `json:"files"`
}
2021-11-13 07:53:26 +00:00
func Path(c *gin.Context) {
2021-12-08 02:33:26 +00:00
reqV, _ := c.Get("req")
2021-12-07 07:56:43 +00:00
req := reqV.(common.PathReq)
_, ok := c.Get("admin")
2021-12-31 12:33:39 +00:00
meta, _ := model.GetMetaByPath(req.Path)
upload := false
if meta != nil && meta.Upload {
upload = true
}
2021-10-28 04:37:31 +00:00
if model.AccountsCount() > 1 && req.Path == "/" {
2021-11-03 02:37:33 +00:00
files, err := model.GetAccountFiles()
if err != nil {
2021-12-07 07:56:43 +00:00
common.ErrorResp(c, err, 500)
2021-11-13 07:53:26 +00:00
return
2021-11-03 02:37:33 +00:00
}
if !ok {
files = Hide(meta, files)
}
2021-12-07 07:56:43 +00:00
c.JSON(200, common.Resp{
2021-11-02 11:25:54 +00:00
Code: 200,
Message: "success",
Data: PathResp{
2021-12-31 12:33:39 +00:00
Type: "folder",
Meta: Meta{
Driver: "root",
},
Files: files,
},
2021-10-26 14:28:37 +00:00
})
2021-11-13 07:53:26 +00:00
return
2021-10-26 14:28:37 +00:00
}
2022-01-04 13:21:27 +00:00
err := CheckPagination(&req)
2022-01-03 15:30:57 +00:00
if err != nil {
common.ErrorResp(c, err, 400)
return
}
2021-12-07 07:56:43 +00:00
account, path, driver, err := common.ParsePath(req.Path)
2021-10-26 14:28:37 +00:00
if err != nil {
2021-12-07 07:56:43 +00:00
common.ErrorResp(c, err, 500)
2021-11-13 07:53:26 +00:00
return
2021-10-26 14:28:37 +00:00
}
file, files, err := driver.Path(path, account)
if err != nil {
2021-12-07 07:56:43 +00:00
common.ErrorResp(c, err, 500)
2021-11-13 07:53:26 +00:00
return
2021-10-26 14:28:37 +00:00
}
if file != nil {
2021-12-08 12:00:52 +00:00
// 对于中转文件或只能中转,将链接修改为中转链接
if driver.Config().OnlyProxy || account.Proxy {
2021-12-19 09:10:20 +00:00
if account.DownProxyUrl != "" {
file.Url = fmt.Sprintf("%s%s?sign=%s", account.DownProxyUrl, req.Path, utils.SignWithToken(file.Name, conf.Token))
2021-12-09 11:24:34 +00:00
} else {
file.Url = fmt.Sprintf("//%s/p%s?sign=%s", c.Request.Host, req.Path, utils.SignWithToken(file.Name, conf.Token))
2021-12-08 12:00:52 +00:00
}
} else if !driver.Config().NoNeedSetLink {
2021-12-19 12:32:47 +00:00
link, err := driver.Link(base.Args{Path: path, IP: c.ClientIP()}, account)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
file.Url = link.Url
2021-11-01 14:42:24 +00:00
}
2021-12-07 07:56:43 +00:00
c.JSON(200, common.Resp{
2021-11-02 11:25:54 +00:00
Code: 200,
Message: "success",
Data: PathResp{
2021-12-31 12:33:39 +00:00
Type: "file",
Meta: Meta{
Driver: driver.Config().Name,
},
Files: []model.File{*file},
},
2021-10-26 14:28:37 +00:00
})
} else {
if !ok {
files = Hide(meta, files)
}
if driver.Config().LocalSort {
model.SortFiles(files, account)
}
2022-01-16 08:38:41 +00:00
model.ExtractFolder(files, account)
2022-01-11 16:37:44 +00:00
total, files := Pagination(files, &req)
2021-12-07 07:56:43 +00:00
c.JSON(200, common.Resp{
2021-11-02 11:25:54 +00:00
Code: 200,
Message: "success",
Data: PathResp{
2021-12-31 12:33:39 +00:00
Type: "folder",
Meta: Meta{
Driver: driver.Config().Name,
Upload: upload,
2022-01-03 15:30:57 +00:00
Total: total,
2021-12-31 12:33:39 +00:00
},
Files: files,
},
2021-10-26 14:28:37 +00:00
})
}
}
2021-10-30 16:36:17 +00:00
2021-11-13 07:53:26 +00:00
func Preview(c *gin.Context) {
2021-12-08 02:33:26 +00:00
reqV, _ := c.Get("req")
2021-12-07 07:56:43 +00:00
req := reqV.(common.PathReq)
2021-10-31 13:27:47 +00:00
rawPath := req.Path
rawPath = utils.ParsePath(rawPath)
2021-11-02 11:25:54 +00:00
log.Debugf("preview: %s", rawPath)
2021-12-07 07:56:43 +00:00
account, path, driver, err := common.ParsePath(rawPath)
2021-10-31 13:27:47 +00:00
if err != nil {
2021-12-07 07:56:43 +00:00
common.ErrorResp(c, err, 500)
2021-11-13 07:53:26 +00:00
return
2021-10-31 13:27:47 +00:00
}
data, err := driver.Preview(path, account)
if err != nil {
2021-12-07 07:56:43 +00:00
common.ErrorResp(c, err, 500)
2021-11-02 11:25:54 +00:00
} else {
2021-12-07 07:56:43 +00:00
common.SuccessResp(c, data)
2021-10-31 13:27:47 +00:00
}
2021-11-02 11:25:54 +00:00
}