alist/server/common/common.go

109 lines
2.3 KiB
Go
Raw Normal View History

2021-12-07 07:56:43 +00:00
package common
2021-10-26 14:28:37 +00:00
import (
"fmt"
2021-12-06 07:55:05 +00:00
"github.com/Xhofe/alist/drivers/base"
2021-10-26 14:28:37 +00:00
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
2021-11-13 07:53:26 +00:00
"github.com/gin-gonic/gin"
2021-11-16 07:00:56 +00:00
log "github.com/sirupsen/logrus"
2021-10-26 14:28:37 +00:00
"strings"
)
type Resp struct {
2021-11-13 07:53:26 +00:00
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
2021-10-26 14:28:37 +00:00
}
2021-12-07 07:56:43 +00:00
type PathReq struct {
Path string `json:"path"`
Password string `json:"password"`
2022-01-03 15:30:57 +00:00
PageNum int `json:"page_num"`
PageSize int `json:"page_size"`
2021-12-07 07:56:43 +00:00
}
2021-12-06 07:55:05 +00:00
func ParsePath(rawPath string) (*model.Account, string, base.Driver, error) {
2022-03-31 12:43:17 +00:00
rawPath = utils.ParsePath(rawPath)
account, ok := model.GetBalancedAccount(rawPath)
2021-10-26 14:28:37 +00:00
if !ok {
2022-03-31 12:43:17 +00:00
return nil, "", nil, fmt.Errorf("path not found")
2021-10-26 14:28:37 +00:00
}
2021-12-06 07:55:05 +00:00
driver, ok := base.GetDriver(account.Type)
2021-10-26 14:28:37 +00:00
if !ok {
2021-11-13 07:53:26 +00:00
return nil, "", nil, fmt.Errorf("no [%s] driver", account.Type)
2021-10-26 14:28:37 +00:00
}
2022-03-31 13:52:19 +00:00
name := utils.ParsePath(account.Name)
bIndex := strings.LastIndex(name, ".balance")
if bIndex != -1 {
name = name[:bIndex]
}
2022-04-01 12:40:57 +00:00
//if name == "/" {
// name = ""
//}
return &account, utils.ParsePath(strings.TrimPrefix(rawPath, name)), driver, nil
2021-10-26 14:28:37 +00:00
}
2021-11-13 07:53:26 +00:00
func ErrorResp(c *gin.Context, err error, code int) {
2021-11-16 07:00:56 +00:00
log.Error(err.Error())
2021-11-13 07:53:26 +00:00
c.JSON(200, Resp{
Code: code,
Message: err.Error(),
Data: nil,
2021-10-26 14:28:37 +00:00
})
2021-11-13 07:53:26 +00:00
c.Abort()
2021-10-26 14:28:37 +00:00
}
2022-01-13 13:23:27 +00:00
func ErrorStrResp(c *gin.Context, str string, code int) {
log.Error(str)
c.JSON(200, Resp{
Code: code,
Message: str,
Data: nil,
})
c.Abort()
}
2021-11-13 07:53:26 +00:00
func SuccessResp(c *gin.Context, data ...interface{}) {
2021-10-26 14:28:37 +00:00
if len(data) == 0 {
2021-11-13 07:53:26 +00:00
c.JSON(200, Resp{
Code: 200,
Message: "success",
Data: nil,
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-11-13 07:53:26 +00:00
c.JSON(200, Resp{
Code: 200,
Message: "success",
Data: data[0],
2021-10-26 14:28:37 +00:00
})
2021-11-13 07:53:26 +00:00
}
func Hide(meta *model.Meta, files []model.File) []model.File {
2022-03-13 11:37:58 +00:00
if meta == nil {
return files
}
if 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
}
2022-03-13 11:37:58 +00:00
if meta.OnlyShows != "" {
tmpFiles := make([]model.File, 0)
showFiles := strings.Split(meta.OnlyShows, ",")
for _, item := range files {
if utils.IsContain(showFiles, item.Name) {
tmpFiles = append(tmpFiles, item)
}
}
files = tmpFiles
}
return files
}