diff --git a/.gitignore b/.gitignore index 82c90ee0..8891ed1c 100644 --- a/.gitignore +++ b/.gitignore @@ -20,7 +20,6 @@ dist/ # Dependency directories (remove the comment below to include it) # vendor/ -*.yml bin/* alist *.json diff --git a/model/account.go b/model/account.go index c04b8973..8132ec00 100644 --- a/model/account.go +++ b/model/account.go @@ -21,6 +21,7 @@ type Account struct { OrderDirection string `json:"order_direction"` Proxy bool `json:"proxy"` UpdatedAt *time.Time `json:"updated_at"` + Search bool `json:"search"` } var accountsMap = map[string]Account{} diff --git a/model/meta.go b/model/meta.go index a446434f..3dbda541 100644 --- a/model/meta.go +++ b/model/meta.go @@ -3,13 +3,12 @@ package model import "github.com/Xhofe/alist/conf" type Meta struct { - Path string `json:"path" gorm:"primaryKey"` + Path string `json:"path" gorm:"primaryKey" validate:"required"` Password string `json:"password"` - Hide bool `json:"hide"` - Ignore bool `json:"ignore"` + Hide string `json:"hide"` } -func GetMetaByPath(path string) (*Meta,error) { +func GetMetaByPath(path string) (*Meta, error) { var meta Meta meta.Path = path err := conf.DB.First(&meta).Error @@ -17,4 +16,21 @@ func GetMetaByPath(path string) (*Meta,error) { return nil, err } return &meta, nil -} \ No newline at end of file +} + +func SaveMeta(meta Meta) error { + return conf.DB.Save(meta).Error +} + +func DeleteMeta(path string) error { + meta := Meta{Path: path} + return conf.DB.Delete(&meta).Error +} + +func GetMetas() (*[]Meta, error) { + var metas []Meta + if err := conf.DB.Find(&metas).Error; err != nil { + return nil, err + } + return &metas, nil +} diff --git a/server/meta.go b/server/meta.go new file mode 100644 index 00000000..c94d3818 --- /dev/null +++ b/server/meta.go @@ -0,0 +1,39 @@ +package server + +import ( + "github.com/Xhofe/alist/model" + "github.com/Xhofe/alist/utils" + "github.com/gofiber/fiber/v2" +) + +func GetMetas(ctx *fiber.Ctx) error { + metas,err := model.GetMetas() + if err != nil { + return ErrorResp(ctx,err,500) + } + return SuccessResp(ctx, metas) +} + +func SaveMeta(ctx *fiber.Ctx) error { + var req model.Meta + if err := ctx.BodyParser(&req); err != nil { + return ErrorResp(ctx, err, 400) + } + if err := validate.Struct(req); err != nil { + return ErrorResp(ctx, err, 400) + } + if err := model.SaveMeta(req); err != nil { + return ErrorResp(ctx, err, 500) + } else { + return SuccessResp(ctx) + } +} + +func DeleteMeta(ctx *fiber.Ctx) error { + path := ctx.Query("path") + path = utils.ParsePath(path) + if err := model.DeleteMeta(path); err != nil { + return ErrorResp(ctx, err, 500) + } + return SuccessResp(ctx) +} diff --git a/server/path.go b/server/path.go index b561aa10..5e91cff3 100644 --- a/server/path.go +++ b/server/path.go @@ -6,6 +6,7 @@ import ( "github.com/Xhofe/alist/utils" "github.com/gofiber/fiber/v2" log "github.com/sirupsen/logrus" + "strings" ) type PathReq struct { @@ -19,19 +20,19 @@ func Path(ctx *fiber.Ctx) error { return ErrorResp(ctx, err, 400) } req.Path = utils.ParsePath(req.Path) - log.Debugf("path: %s",req.Path) + log.Debugf("path: %s", req.Path) meta, err := model.GetMetaByPath(req.Path) if err == nil { - if meta.Password != "" && meta.Password!= req.Password { - return ErrorResp(ctx,fmt.Errorf("wrong password"),401) + if meta.Password != "" && meta.Password != req.Password { + return ErrorResp(ctx, fmt.Errorf("wrong password"), 401) } // TODO hide or ignore? } if model.AccountsCount() > 1 && req.Path == "/" { return ctx.JSON(Resp{ - Code: 200, - Message: "folder", - Data: model.GetAccountFiles(), + Code: 200, + Message: "folder", + Data: model.GetAccountFiles(), }) } account, path, driver, err := ParsePath(req.Path) @@ -44,18 +45,28 @@ func Path(ctx *fiber.Ctx) error { } if file != nil { if account.Type == "Native" { - file.Url = fmt.Sprintf("%s://%s/p%s",ctx.Protocol(),ctx.Hostname(),req.Path) + file.Url = fmt.Sprintf("%s://%s/p%s", ctx.Protocol(), ctx.Hostname(), req.Path) } return ctx.JSON(Resp{ - Code: 200, - Message: "file", - Data: []*model.File{file}, + Code: 200, + Message: "file", + Data: []*model.File{file}, }) } else { + 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 + } return ctx.JSON(Resp{ - Code: 200, - Message: "folder", - Data: files, + Code: 200, + Message: "folder", + Data: files, }) } } @@ -67,7 +78,7 @@ func Link(ctx *fiber.Ctx) error { } rawPath := req.Path rawPath = utils.ParsePath(rawPath) - log.Debugf("link: %s",rawPath) + log.Debugf("link: %s", rawPath) account, path, driver, err := ParsePath(rawPath) if err != nil { return ErrorResp(ctx, err, 500) @@ -78,11 +89,11 @@ func Link(ctx *fiber.Ctx) error { } if account.Type == "Native" { return SuccessResp(ctx, fiber.Map{ - "url":fmt.Sprintf("%s://%s/p%s",ctx.Protocol(),ctx.Hostname(),rawPath), + "url": fmt.Sprintf("%s://%s/p%s", ctx.Protocol(), ctx.Hostname(), rawPath), }) } else { - return SuccessResp(ctx,fiber.Map{ - "url":link, + return SuccessResp(ctx, fiber.Map{ + "url": link, }) } } @@ -94,15 +105,15 @@ func Preview(ctx *fiber.Ctx) error { } rawPath := req.Path rawPath = utils.ParsePath(rawPath) - log.Debugf("preview: %s",rawPath) + log.Debugf("preview: %s", rawPath) account, path, driver, err := ParsePath(rawPath) if err != nil { return ErrorResp(ctx, err, 500) } data, err := driver.Preview(path, account) if err != nil { - return ErrorResp(ctx,err,500) - }else { - return SuccessResp(ctx,data) + return ErrorResp(ctx, err, 500) + } else { + return SuccessResp(ctx, data) } -} \ No newline at end of file +} diff --git a/server/router.go b/server/router.go index 64ec10c7..28d10318 100644 --- a/server/router.go +++ b/server/router.go @@ -34,5 +34,9 @@ func InitApiRouter(app *fiber.App) { admin.Delete("/account", DeleteAccount) admin.Get("/drivers", GetDrivers) admin.Get("/clear_cache",ClearCache) + + admin.Get("/metas", GetMetas) + admin.Post("/meta", SaveMeta) + admin.Delete("/meta", DeleteMeta) } }