2021-12-07 07:56:43 +00:00
|
|
|
package common
|
2021-10-26 14:28:37 +00:00
|
|
|
|
|
|
|
import (
|
2021-12-31 12:33:39 +00:00
|
|
|
"errors"
|
2021-10-26 14:28:37 +00:00
|
|
|
"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"
|
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) {
|
2021-11-13 07:53:26 +00:00
|
|
|
var path, name string
|
2021-10-26 14:28:37 +00:00
|
|
|
switch model.AccountsCount() {
|
|
|
|
case 0:
|
2021-11-13 07:53:26 +00:00
|
|
|
return nil, "", nil, fmt.Errorf("no accounts,please add one first")
|
2021-10-26 14:28:37 +00:00
|
|
|
case 1:
|
|
|
|
path = rawPath
|
|
|
|
break
|
|
|
|
default:
|
2021-12-31 12:33:39 +00:00
|
|
|
if path == "/" {
|
|
|
|
return nil, "", nil, errors.New("can't operate root of multiple accounts")
|
|
|
|
}
|
2021-11-13 07:53:26 +00:00
|
|
|
paths := strings.Split(rawPath, "/")
|
|
|
|
path = "/" + strings.Join(paths[2:], "/")
|
2021-10-28 04:37:31 +00:00
|
|
|
name = paths[1]
|
2021-10-26 14:28:37 +00:00
|
|
|
}
|
2021-11-13 07:53:26 +00:00
|
|
|
account, ok := model.GetAccount(name)
|
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] account", name)
|
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
|
|
|
}
|
2021-11-13 07:53:26 +00:00
|
|
|
return &account, path, 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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|