🚧 构建目录树

This commit is contained in:
微凉
2021-03-05 21:07:45 +08:00
parent 389226662c
commit d137ef8759
11 changed files with 238 additions and 53 deletions

View File

@@ -2,6 +2,7 @@ package controllers
import (
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/server/models"
"github.com/gin-gonic/gin"
)
@@ -24,4 +25,18 @@ func RefreshCache(c *gin.Context) {
}
c.JSON(200, MetaResponse(400,"disabled cache."))
return
}
// rebuild tree
func RebuildTree(c *gin.Context) {
if err:=models.Clear();err!=nil{
c.JSON(200,MetaResponse(500,err.Error()))
return
}
if err:=models.BuildTree();err!=nil {
c.JSON(200,MetaResponse(500,err.Error()))
return
}
c.JSON(200,MetaResponse(200,"success."))
return
}

View File

@@ -1,7 +1,8 @@
package controllers
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"
@@ -12,7 +13,7 @@ import (
func Get(c *gin.Context) {
var get alidrive.GetReq
if err := c.ShouldBindJSON(&get); err != nil {
c.JSON(200, MetaResponse(400,"Bad Request"))
c.JSON(200, controllers.MetaResponse(400,"Bad Request"))
return
}
log.Debugf("get:%+v",get)
@@ -28,25 +29,25 @@ func Get(c *gin.Context) {
//}
file,err:=alidrive.GetFile(get.FileId)
if err !=nil {
c.JSON(200, MetaResponse(500,err.Error()))
c.JSON(200, controllers.MetaResponse(500,err.Error()))
return
}
paths,err:=alidrive.GetPaths(get.FileId)
if err!=nil {
c.JSON(200, MetaResponse(500,err.Error()))
c.JSON(200, controllers.MetaResponse(500,err.Error()))
return
}
file.Paths=*paths
download,err:=alidrive.GetDownLoadUrl(get.FileId)
if err!=nil {
c.JSON(200, MetaResponse(500,err.Error()))
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, DataResponse(file))
c.JSON(200, controllers.DataResponse(file))
}
func Down(c *gin.Context) {
@@ -64,7 +65,7 @@ func Down(c *gin.Context) {
//}
file,err:=alidrive.GetDownLoadUrl(fileId)
if err != nil {
c.JSON(200, MetaResponse(500,err.Error()))
c.JSON(200, controllers.MetaResponse(500,err.Error()))
return
}
//if conf.Conf.Cache.Enable {

View File

@@ -1,9 +1,10 @@
package controllers
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"
@@ -19,7 +20,7 @@ type ListReq struct {
func List(c *gin.Context) {
var list ListReq
if err := c.ShouldBindJSON(&list);err!=nil {
c.JSON(200, MetaResponse(400,"Bad Request"))
c.JSON(200, controllers.MetaResponse(400,"Bad Request"))
return
}
log.Debugf("list:%+v",list)
@@ -29,7 +30,7 @@ func List(c *gin.Context) {
files,exist:=conf.Cache.Get(cacheKey)
if exist {
log.Debugf("使用了缓存:%s",cacheKey)
c.JSON(200, DataResponse(files))
c.JSON(200, controllers.DataResponse(files))
return
}
}
@@ -49,21 +50,21 @@ func List(c *gin.Context) {
files,err=alidrive.GetList(list.ParentFileId,list.Limit,list.Marker,list.OrderBy,list.OrderDirection)
}
if err!=nil {
c.JSON(200, MetaResponse(500,err.Error()))
c.JSON(200, controllers.MetaResponse(500,err.Error()))
return
}
password:=alidrive.HasPassword(files)
if password!="" && password!=list.Password {
if list.Password=="" {
c.JSON(200, MetaResponse(401,"need password."))
c.JSON(200, controllers.MetaResponse(401,"need password."))
return
}
c.JSON(200, MetaResponse(401,"wrong password."))
c.JSON(200, controllers.MetaResponse(401,"wrong password."))
return
}
paths,err:=alidrive.GetPaths(list.ParentFileId)
if err!=nil {
c.JSON(200, MetaResponse(500,err.Error()))
c.JSON(200, controllers.MetaResponse(500,err.Error()))
return
}
files.Paths=*paths
@@ -71,5 +72,5 @@ func List(c *gin.Context) {
if conf.Conf.Cache.Enable {
conf.Cache.Set(cacheKey,files,cache.DefaultExpiration)
}
c.JSON(200, DataResponse(files))
c.JSON(200, controllers.DataResponse(files))
}

View File

@@ -1,7 +1,8 @@
package controllers
package v1
import (
"github.com/Xhofe/alist/alidrive"
"github.com/Xhofe/alist/server/controllers"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
@@ -10,14 +11,14 @@ import (
func OfficePreview(c *gin.Context) {
var req alidrive.OfficePreviewUrlReq
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(200, MetaResponse(400,"Bad Request"))
c.JSON(200, controllers.MetaResponse(400,"Bad Request"))
return
}
log.Debugf("preview_req:%+v",req)
preview,err:=alidrive.GetOfficePreviewUrl(req.FileId)
if err!=nil {
c.JSON(200, MetaResponse(500,err.Error()))
c.JSON(200, controllers.MetaResponse(500,err.Error()))
return
}
c.JSON(200, DataResponse(preview))
c.JSON(200, controllers.DataResponse(preview))
}

View File

@@ -1,9 +1,10 @@
package controllers
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"
@@ -12,12 +13,12 @@ import (
// handle search request
func Search(c *gin.Context) {
if !conf.Conf.Server.Search {
c.JSON(200, MetaResponse(403,"Not allow 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, MetaResponse(400,"Bad Request"))
c.JSON(200, controllers.MetaResponse(400,"Bad Request"))
return
}
log.Debugf("search:%+v",search)
@@ -27,7 +28,7 @@ func Search(c *gin.Context) {
files,exist:=conf.Cache.Get(cacheKey)
if exist {
log.Debugf("使用了缓存:%s",cacheKey)
c.JSON(200, DataResponse(files))
c.JSON(200, controllers.DataResponse(files))
return
}
}
@@ -40,11 +41,11 @@ func Search(c *gin.Context) {
//}
files,err:=alidrive.Search(search.Query,search.Limit,search.OrderBy)
if err != nil {
c.JSON(200, MetaResponse(500,err.Error()))
c.JSON(200, controllers.MetaResponse(500,err.Error()))
return
}
if conf.Conf.Cache.Enable {
conf.Cache.Set(cacheKey,files,cache.DefaultExpiration)
}
c.JSON(200, DataResponse(files))
c.JSON(200, controllers.DataResponse(files))
}

View File

@@ -0,0 +1,52 @@
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)
path,name:=filepath.Split(get.File)
file, err := models.GetFileByParentPathAndName(path,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)
path,name:=filepath.Split(filePath)
fileModel, err := models.GetFileByParentPathAndName(path,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
}

View File

@@ -0,0 +1,30 @@
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))
}