alist/server/controllers/path.go

144 lines
3.3 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 (
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
)
2021-12-31 12:33:39 +00:00
func Hide(meta *model.Meta, files []model.File, path string) []model.File {
//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
}
2021-12-31 12:33:39 +00:00
type Meta struct {
Driver string `json:"driver"`
Upload bool `json:"upload"`
}
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)
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
}
2021-12-31 12:33:39 +00:00
files = Hide(meta, files, req.Path)
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
}
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 {
2021-12-31 12:33:39 +00:00
files = Hide(meta, files, req.Path)
if driver.Config().LocalSort {
model.SortFiles(files, account)
}
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,
},
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
}