diff --git a/drivers/alidrive.go b/drivers/alidrive.go index a4c18c18..0814a316 100644 --- a/drivers/alidrive.go +++ b/drivers/alidrive.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/model" + "github.com/Xhofe/alist/utils" "github.com/go-resty/resty/v2" "github.com/robfig/cron/v3" log "github.com/sirupsen/logrus" @@ -29,39 +30,47 @@ type AliRespError struct { } type AliFile struct { - AliRespError DriveId string `json:"drive_id"` CreatedAt *time.Time `json:"created_at"` - DomainId string `json:"domain_id"` - EncryptMode string `json:"encrypt_mode"` FileExtension string `json:"file_extension"` FileId string `json:"file_id"` - Hidden bool `json:"hidden"` Name string `json:"name"` ParentFileId string `json:"parent_file_id"` - Starred bool `json:"starred"` - Status string `json:"status"` - Type string `json:"type"` 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"` - Thumbnail string `json:"thumbnail"` + //Thumbnail string `json:"thumbnail"` 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) { - _, err := conf.Cache.Get(conf.Ctx, path) - if err == nil { - // return +func AliToFile(file AliFile) *model.File { + return &model.File{ + Name: file.Name, + 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") } diff --git a/server/auth.go b/server/check.go similarity index 75% rename from server/auth.go rename to server/check.go index c01bb887..cb40e904 100644 --- a/server/auth.go +++ b/server/check.go @@ -22,3 +22,10 @@ func Auth(ctx *fiber.Ctx) error { } 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() +} \ No newline at end of file diff --git a/server/common.go b/server/common.go index 1d3a6190..eb6d1d36 100644 --- a/server/common.go +++ b/server/common.go @@ -27,12 +27,12 @@ func ParsePath(rawPath string) (*model.Account,string,drivers.Driver,error) { break default: paths := strings.Split(rawPath,"/") - path = strings.Join(paths[1:],"/") - name = paths[0] + path = strings.Join(paths[2:],"/") + name = paths[1] } account,ok := model.GetAccount(name) if !ok { - return nil,"",nil,fmt.Errorf("") + return nil,"",nil,fmt.Errorf("no [%s] account", name) } driver,ok := drivers.GetDriver(account.Type) if !ok { diff --git a/server/path.go b/server/path.go index e2a594e0..47e87283 100644 --- a/server/path.go +++ b/server/path.go @@ -3,6 +3,7 @@ package server import ( "github.com/Xhofe/alist/model" "github.com/gofiber/fiber/v2" + "strings" ) type PathReq struct { @@ -15,7 +16,10 @@ func Path(ctx *fiber.Ctx) error { if err := ctx.BodyParser(&req); err != nil { 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{ Code: 200, Msg: "folder", diff --git a/server/router.go b/server/router.go index 4962a7a1..b1a38f91 100644 --- a/server/router.go +++ b/server/router.go @@ -1,15 +1,20 @@ 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) { + // TODO from settings + app.Use(cors.New()) app.Get("/d/*", Down) public := app.Group("/api/public") { // TODO check accounts - public.Post("/path", Path) + public.Post("/path", CheckAccount, Path) public.Get("/settings", GetSettingsPublic) }