alist/server/controllers/path.go

125 lines
2.9 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-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-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-07 07:56:43 +00:00
c.JSON(200, common.Resp{
2021-11-02 11:25:54 +00:00
Code: 200,
Message: "folder",
2021-11-03 02:37:33 +00:00
Data: 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 {
if account.ProxyUrl != "" {
file.Url = fmt.Sprintf("%s%s?sign=%s", account.ProxyUrl, req.Path, utils.SignWithToken(file.Name, conf.Token))
2021-12-09 11:24:34 +00:00
} else {
2021-12-08 12:00:52 +00:00
file.Url = fmt.Sprintf("//%s/d%s", c.Request.Host, req.Path)
}
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: "file",
Data: []*model.File{file},
2021-10-26 14:28:37 +00:00
})
} else {
2021-12-07 07:56:43 +00:00
meta, _ := model.GetMetaByPath(req.Path)
2021-11-02 11:25:54 +00:00
if meta != nil && meta.Hide != "" {
2021-11-23 07:58:03 +00:00
tmpFiles := make([]model.File, 0)
2021-11-02 11:25:54 +00:00
hideFiles := strings.Split(meta.Hide, ",")
for _, item := range files {
if !utils.IsContain(hideFiles, item.Name) {
tmpFiles = append(tmpFiles, item)
}
}
files = tmpFiles
}
2021-12-07 07:56:43 +00:00
c.JSON(200, common.Resp{
2021-11-02 11:25:54 +00:00
Code: 200,
Message: "folder",
Data: files,
2021-10-26 14:28:37 +00:00
})
}
}
2021-10-30 16:36:17 +00:00
2021-12-09 11:24:34 +00:00
// 返回真实的链接,且携带头,只提供给中转程序使用
2021-11-13 07:53:26 +00:00
func Link(c *gin.Context) {
2021-12-08 12:27:39 +00:00
var req common.PathReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
req.Path = utils.ParsePath(req.Path)
2021-10-30 16:36:17 +00:00
rawPath := req.Path
rawPath = utils.ParsePath(rawPath)
2021-11-02 11:25:54 +00:00
log.Debugf("link: %s", rawPath)
2021-12-07 07:56:43 +00:00
account, path, driver, err := common.ParsePath(rawPath)
2021-10-30 16:36:17 +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-30 16:36:17 +00:00
}
link, err := driver.Link(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-30 16:36:17 +00:00
}
2021-12-09 11:24:34 +00:00
if driver.Config().NoLink {
common.SuccessResp(c, base.Link{
Url: fmt.Sprintf("//%s/d%s?sign=%s", c.Request.Host, req.Path, utils.SignWithToken(utils.Base(rawPath), conf.Token)),
2021-10-30 16:36:17 +00:00
})
2021-11-13 07:53:26 +00:00
return
2021-10-30 16:36:17 +00:00
} else {
2021-12-09 11:24:34 +00:00
common.SuccessResp(c, link)
2021-11-13 07:53:26 +00:00
return
2021-10-30 16:36:17 +00:00
}
2021-10-31 13:27:47 +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
}