➖ 去除缓存
This commit is contained in:
84
server/controllers/get.go
Normal file
84
server/controllers/get.go
Normal file
@@ -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
|
||||
}
|
51
server/controllers/list.go
Normal file
51
server/controllers/list.go
Normal file
@@ -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))
|
||||
}
|
@@ -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))
|
||||
}
|
1
server/controllers/search.go
Normal file
1
server/controllers/search.go
Normal file
@@ -0,0 +1 @@
|
||||
package controllers
|
@@ -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 {
|
||||
|
@@ -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
|
||||
}
|
@@ -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))
|
||||
}
|
@@ -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))
|
||||
}
|
@@ -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
|
||||
}
|
@@ -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))
|
||||
}
|
@@ -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,
|
||||
|
@@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user