alist/server/path.go

137 lines
2.7 KiB
Go
Raw Normal View History

2021-10-26 14:28:37 +00:00
package server
import (
2021-10-28 14:50:09 +00:00
"fmt"
2021-10-26 14:28:37 +00:00
"github.com/Xhofe/alist/model"
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
)
type PathReq struct {
Path string `json:"Path"`
Password string `json:"Password"`
}
2021-11-13 07:53:26 +00:00
func Path(c *gin.Context) {
2021-10-26 14:28:37 +00:00
var req PathReq
2021-11-13 07:53:26 +00:00
if err := c.ShouldBind(&req); err != nil {
ErrorResp(c, err, 400)
return
2021-10-26 14:28:37 +00:00
}
2021-10-28 14:50:09 +00:00
req.Path = utils.ParsePath(req.Path)
2021-11-02 11:25:54 +00:00
log.Debugf("path: %s", req.Path)
2021-10-28 14:50:09 +00:00
meta, err := model.GetMetaByPath(req.Path)
if err == nil {
2021-11-02 11:25:54 +00:00
if meta.Password != "" && meta.Password != req.Password {
2021-11-13 07:53:26 +00:00
ErrorResp(c, fmt.Errorf("wrong password"), 401)
return
2021-10-28 14:50:09 +00:00
}
// TODO hide or ignore?
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-11-13 07:53:26 +00:00
ErrorResp(c, err, 500)
return
2021-11-03 02:37:33 +00:00
}
2021-11-13 07:53:26 +00:00
c.JSON(200, 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
}
account, path, driver, err := ParsePath(req.Path)
if err != nil {
2021-11-13 07:53:26 +00:00
ErrorResp(c, err, 500)
return
2021-10-26 14:28:37 +00:00
}
file, files, err := driver.Path(path, account)
if err != nil {
2021-11-13 07:53:26 +00:00
ErrorResp(c, err, 500)
return
2021-10-26 14:28:37 +00:00
}
if file != nil {
2021-11-01 14:42:24 +00:00
if account.Type == "Native" {
2021-11-13 07:53:26 +00:00
file.Url = fmt.Sprintf("//%s/d%s", c.Request.Host, req.Path)
2021-11-01 14:42:24 +00:00
}
2021-11-13 07:53:26 +00:00
c.JSON(200, 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-11-02 11:25:54 +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
}
2021-11-13 07:53:26 +00:00
c.JSON(200, 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-11-13 07:53:26 +00:00
func Link(c *gin.Context) {
2021-10-30 16:36:17 +00:00
var req PathReq
2021-11-13 07:53:26 +00:00
if err := c.ShouldBind(&req); err != nil {
ErrorResp(c, err, 400)
return
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-10-30 16:36:17 +00:00
account, path, driver, err := ParsePath(rawPath)
if err != nil {
2021-11-13 07:53:26 +00:00
ErrorResp(c, err, 500)
return
2021-10-30 16:36:17 +00:00
}
link, err := driver.Link(path, account)
if err != nil {
2021-11-13 07:53:26 +00:00
ErrorResp(c, err, 500)
return
2021-10-30 16:36:17 +00:00
}
if account.Type == "Native" {
2021-11-13 07:53:26 +00:00
SuccessResp(c, gin.H{
"url": fmt.Sprintf("//%s/d%s", c.Request.Host, req.Path),
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-11-13 07:53:26 +00:00
SuccessResp(c, gin.H{
2021-11-02 11:25:54 +00:00
"url": link,
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
}
2021-10-31 13:27:47 +00:00
}
2021-11-13 07:53:26 +00:00
func Preview(c *gin.Context) {
2021-10-31 13:27:47 +00:00
var req PathReq
2021-11-13 07:53:26 +00:00
if err := c.ShouldBind(&req); err != nil {
ErrorResp(c, err, 400)
return
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-10-31 13:27:47 +00:00
account, path, driver, err := ParsePath(rawPath)
if err != nil {
2021-11-13 07:53:26 +00:00
ErrorResp(c, err, 500)
return
2021-10-31 13:27:47 +00:00
}
data, err := driver.Preview(path, account)
if err != nil {
2021-11-13 07:53:26 +00:00
ErrorResp(c, err, 500)
2021-11-02 11:25:54 +00:00
} else {
2021-11-13 07:53:26 +00:00
SuccessResp(c, data)
2021-10-31 13:27:47 +00:00
}
2021-11-02 11:25:54 +00:00
}