From 863601439709fdd78b10893644a36cbeeb10e01f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=AE=E5=87=89?= <36558727+Xhofe@users.noreply.github.com> Date: Mon, 8 Mar 2021 20:26:02 +0800 Subject: [PATCH] =?UTF-8?q?:construction:=20=E5=90=88=E5=B9=B6get=E4=B8=8E?= =?UTF-8?q?list?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bootstrap/model.go | 5 +++ conf/config.go | 9 +++--- server/controllers/get.go | 18 ++++++----- server/controllers/list.go | 20 ++++++------ server/controllers/path.go | 61 ++++++++++++++++++++++++++++++++++++ server/controllers/search.go | 30 ++++++++++++++++-- server/controllers/utils.go | 9 ++++++ server/models/create.go | 12 +++---- server/models/file.go | 16 +++++----- server/router.go | 5 +-- test/string_test.go | 2 +- 11 files changed, 146 insertions(+), 41 deletions(-) create mode 100644 server/controllers/path.go diff --git a/bootstrap/model.go b/bootstrap/model.go index b329811b..c4cc5c7f 100644 --- a/bootstrap/model.go +++ b/bootstrap/model.go @@ -7,6 +7,7 @@ import ( log "github.com/sirupsen/logrus" "gorm.io/driver/sqlite" "gorm.io/gorm" + "strings" ) func InitModel() bool { @@ -14,6 +15,10 @@ func InitModel() bool { switch conf.Conf.Database.Type { case "sqlite3": { + if !(strings.HasSuffix(conf.Conf.Database.DBFile, ".db") && len(conf.Conf.Database.DBFile) > 3) { + log.Errorf("db名称不正确.") + return false + } needMigrate := !utils.Exists(conf.Conf.Database.DBFile) db, err := gorm.Open(sqlite.Open(conf.Conf.Database.DBFile), &gorm.Config{}) if err != nil { diff --git a/conf/config.go b/conf/config.go index 1e81a355..c99acf23 100644 --- a/conf/config.go +++ b/conf/config.go @@ -20,10 +20,11 @@ type Config struct { } `yaml:"preview" json:"preview"` } `yaml:"info"` Server struct { - Port string `yaml:"port"` //端口 - Search bool `yaml:"search" json:"search"` //允许搜索 - Static string `yaml:"static"` - SiteUrl string `yaml:"site_url" json:"site_url"` //网站url + Port string `yaml:"port"` //端口 + Search bool `yaml:"search"` //允许搜索 + Static string `yaml:"static"` + SiteUrl string `yaml:"site_url"` //网站url + Password string `yaml:"password"` } `yaml:"server"` AliDrive struct { ApiUrl string `yaml:"api_url"` //阿里云盘api diff --git a/server/controllers/get.go b/server/controllers/get.go index 8df36fc5..67a9198c 100644 --- a/server/controllers/get.go +++ b/server/controllers/get.go @@ -10,7 +10,7 @@ import ( // get request bean type GetReq struct { - File string `json:"file" binding:"required"` + Path string `json:"path" binding:"required"` Password string `json:"password"` } @@ -22,11 +22,11 @@ func Get(c *gin.Context) { return } log.Debugf("list:%+v", get) - dir, name := filepath.Split(get.File) - file, err := models.GetFileByParentPathAndName(dir, name) + dir, name := filepath.Split(get.Path) + file, err := models.GetFileByDirAndName(dir, name) if err != nil { if file == nil { - c.JSON(200, MetaResponse(404, "File not found.")) + c.JSON(200, MetaResponse(404, "Path not found.")) return } c.JSON(200, MetaResponse(500, err.Error())) @@ -49,7 +49,7 @@ type DownReq struct { // handle download request func Down(c *gin.Context) { - filePath := c.Param("file") + filePath := c.Param("path")[1:] var down DownReq if err := c.ShouldBindQuery(&down); err != nil { c.JSON(200, MetaResponse(400, "Bad Request.")) @@ -57,10 +57,10 @@ func Down(c *gin.Context) { } log.Debugf("down:%s", filePath) dir, name := filepath.Split(filePath) - fileModel, err := models.GetFileByParentPathAndName(dir, name) + fileModel, err := models.GetFileByDirAndName(dir, name) if err != nil { if fileModel == nil { - c.JSON(200, MetaResponse(404, "File not found.")) + c.JSON(200, MetaResponse(404, "Path not found.")) return } c.JSON(200, MetaResponse(500, err.Error())) @@ -74,6 +74,10 @@ func Down(c *gin.Context) { } return } + if fileModel.Type == "folder" { + c.JSON(200, MetaResponse(406, "无法下载目录.")) + return + } file, err := alidrive.GetDownLoadUrl(fileModel.FileId) if err != nil { c.JSON(200, MetaResponse(500, err.Error())) diff --git a/server/controllers/list.go b/server/controllers/list.go index 0415683f..08ac97e8 100644 --- a/server/controllers/list.go +++ b/server/controllers/list.go @@ -9,7 +9,7 @@ import ( // list request bean type ListReq struct { - Path string `json:"path" binding:"required"` + Dir string `json:"dir" binding:"required"` Password string `json:"password"` } @@ -22,11 +22,11 @@ func List(c *gin.Context) { } log.Debugf("list:%+v", list) // find folder model - dir, file := filepath.Split(list.Path) - fileModel, err := models.GetFileByParentPathAndName(dir, file) + dir, name := filepath.Split(list.Dir) + file, err := models.GetFileByDirAndName(dir, name) if err != nil { // folder model not exist - if fileModel == nil { + if file == nil { c.JSON(200, MetaResponse(404, "folder not found.")) return } @@ -34,7 +34,7 @@ func List(c *gin.Context) { return } // check password - if fileModel.Password != "" && fileModel.Password != list.Password { + if file.Password != "" && file.Password != list.Password { if list.Password == "" { c.JSON(200, MetaResponse(401, "need password.")) } else { @@ -42,14 +42,14 @@ func List(c *gin.Context) { } return } - files, err := models.GetFilesByParentPath(list.Path + "/") - // delete password - for i, _ := range *files { - (*files)[i].Password = "" - } + files, err := models.GetFilesByDir(list.Dir + "/") if err != nil { c.JSON(200, MetaResponse(500, err.Error())) return } + // delete password + for i, _ := range *files { + (*files)[i].Password = "" + } c.JSON(200, DataResponse(files)) } diff --git a/server/controllers/path.go b/server/controllers/path.go new file mode 100644 index 00000000..fe8d4d65 --- /dev/null +++ b/server/controllers/path.go @@ -0,0 +1,61 @@ +package controllers + +import ( + "github.com/Xhofe/alist/server/models" + "github.com/gin-gonic/gin" + log "github.com/sirupsen/logrus" + "path/filepath" +) + +// path request bean +type PathReq struct { + Path string `json:"path" binding:"required"` + Password string `json:"password"` +} + +// handle path request +func Path(c *gin.Context) { + var req PathReq + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(200, MetaResponse(400, "Bad Request:"+err.Error())) + return + } + log.Debugf("path:%+v", req) + // find model + dir, name := filepath.Split(req.Path) + file, err := models.GetFileByDirAndName(dir, name) + if err != nil { + // folder model not exist + if file == nil { + c.JSON(200, MetaResponse(404, "path not found.")) + return + } + c.JSON(200, MetaResponse(500, err.Error())) + return + } + // check password + if file.Password != "" && file.Password != req.Password { + if req.Password == "" { + c.JSON(200, MetaResponse(401, "need password.")) + } else { + c.JSON(200, MetaResponse(401, "wrong password.")) + } + return + } + // file + if file.Type == "file" { + c.JSON(200, DataResponse(file)) + return + } + // folder + files, err := models.GetFilesByDir(req.Path + "/") + if err != nil { + c.JSON(200, MetaResponse(500, err.Error())) + return + } + // delete password + for i, _ := range *files { + (*files)[i].Password = "" + } + c.JSON(200, DataResponse(files)) +} diff --git a/server/controllers/search.go b/server/controllers/search.go index c06aab70..f0bfdba9 100644 --- a/server/controllers/search.go +++ b/server/controllers/search.go @@ -1,11 +1,35 @@ package controllers -import "github.com/gin-gonic/gin" +import ( + "github.com/Xhofe/alist/server/models" + "github.com/gin-gonic/gin" + log "github.com/sirupsen/logrus" +) + +type SearchReq struct { + Keyword string `json:"keyword" binding:"required"` + Dir string `json:"dir" binding:"required"` +} func LocalSearch(c *gin.Context) { - + var req SearchReq + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(200, MetaResponse(400, "Bad Request:"+err.Error())) + return + } + log.Debugf("list:%+v", req) + files, err := models.SearchByNameInDir(req.Keyword, req.Dir) + if err != nil { + if files == nil { + c.JSON(200, MetaResponse(404, "Path not found.")) + return + } + c.JSON(200, MetaResponse(500, err.Error())) + return + } + c.JSON(200, DataResponse(files)) } func GlobalSearch(c *gin.Context) { -} \ No newline at end of file +} diff --git a/server/controllers/utils.go b/server/controllers/utils.go index 770177cc..c219b240 100644 --- a/server/controllers/utils.go +++ b/server/controllers/utils.go @@ -13,6 +13,15 @@ func Info(c *gin.Context) { // rebuild tree func RebuildTree(c *gin.Context) { + password := c.Param("password")[1:] + if password != conf.Conf.Server.Password { + if password == "" { + c.JSON(200, MetaResponse(401, "need password.")) + return + } + c.JSON(200, MetaResponse(401, "wrong password.")) + return + } if err := models.Clear(); err != nil { c.JSON(200, MetaResponse(500, err.Error())) return diff --git a/server/models/create.go b/server/models/create.go index fd8fb24d..b148b2f0 100644 --- a/server/models/create.go +++ b/server/models/create.go @@ -22,16 +22,16 @@ func BuildTree() error { return err } rootFile := File{ - ParentPath: "/", - FileId: conf.Conf.AliDrive.RootFolder, - Name: "root", - Type: "folder", + Dir: "", + 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 { + if err := BuildOne(conf.Conf.AliDrive.RootFolder, "root/", tx, ""); err != nil { tx.Rollback() return err } @@ -55,7 +55,7 @@ func BuildOne(parent string, path string, tx *gorm.DB, parentPassword string) er password = file.Name[index+10:] } newFile := File{ - ParentPath: path, + Dir: path, FileExtension: file.FileExtension, FileId: file.FileId, Name: name, diff --git a/server/models/file.go b/server/models/file.go index fef7f5c3..dd166933 100644 --- a/server/models/file.go +++ b/server/models/file.go @@ -7,7 +7,7 @@ import ( ) type File struct { - ParentPath string `json:"parent_path" gorm:"index"` + Dir string `json:"dir" gorm:"index"` FileExtension string `json:"file_extension"` FileId string `json:"file_id"` Name string `json:"name" gorm:"index"` @@ -27,17 +27,17 @@ func Clear() error { return conf.DB.Where("1 = 1").Delete(&File{}).Error } -func GetFileByParentPathAndName(parentPath, name string) (*File, error) { +func GetFileByDirAndName(dir, name string) (*File, error) { var file File - if err := conf.DB.Where("parent_path = ? AND name = ?", parentPath, name).First(&file).Error; err != nil { + if err := conf.DB.Where("dir = ? AND name = ?", dir, name).First(&file).Error; err != nil { return nil, err } return &file, nil } -func GetFilesByParentPath(parentPath string) (*[]File, error) { +func GetFilesByDir(dir string) (*[]File, error) { var files []File - if err := conf.DB.Where("parent_path = ?", parentPath).Find(&files).Error; err != nil { + if err := conf.DB.Where("dir = ?", dir).Find(&files).Error; err != nil { return nil, err } return &files, nil @@ -45,15 +45,15 @@ func GetFilesByParentPath(parentPath string) (*[]File, error) { func SearchByNameGlobal(keyword string) (*[]File, error) { var files []File - if err := conf.DB.Where("name LIKE ?", fmt.Sprintf("%%%s%%", keyword)).Find(&files).Error; err != nil { + if err := conf.DB.Where("name LIKE ? AND password = ''", fmt.Sprintf("%%%s%%", keyword)).Find(&files).Error; err != nil { return nil, err } return &files, nil } -func SearchByNameInPath(keyword string, parentPath string) (*[]File, error) { +func SearchByNameInDir(keyword string, dir string) (*[]File, error) { var files []File - if err := conf.DB.Where("parent_path LIKE ? AND name LIKE ?", fmt.Sprintf("%s%%", parentPath), fmt.Sprintf("%%%s%%", keyword)).Find(&files).Error; err != nil { + if err := conf.DB.Where("dir LIKE ? AND name LIKE ? AND password = ''", fmt.Sprintf("%s%%", dir), fmt.Sprintf("%%%s%%", keyword)).Find(&files).Error; err != nil { return nil, err } return &files, nil diff --git a/server/router.go b/server/router.go index 85ee1532..f1e37e6a 100644 --- a/server/router.go +++ b/server/router.go @@ -26,10 +26,11 @@ func InitApiRouter(engine *gin.Engine) { apiV2.GET("/info", controllers.Info) apiV2.POST("/list", controllers.List) apiV2.POST("/get", controllers.Get) + apiV2.POST("/path", controllers.Path) apiV2.POST("/office_preview", controllers.OfficePreview) apiV2.POST("/local_search", controllers.LocalSearch) apiV2.POST("/global_search", controllers.GlobalSearch) } - engine.GET("/d/*file", controllers.Down) - engine.GET("/rebuild", controllers.RebuildTree) + engine.GET("/d/*path", controllers.Down) + engine.GET("/rebuild/*password", controllers.RebuildTree) } diff --git a/test/string_test.go b/test/string_test.go index 36462689..e28a0842 100644 --- a/test/string_test.go +++ b/test/string_test.go @@ -22,6 +22,6 @@ func TestPassword(t *testing.T) { } func TestDir(t *testing.T) { - dir,file:=filepath.Split("/root/") + dir,file:=filepath.Split("root") fmt.Printf("dir:%s\nfile:%s\n",dir,file) } \ No newline at end of file