🔨 change path

pull/548/head
微凉 2021-10-28 12:37:31 +08:00
parent 55f683b12d
commit 98f7dffed9
5 changed files with 51 additions and 26 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -29,39 +30,47 @@ type AliRespError struct {
} }
type AliFile struct { type AliFile struct {
AliRespError
DriveId string `json:"drive_id"` DriveId string `json:"drive_id"`
CreatedAt *time.Time `json:"created_at"` CreatedAt *time.Time `json:"created_at"`
DomainId string `json:"domain_id"`
EncryptMode string `json:"encrypt_mode"`
FileExtension string `json:"file_extension"` FileExtension string `json:"file_extension"`
FileId string `json:"file_id"` FileId string `json:"file_id"`
Hidden bool `json:"hidden"`
Name string `json:"name"` Name string `json:"name"`
ParentFileId string `json:"parent_file_id"` ParentFileId string `json:"parent_file_id"`
Starred bool `json:"starred"`
Status string `json:"status"`
Type string `json:"type"`
UpdatedAt *time.Time `json:"updated_at"` UpdatedAt *time.Time `json:"updated_at"`
Category string `json:"category"`
ContentHash string `json:"content_hash"`
ContentHashName string `json:"content_hash_name"`
ContentType string `json:"content_type"`
Crc64Hash string `json:"crc_64_hash"`
DownloadUrl string `json:"download_url"`
PunishFlag int64 `json:"punish_flag"`
Size int64 `json:"size"` Size int64 `json:"size"`
Thumbnail string `json:"thumbnail"` //Thumbnail string `json:"thumbnail"`
Url string `json:"url"` Url string `json:"url"`
ImageMediaMetadata map[string]interface{} `json:"image_media_metadata"`
} }
func (a AliDrive) Path(path string, account *model.Account) (*model.File, []*model.File, error) { func AliToFile(file AliFile) *model.File {
_, err := conf.Cache.Get(conf.Ctx, path) return &model.File{
if err == nil { Name: file.Name,
// return Size: file.Size,
Type: utils.GetFileType(file.FileExtension),
UpdatedAt: file.UpdatedAt,
} }
}
// path: /aaa/bbb
func (a AliDrive) Path(path string, account *model.Account) (*model.File, []*model.File, error) {
cache, err := conf.Cache.Get(conf.Ctx, path)
if err == nil {
file,ok := cache.(AliFile)
if ok {
return AliToFile(file), nil, nil
}else {
files,_ := cache.([]AliFile)
res := make([]*model.File,0)
for _,file = range files{
res = append(res, AliToFile(file))
}
return nil, res, nil
}
}else {
if path != "/" {
}
}
panic("implement me") panic("implement me")
} }

View File

@ -22,3 +22,10 @@ func Auth(ctx *fiber.Ctx) error {
} }
return ctx.Next() return ctx.Next()
} }
func CheckAccount(ctx *fiber.Ctx) error {
if model.AccountsCount() == 0 {
return ErrorResp(ctx,fmt.Errorf("no accounts,please add one first"),1001)
}
return ctx.Next()
}

View File

@ -27,12 +27,12 @@ func ParsePath(rawPath string) (*model.Account,string,drivers.Driver,error) {
break break
default: default:
paths := strings.Split(rawPath,"/") paths := strings.Split(rawPath,"/")
path = strings.Join(paths[1:],"/") path = strings.Join(paths[2:],"/")
name = paths[0] name = paths[1]
} }
account,ok := model.GetAccount(name) account,ok := model.GetAccount(name)
if !ok { if !ok {
return nil,"",nil,fmt.Errorf("") return nil,"",nil,fmt.Errorf("no [%s] account", name)
} }
driver,ok := drivers.GetDriver(account.Type) driver,ok := drivers.GetDriver(account.Type)
if !ok { if !ok {

View File

@ -3,6 +3,7 @@ package server
import ( import (
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"strings"
) )
type PathReq struct { type PathReq struct {
@ -15,7 +16,10 @@ func Path(ctx *fiber.Ctx) error {
if err := ctx.BodyParser(&req); err != nil { if err := ctx.BodyParser(&req); err != nil {
return ErrorResp(ctx, err, 400) return ErrorResp(ctx, err, 400)
} }
if model.AccountsCount() > 1 && req.Path == "" { if !strings.HasPrefix(req.Path, "/") {
req.Path = "/"+req.Path
}
if model.AccountsCount() > 1 && req.Path == "/" {
return ctx.JSON(Resp{ return ctx.JSON(Resp{
Code: 200, Code: 200,
Msg: "folder", Msg: "folder",

View File

@ -1,15 +1,20 @@
package server package server
import "github.com/gofiber/fiber/v2" import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
)
func InitApiRouter(app *fiber.App) { func InitApiRouter(app *fiber.App) {
// TODO from settings
app.Use(cors.New())
app.Get("/d/*", Down) app.Get("/d/*", Down)
public := app.Group("/api/public") public := app.Group("/api/public")
{ {
// TODO check accounts // TODO check accounts
public.Post("/path", Path) public.Post("/path", CheckAccount, Path)
public.Get("/settings", GetSettingsPublic) public.Get("/settings", GetSettingsPublic)
} }