diff --git a/bootstrap/cache.go b/bootstrap/cache.go deleted file mode 100644 index 5f1b4bed..00000000 --- a/bootstrap/cache.go +++ /dev/null @@ -1,16 +0,0 @@ -package bootstrap - -import ( - "github.com/Xhofe/alist/conf" - "github.com/patrickmn/go-cache" - log "github.com/sirupsen/logrus" - "time" -) - -// init cache -func InitCache() { - if conf.Conf.Cache.Enable { - log.Infof("初始化缓存...") - conf.Cache = cache.New(time.Duration(conf.Conf.Cache.Expiration)*time.Minute, time.Duration(conf.Conf.Cache.CleanupInterval)*time.Minute) - } -} diff --git a/bootstrap/cmd.go b/bootstrap/cmd.go index 4f719ece..25111d19 100644 --- a/bootstrap/cmd.go +++ b/bootstrap/cmd.go @@ -65,7 +65,6 @@ func start() { log.Errorf("初始化数据库出现错误,启动失败.") return } - InitCache() InitCron() server() } diff --git a/conf/config.go b/conf/config.go index d86c5ced..1e81a355 100644 --- a/conf/config.go +++ b/conf/config.go @@ -25,12 +25,6 @@ type Config struct { Static string `yaml:"static"` SiteUrl string `yaml:"site_url" json:"site_url"` //网站url } `yaml:"server"` - Cache struct { - Enable bool `yaml:"enable"` - Expiration int `yaml:"expiration"` - CleanupInterval int `yaml:"cleanup_interval"` - RefreshPassword string `yaml:"refresh_password"` - } AliDrive struct { ApiUrl string `yaml:"api_url"` //阿里云盘api RootFolder string `yaml:"root_folder"` //根目录id diff --git a/conf/const.go b/conf/const.go index 019d893e..f5d37a34 100644 --- a/conf/const.go +++ b/conf/const.go @@ -1,7 +1,6 @@ package conf import ( - "github.com/patrickmn/go-cache" "gorm.io/gorm" "net/http" ) @@ -16,8 +15,6 @@ var ( Client *http.Client // request client Authorization string // authorization string - Cache *cache.Cache // cache - DB *gorm.DB Origins []string // allow origins diff --git a/go.mod b/go.mod index 1865081c..b0c8af31 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,6 @@ require ( github.com/mattn/go-sqlite3 v1.14.6 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.1 // indirect - github.com/patrickmn/go-cache v2.1.0+incompatible github.com/robfig/cron/v3 v3.0.0 github.com/sirupsen/logrus v1.7.0 github.com/ugorji/go v1.2.2 // indirect diff --git a/go.sum b/go.sum index b26bbd12..bb6fb587 100644 --- a/go.sum +++ b/go.sum @@ -77,8 +77,6 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLD github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= -github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= -github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= diff --git a/server/controllers/get.go b/server/controllers/get.go new file mode 100644 index 00000000..870ce413 --- /dev/null +++ b/server/controllers/get.go @@ -0,0 +1,84 @@ +package controllers + +import ( + "github.com/Xhofe/alist/alidrive" + "github.com/Xhofe/alist/server/models" + "github.com/gin-gonic/gin" + log "github.com/sirupsen/logrus" + "path/filepath" +) + +// get request bean +type GetReq struct { + File string `json:"file"` + Password string `json:"password"` +} + +// handle list request +func Get(c *gin.Context) { + var get GetReq + if err := c.ShouldBindJSON(&get); err != nil { + c.JSON(200, MetaResponse(400, "Bad Request.")) + return + } + log.Debugf("list:%+v", get) + dir, name := filepath.Split(get.File) + file, err := models.GetFileByParentPathAndName(dir, name) + if err != nil { + if file == nil { + c.JSON(200, MetaResponse(404, "File not found.")) + return + } + c.JSON(200, MetaResponse(500, err.Error())) + return + } + if file.Password != "" && file.Password != get.Password { + if get.Password == "" { + c.JSON(200, MetaResponse(401, "need password.")) + } else { + c.JSON(200, MetaResponse(401, "wrong password.")) + } + return + } + c.JSON(200, DataResponse(file)) +} + +type DownReq struct { + Password string `form:"pw"` +} + +// handle download request +func Down(c *gin.Context) { + filePath := c.Param("file") + var down DownReq + if err := c.ShouldBindQuery(&down); err != nil { + c.JSON(200, MetaResponse(400, "Bad Request.")) + return + } + log.Debugf("down:%s", filePath) + dir, name := filepath.Split(filePath) + fileModel, err := models.GetFileByParentPathAndName(dir, name) + if err != nil { + if fileModel == nil { + c.JSON(200, MetaResponse(404, "File not found.")) + return + } + c.JSON(200, MetaResponse(500, err.Error())) + return + } + if fileModel.Password != "" && fileModel.Password != down.Password { + if down.Password == "" { + c.JSON(200, MetaResponse(401, "need password.")) + } else { + c.JSON(200, MetaResponse(401, "wrong password.")) + } + return + } + file, err := alidrive.GetDownLoadUrl(fileModel.FileId) + if err != nil { + c.JSON(200, MetaResponse(500, err.Error())) + return + } + c.Redirect(301, file.Url) + return +} diff --git a/server/controllers/list.go b/server/controllers/list.go new file mode 100644 index 00000000..23d46719 --- /dev/null +++ b/server/controllers/list.go @@ -0,0 +1,51 @@ +package controllers + +import ( + "github.com/Xhofe/alist/server/models" + "github.com/gin-gonic/gin" + log "github.com/sirupsen/logrus" + "path/filepath" +) + +// list request bean +type ListReq struct { + Path string `json:"path"` + Password string `json:"password"` +} + +// handle list request +func List(c *gin.Context) { + var list ListReq + if err := c.ShouldBindJSON(&list); err != nil { + c.JSON(200, MetaResponse(400, "Bad Request.")) + return + } + log.Debugf("list:%+v", list) + // find folder model + dir, file := filepath.Split(list.Path) + fileModel, err := models.GetFileByParentPathAndName(dir, file) + if err != nil { + // folder model not exist + if fileModel == nil { + c.JSON(200, MetaResponse(404, "folder not found.")) + return + } + c.JSON(200, MetaResponse(500, err.Error())) + return + } + // check password + if fileModel.Password != "" && fileModel.Password != list.Password { + if list.Password == "" { + c.JSON(200, MetaResponse(401, "need password.")) + } else { + c.JSON(200, MetaResponse(401, "wrong password.")) + } + return + } + files, err := models.GetFilesByParentPath(list.Path + "/") + if err != nil { + c.JSON(200, MetaResponse(500, err.Error())) + return + } + c.JSON(200, DataResponse(files)) +} diff --git a/server/controllers/v1/offie_preview.go b/server/controllers/offie_preview.go similarity index 64% rename from server/controllers/v1/offie_preview.go rename to server/controllers/offie_preview.go index ac62bf56..e8ba2fd0 100644 --- a/server/controllers/v1/offie_preview.go +++ b/server/controllers/offie_preview.go @@ -1,8 +1,7 @@ -package v1 +package controllers import ( "github.com/Xhofe/alist/alidrive" - "github.com/Xhofe/alist/server/controllers" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" ) @@ -11,14 +10,14 @@ import ( func OfficePreview(c *gin.Context) { var req alidrive.OfficePreviewUrlReq if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(200, controllers.MetaResponse(400, "Bad Request")) + c.JSON(200, MetaResponse(400, "Bad Request")) return } log.Debugf("preview_req:%+v", req) preview, err := alidrive.GetOfficePreviewUrl(req.FileId) if err != nil { - c.JSON(200, controllers.MetaResponse(500, err.Error())) + c.JSON(200, MetaResponse(500, err.Error())) return } - c.JSON(200, controllers.DataResponse(preview)) + c.JSON(200, DataResponse(preview)) } diff --git a/server/controllers/search.go b/server/controllers/search.go new file mode 100644 index 00000000..2d329367 --- /dev/null +++ b/server/controllers/search.go @@ -0,0 +1 @@ +package controllers diff --git a/server/controllers/utils.go b/server/controllers/utils.go index a88753af..770177cc 100644 --- a/server/controllers/utils.go +++ b/server/controllers/utils.go @@ -11,22 +11,6 @@ func Info(c *gin.Context) { c.JSON(200, DataResponse(conf.Conf.Info)) } -// handle refresh_cache request -func RefreshCache(c *gin.Context) { - password := c.Param("password") - if conf.Conf.Cache.Enable { - if password == conf.Conf.Cache.RefreshPassword { - conf.Cache.Flush() - c.JSON(200, MetaResponse(200, "flush success.")) - return - } - c.JSON(200, MetaResponse(401, "wrong password.")) - return - } - c.JSON(200, MetaResponse(400, "disabled cache.")) - return -} - // rebuild tree func RebuildTree(c *gin.Context) { if err := models.Clear(); err != nil { diff --git a/server/controllers/v1/get.go b/server/controllers/v1/get.go deleted file mode 100644 index 944732c2..00000000 --- a/server/controllers/v1/get.go +++ /dev/null @@ -1,76 +0,0 @@ -package v1 - -import ( - "github.com/Xhofe/alist/alidrive" - "github.com/Xhofe/alist/server/controllers" - "github.com/gin-gonic/gin" - log "github.com/sirupsen/logrus" - "strings" -) - -// handle get request -// 因为下载地址有时效,所以去掉了文件请求和直链的缓存 -func Get(c *gin.Context) { - var get alidrive.GetReq - if err := c.ShouldBindJSON(&get); err != nil { - c.JSON(200, controllers.MetaResponse(400, "Bad Request")) - return - } - log.Debugf("get:%+v", get) - // cache - //cacheKey:=fmt.Sprintf("%s-%s","g",get.FileId) - //if conf.Conf.Cache.Enable { - // file,exist:=conf.Cache.Get(cacheKey) - // if exist { - // log.Debugf("使用了缓存:%s",cacheKey) - // c.JSON(200,DataResponse(file)) - // return - // } - //} - file, err := alidrive.GetFile(get.FileId) - if err != nil { - c.JSON(200, controllers.MetaResponse(500, err.Error())) - return - } - paths, err := alidrive.GetPaths(get.FileId) - if err != nil { - c.JSON(200, controllers.MetaResponse(500, err.Error())) - return - } - file.Paths = *paths - download, err := alidrive.GetDownLoadUrl(get.FileId) - if err != nil { - c.JSON(200, controllers.MetaResponse(500, err.Error())) - return - } - file.DownloadUrl = download.Url - //if conf.Conf.Cache.Enable { - // conf.Cache.Set(cacheKey,file,cache.DefaultExpiration) - //} - c.JSON(200, controllers.DataResponse(file)) -} - -func Down(c *gin.Context) { - fileIdParam := c.Param("file_id") - log.Debugf("down:%s", fileIdParam) - fileId := strings.Split(fileIdParam, "/")[1] - //cacheKey:=fmt.Sprintf("%s-%s","d",fileId) - //if conf.Conf.Cache.Enable { - // downloadUrl,exist:=conf.Cache.Get(cacheKey) - // if exist { - // log.Debugf("使用了缓存:%s",cacheKey) - // c.Redirect(301,downloadUrl.(string)) - // return - // } - //} - file, err := alidrive.GetDownLoadUrl(fileId) - if err != nil { - c.JSON(200, controllers.MetaResponse(500, err.Error())) - return - } - //if conf.Conf.Cache.Enable { - // conf.Cache.Set(cacheKey,file.DownloadUrl,cache.DefaultExpiration) - //} - c.Redirect(301, file.Url) - return -} diff --git a/server/controllers/v1/list.go b/server/controllers/v1/list.go deleted file mode 100644 index 6ed72666..00000000 --- a/server/controllers/v1/list.go +++ /dev/null @@ -1,76 +0,0 @@ -package v1 - -import ( - "fmt" - "github.com/Xhofe/alist/alidrive" - "github.com/Xhofe/alist/conf" - "github.com/Xhofe/alist/server/controllers" - "github.com/gin-gonic/gin" - "github.com/patrickmn/go-cache" - log "github.com/sirupsen/logrus" -) - -// list request bean -type ListReq struct { - Password string `json:"password"` - alidrive.ListReq -} - -// handle list request -func List(c *gin.Context) { - var list ListReq - if err := c.ShouldBindJSON(&list); err != nil { - c.JSON(200, controllers.MetaResponse(400, "Bad Request")) - return - } - log.Debugf("list:%+v", list) - // cache - cacheKey := fmt.Sprintf("%s-%s-%s", "l", list.ParentFileId, list.Password) - if conf.Conf.Cache.Enable { - files, exist := conf.Cache.Get(cacheKey) - if exist { - log.Debugf("使用了缓存:%s", cacheKey) - c.JSON(200, controllers.DataResponse(files)) - return - } - } - var ( - files *alidrive.Files - err error - ) - if list.Limit == 0 { - list.Limit = 50 - } - if conf.Conf.AliDrive.MaxFilesCount != 0 { - list.Limit = conf.Conf.AliDrive.MaxFilesCount - } - if list.ParentFileId == "root" { - files, err = alidrive.GetRoot(list.Limit, list.Marker, list.OrderBy, list.OrderDirection) - } else { - files, err = alidrive.GetList(list.ParentFileId, list.Limit, list.Marker, list.OrderBy, list.OrderDirection) - } - if err != nil { - c.JSON(200, controllers.MetaResponse(500, err.Error())) - return - } - password := alidrive.HasPassword(files) - if password != "" && password != list.Password { - if list.Password == "" { - c.JSON(200, controllers.MetaResponse(401, "need password.")) - return - } - c.JSON(200, controllers.MetaResponse(401, "wrong password.")) - return - } - paths, err := alidrive.GetPaths(list.ParentFileId) - if err != nil { - c.JSON(200, controllers.MetaResponse(500, err.Error())) - return - } - files.Paths = *paths - //files.Readme=alidrive.HasReadme(files) - if conf.Conf.Cache.Enable { - conf.Cache.Set(cacheKey, files, cache.DefaultExpiration) - } - c.JSON(200, controllers.DataResponse(files)) -} diff --git a/server/controllers/v1/search.go b/server/controllers/v1/search.go deleted file mode 100644 index 179a2fea..00000000 --- a/server/controllers/v1/search.go +++ /dev/null @@ -1,51 +0,0 @@ -package v1 - -import ( - "fmt" - "github.com/Xhofe/alist/alidrive" - "github.com/Xhofe/alist/conf" - "github.com/Xhofe/alist/server/controllers" - "github.com/gin-gonic/gin" - "github.com/patrickmn/go-cache" - log "github.com/sirupsen/logrus" -) - -// handle search request -func Search(c *gin.Context) { - if !conf.Conf.Server.Search { - c.JSON(200, controllers.MetaResponse(403, "Not allow search.")) - return - } - var search alidrive.SearchReq - if err := c.ShouldBindJSON(&search); err != nil { - c.JSON(200, controllers.MetaResponse(400, "Bad Request")) - return - } - log.Debugf("search:%+v", search) - // cache - cacheKey := fmt.Sprintf("%s-%s", "s", search.Query) - if conf.Conf.Cache.Enable { - files, exist := conf.Cache.Get(cacheKey) - if exist { - log.Debugf("使用了缓存:%s", cacheKey) - c.JSON(200, controllers.DataResponse(files)) - return - } - } - if search.Limit == 0 { - search.Limit = 50 - } - // Search只支持0-100 - //if conf.Conf.AliDrive.MaxFilesCount!=0 { - // search.Limit=conf.Conf.AliDrive.MaxFilesCount - //} - files, err := alidrive.Search(search.Query, search.Limit, search.OrderBy) - if err != nil { - c.JSON(200, controllers.MetaResponse(500, err.Error())) - return - } - if conf.Conf.Cache.Enable { - conf.Cache.Set(cacheKey, files, cache.DefaultExpiration) - } - c.JSON(200, controllers.DataResponse(files)) -} diff --git a/server/controllers/v2/get.go b/server/controllers/v2/get.go deleted file mode 100644 index bb469dab..00000000 --- a/server/controllers/v2/get.go +++ /dev/null @@ -1,52 +0,0 @@ -package v2 - -import ( - "github.com/Xhofe/alist/alidrive" - "github.com/Xhofe/alist/server/controllers" - "github.com/Xhofe/alist/server/models" - "github.com/gin-gonic/gin" - log "github.com/sirupsen/logrus" - "path/filepath" -) - -// get request bean -type GetReq struct { - File string `json:"file"` - Password string `json:"password"` -} - -// handle list request -func Get(c *gin.Context) { - var get GetReq - if err := c.ShouldBindJSON(&get); err != nil { - c.JSON(200, controllers.MetaResponse(400, "Bad Request.")) - return - } - log.Debugf("list:%+v", get) - dir, name := filepath.Split(get.File) - file, err := models.GetFileByParentPathAndName(dir, name) - if err != nil { - c.JSON(200, controllers.MetaResponse(500, err.Error())) - return - } - c.JSON(200, controllers.DataResponse(file)) -} - -// handle download request -func Down(c *gin.Context) { - filePath := c.Param("file") - log.Debugf("down:%s", filePath) - dir, name := filepath.Split(filePath) - fileModel, err := models.GetFileByParentPathAndName(dir, name) - if err != nil { - c.JSON(200, controllers.MetaResponse(500, err.Error())) - return - } - file, err := alidrive.GetDownLoadUrl(fileModel.FileId) - if err != nil { - c.JSON(200, controllers.MetaResponse(500, err.Error())) - return - } - c.Redirect(301, file.Url) - return -} diff --git a/server/controllers/v2/list.go b/server/controllers/v2/list.go deleted file mode 100644 index b9f047a4..00000000 --- a/server/controllers/v2/list.go +++ /dev/null @@ -1,30 +0,0 @@ -package v2 - -import ( - "github.com/Xhofe/alist/server/controllers" - "github.com/Xhofe/alist/server/models" - "github.com/gin-gonic/gin" - log "github.com/sirupsen/logrus" -) - -// list request bean -type ListReq struct { - Path string `json:"path"` - Password string `json:"password"` -} - -// handle list request -func List(c *gin.Context) { - var list ListReq - if err := c.ShouldBindJSON(&list); err != nil { - c.JSON(200, controllers.MetaResponse(400, "Bad Request.")) - return - } - log.Debugf("list:%+v", list) - files, err := models.GetFilesByParentPath(list.Path) - if err != nil { - c.JSON(200, controllers.MetaResponse(500, err.Error())) - return - } - c.JSON(200, controllers.DataResponse(files)) -} diff --git a/server/models/create.go b/server/models/create.go index 8cc3219b..fd8fb24d 100644 --- a/server/models/create.go +++ b/server/models/create.go @@ -21,6 +21,16 @@ func BuildTree() error { if err := tx.Error; err != nil { return err } + rootFile := File{ + ParentPath: "/", + FileId: conf.Conf.AliDrive.RootFolder, + Name: "root", + Type: "folder", + } + if err := tx.Create(&rootFile).Error; err != nil { + tx.Rollback() + return err + } if err := BuildOne(conf.Conf.AliDrive.RootFolder, "/root/", tx, ""); err != nil { tx.Rollback() return err @@ -48,7 +58,7 @@ func BuildOne(parent string, path string, tx *gorm.DB, parentPassword string) er ParentPath: path, FileExtension: file.FileExtension, FileId: file.FileId, - Name: file.Name, + Name: name, Type: file.Type, UpdatedAt: file.UpdatedAt, Category: file.Category, diff --git a/server/router.go b/server/router.go index 95d6aab1..3dc38d4b 100644 --- a/server/router.go +++ b/server/router.go @@ -3,8 +3,6 @@ package server import ( "github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/server/controllers" - "github.com/Xhofe/alist/server/controllers/v1" - v2 "github.com/Xhofe/alist/server/controllers/v2" "github.com/gin-contrib/static" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" @@ -23,21 +21,13 @@ func InitRouter(engine *gin.Engine) { // init api router func InitApiRouter(engine *gin.Engine) { - apiV1 := engine.Group("/api/v1") - { - apiV1.GET("/info", controllers.Info) - apiV1.POST("/get", v1.Get) - apiV1.POST("/list", v1.List) - apiV1.POST("/search", v1.Search) - apiV1.POST("/office_preview", v1.OfficePreview) - apiV1.GET("/d/*file_id", v1.Down) - } apiV2 := engine.Group("/api") { - apiV2.POST("/list", v2.List) - apiV2.POST("/get", v2.Get) + apiV2.GET("/info", controllers.Info) + apiV2.POST("/list", controllers.List) + apiV2.POST("/get", controllers.Get) + apiV2.POST("/office_preview", controllers.OfficePreview) } - engine.GET("/d/*file", v2.Down) - engine.GET("/cache/:password", controllers.RefreshCache) + engine.GET("/d/*file", controllers.Down) engine.GET("/rebuild", controllers.RebuildTree) } diff --git a/test/string_test.go b/test/string_test.go index 6538ee15..36462689 100644 --- a/test/string_test.go +++ b/test/string_test.go @@ -2,6 +2,7 @@ package test import ( "fmt" + "path/filepath" "strings" "testing" ) @@ -18,4 +19,9 @@ func TestPassword(t *testing.T) { name:=fullName[:index] password:=fullName[index+10:] fmt.Printf("name:%s, password:%s\n",name,password) +} + +func TestDir(t *testing.T) { + dir,file:=filepath.Split("/root/") + fmt.Printf("dir:%s\nfile:%s\n",dir,file) } \ No newline at end of file