🚧 合并get与list

This commit is contained in:
微凉
2021-03-08 20:26:02 +08:00
parent c0f50ffeff
commit 8636014397
11 changed files with 146 additions and 41 deletions

View File

@ -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 {

View File

@ -21,9 +21,10 @@ type Config struct {
} `yaml:"info"`
Server struct {
Port string `yaml:"port"` //端口
Search bool `yaml:"search" json:"search"` //允许搜索
Search bool `yaml:"search"` //允许搜索
Static string `yaml:"static"`
SiteUrl string `yaml:"site_url" json:"site_url"` //网站url
SiteUrl string `yaml:"site_url"` //网站url
Password string `yaml:"password"`
} `yaml:"server"`
AliDrive struct {
ApiUrl string `yaml:"api_url"` //阿里云盘api

View File

@ -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()))

View File

@ -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))
}

View File

@ -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))
}

View File

@ -1,9 +1,33 @@
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) {

View File

@ -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

View File

@ -22,7 +22,7 @@ func BuildTree() error {
return err
}
rootFile := File{
ParentPath: "/",
Dir: "",
FileId: conf.Conf.AliDrive.RootFolder,
Name: "root",
Type: "folder",
@ -31,7 +31,7 @@ func BuildTree() error {
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,

View File

@ -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

View File

@ -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)
}

View File

@ -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)
}