🎇 多盘支持

This commit is contained in:
微凉
2021-03-16 21:25:16 +08:00
parent 8e9ddcf81e
commit 4d0d892ce7
19 changed files with 177 additions and 173 deletions

View File

@@ -7,6 +7,7 @@ import (
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"path/filepath"
"strings"
)
type DownReq struct {
@@ -44,7 +45,12 @@ func Down(c *gin.Context) {
c.JSON(200, MetaResponse(406, "无法下载目录."))
return
}
file, err := alidrive.GetDownLoadUrl(fileModel.FileId)
drive := utils.GetDriveByName(strings.Split(dir,"/")[0])
if drive == nil {
c.JSON(200, MetaResponse(500, "找不到drive."))
return
}
file, err := alidrive.GetDownLoadUrl(fileModel.FileId, drive)
if err != nil {
c.JSON(200, MetaResponse(500, err.Error()))
return

View File

@@ -3,9 +3,11 @@ package controllers
import (
"github.com/Xhofe/alist/alidrive"
"github.com/Xhofe/alist/server/models"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"path/filepath"
"strings"
)
// get request bean
@@ -40,7 +42,12 @@ func Get(c *gin.Context) {
}
return
}
down, err := alidrive.GetDownLoadUrl(file.FileId)
drive := utils.GetDriveByName(strings.Split(dir,"/")[0])
if drive == nil {
c.JSON(200, MetaResponse(500, "找不到drive."))
return
}
down, err := alidrive.GetDownLoadUrl(file.FileId, drive)
if err != nil {
c.JSON(200, MetaResponse(500, err.Error()))
return

View File

@@ -2,6 +2,7 @@ package controllers
import (
"github.com/Xhofe/alist/alidrive"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
@@ -12,13 +13,18 @@ type OfficePreviewReq struct {
// handle office_preview request
func OfficePreview(c *gin.Context) {
drive := utils.GetDriveByName(c.Param("drive"))
if drive == nil {
c.JSON(200, MetaResponse(400, "drive isn't exist."))
return
}
var req OfficePreviewReq
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(200, MetaResponse(400, "Bad Request:"+err.Error()))
return
}
log.Debugf("preview_req:%+v", req)
preview, err := alidrive.GetOfficePreviewUrl(req.FileId)
preview, err := alidrive.GetOfficePreviewUrl(req.FileId, drive)
if err != nil {
c.JSON(200, MetaResponse(500, err.Error()))
return

View File

@@ -55,7 +55,11 @@ func Path(c *gin.Context) {
}
// delete password
for i, _ := range *files {
(*files)[i].Password = ""
if (*files)[i].Password == "" {
(*files)[i].Password = "n"
}else {
(*files)[i].Password = "y"
}
}
c.JSON(200, DataResponse(files))
}

View File

@@ -3,6 +3,7 @@ package controllers
import (
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/server/models"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
)
@@ -13,7 +14,12 @@ func Info(c *gin.Context) {
// rebuild tree
func RebuildTree(c *gin.Context) {
password := c.Param("password")[1:]
drive := utils.GetDriveByName(c.Param("drive"))
if drive == nil {
c.JSON(200, MetaResponse(400, "drive isn't exist."))
return
}
password := c.Param("password")
if password != conf.Conf.Server.Password {
if password == "" {
c.JSON(200, MetaResponse(401, "need password."))
@@ -22,11 +28,11 @@ func RebuildTree(c *gin.Context) {
c.JSON(200, MetaResponse(401, "wrong password."))
return
}
if err := models.Clear(); err != nil {
if err := models.Clear(drive); err != nil {
c.JSON(200, MetaResponse(500, err.Error()))
return
}
if err := models.BuildTree(); err != nil {
if err := models.BuildTree(drive); err != nil {
c.JSON(200, MetaResponse(500, err.Error()))
return
}

View File

@@ -9,8 +9,18 @@ import (
"strings"
)
func BuildTreeAll() {
for i, _ := range conf.Conf.AliDrive.Drives {
if err := BuildTree(&conf.Conf.AliDrive.Drives[i]); err != nil {
log.Errorf("盘[%s]构建目录树失败:%s", err.Error())
}else {
log.Infof("盘[%s]构建目录树成功")
}
}
}
// build tree
func BuildTree() error {
func BuildTree(drive *conf.Drive) error {
log.Infof("开始构建目录树...")
tx := conf.DB.Begin()
defer func() {
@@ -22,24 +32,25 @@ func BuildTree() error {
return err
}
rootFile := File{
Dir: "",
FileId: conf.Conf.AliDrive.RootFolder,
Name: "root",
Type: "folder",
Dir: "",
FileId: drive.RootFolder,
Name: drive.Name,
Type: "folder",
Password: drive.Password,
}
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(drive.RootFolder, drive.Name+"/", tx, drive.Password, drive); err != nil {
tx.Rollback()
return err
}
return tx.Commit().Error
}
func BuildOne(parent string, path string, tx *gorm.DB, parentPassword string) error {
files, err := alidrive.GetList(parent, conf.Conf.AliDrive.MaxFilesCount, "", "", "")
func BuildOne(parent string, path string, tx *gorm.DB, parentPassword string, drive *conf.Drive) error {
files, err := alidrive.GetList(parent, conf.Conf.AliDrive.MaxFilesCount, "", "", "", drive)
if err != nil {
return err
}
@@ -71,7 +82,7 @@ func BuildOne(parent string, path string, tx *gorm.DB, parentPassword string) er
return err
}
if file.Type == "folder" {
if err := BuildOne(file.FileId, fmt.Sprintf("%s%s/", path, name), tx, password); err != nil {
if err := BuildOne(file.FileId, fmt.Sprintf("%s%s/", path, name), tx, password, drive); err != nil {
return err
}
}

View File

@@ -24,8 +24,8 @@ func (file *File) Create() error {
return conf.DB.Create(file).Error
}
func Clear() error {
return conf.DB.Where("1 = 1").Delete(&File{}).Error
func Clear(drive *conf.Drive) error {
return conf.DB.Where("dir like ?", fmt.Sprintf("%s%%", drive.Name)).Delete(&File{}).Error
}
func GetFileByDirAndName(dir, name string) (*File, error) {

View File

@@ -26,10 +26,10 @@ func InitApiRouter(engine *gin.Engine) {
apiV2.GET("/info", controllers.Info)
apiV2.POST("/get", controllers.Get)
apiV2.POST("/path", controllers.Path)
apiV2.POST("/office_preview", controllers.OfficePreview)
apiV2.POST("/office_preview/:drive", controllers.OfficePreview)
apiV2.POST("/local_search", controllers.LocalSearch)
apiV2.POST("/global_search", controllers.GlobalSearch)
apiV2.GET("/rebuild/*password", controllers.RebuildTree)
apiV2.GET("/rebuild/:drive/:password", controllers.RebuildTree)
}
engine.GET("/d/*path", controllers.Down)
}