🐎 加入缓存

This commit is contained in:
微凉
2020-12-29 13:37:58 +08:00
parent 02e665ae37
commit bf3f741b22
16 changed files with 270 additions and 138 deletions

View File

@@ -1,118 +0,0 @@
package server
import (
"github.com/Xhofe/alist/alidrive"
"github.com/Xhofe/alist/conf"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"strings"
)
func Info(c *gin.Context) {
c.JSON(200,dataResponse(conf.Conf.Info))
}
func Get(c *gin.Context) {
var get alidrive.GetReq
if err := c.ShouldBindJSON(&get); err != nil {
c.JSON(200,metaResponse(400,"Bad Request"))
return
}
log.Debugf("get:%v",get)
file,err:=alidrive.GetFile(get.FileId)
if err !=nil {
c.JSON(200,metaResponse(500,err.Error()))
return
}
paths,err:=alidrive.GetPaths(get.FileId)
if err!=nil {
c.JSON(200,metaResponse(500,err.Error()))
return
}
file.Paths=*paths
c.JSON(200,dataResponse(file))
}
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)
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,metaResponse(500,err.Error()))
return
}
password:=alidrive.HasPassword(files)
if password!="" && password!=list.Password {
if list.Password=="" {
c.JSON(200,metaResponse(401,"need password."))
return
}
c.JSON(200,metaResponse(401,"wrong password."))
return
}
paths,err:=alidrive.GetPaths(list.ParentFileId)
if err!=nil {
c.JSON(200,metaResponse(500,err.Error()))
return
}
files.Paths=*paths
files.Readme=alidrive.HasReadme(files)
c.JSON(200,dataResponse(files))
}
func Search(c *gin.Context) {
if !conf.Conf.Server.Search {
c.JSON(200,metaResponse(403,"Not allow search."))
return
}
var search alidrive.SearchReq
if err := c.ShouldBindJSON(&search); err != nil {
c.JSON(200,metaResponse(400,"Bad Request"))
return
}
log.Debugf("search:%v",search)
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,metaResponse(500,err.Error()))
return
}
c.JSON(200,dataResponse(files))
}
func Down(c *gin.Context) {
fileIdParam:=c.Param("file_id")
log.Debugf("down:%s",fileIdParam)
fileId:=strings.Split(fileIdParam,"/")[1]
file,err:=alidrive.GetFile(fileId)
if err != nil {
c.JSON(200,metaResponse(500,err.Error()))
return
}
c.Redirect(301,file.DownloadUrl)
return
}

View File

@@ -1,4 +1,4 @@
package server
package controllers
import "github.com/gin-gonic/gin"
@@ -19,4 +19,4 @@ func dataResponse(data interface{}) gin.H {
},
"data":data,
}
}
}

70
server/controllers/get.go Normal file
View File

@@ -0,0 +1,70 @@
package controllers
import (
"fmt"
"github.com/Xhofe/alist/alidrive"
"github.com/Xhofe/alist/conf"
"github.com/gin-gonic/gin"
"github.com/patrickmn/go-cache"
log "github.com/sirupsen/logrus"
"strings"
)
func Get(c *gin.Context) {
var get alidrive.GetReq
if err := c.ShouldBindJSON(&get); err != nil {
c.JSON(200,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,metaResponse(500,err.Error()))
return
}
paths,err:=alidrive.GetPaths(get.FileId)
if err!=nil {
c.JSON(200,metaResponse(500,err.Error()))
return
}
file.Paths=*paths
if conf.Conf.Cache.Enable {
conf.Cache.Set(cacheKey,file,cache.DefaultExpiration)
}
c.JSON(200,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.GetFile(fileId)
if err != nil {
c.JSON(200, metaResponse(500,err.Error()))
return
}
if conf.Conf.Cache.Enable {
conf.Cache.Set(cacheKey,file.DownloadUrl,cache.DefaultExpiration)
}
c.Redirect(301,file.DownloadUrl)
return
}

View File

@@ -0,0 +1,68 @@
package controllers
import (
"fmt"
"github.com/Xhofe/alist/alidrive"
"github.com/Xhofe/alist/conf"
"github.com/gin-gonic/gin"
"github.com/patrickmn/go-cache"
log "github.com/sirupsen/logrus"
)
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)
// 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, 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, metaResponse(500,err.Error()))
return
}
password:=alidrive.HasPassword(files)
if password!="" && password!=list.Password {
if list.Password=="" {
c.JSON(200, metaResponse(401,"need password."))
return
}
c.JSON(200, metaResponse(401,"wrong password."))
return
}
paths,err:=alidrive.GetPaths(list.ParentFileId)
if err!=nil {
c.JSON(200, 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, dataResponse(files))
}

View File

@@ -1,4 +1,4 @@
package server
package controllers
import "github.com/Xhofe/alist/alidrive"

View File

@@ -0,0 +1,49 @@
package controllers
import (
"fmt"
"github.com/Xhofe/alist/alidrive"
"github.com/Xhofe/alist/conf"
"github.com/gin-gonic/gin"
"github.com/patrickmn/go-cache"
log "github.com/sirupsen/logrus"
)
func Search(c *gin.Context) {
if !conf.Conf.Server.Search {
c.JSON(200, metaResponse(403,"Not allow search."))
return
}
var search alidrive.SearchReq
if err := c.ShouldBindJSON(&search); err != nil {
c.JSON(200, 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, 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, metaResponse(500,err.Error()))
return
}
if conf.Conf.Cache.Enable {
conf.Cache.Set(cacheKey,files,cache.DefaultExpiration)
}
c.JSON(200, dataResponse(files))
}

View File

@@ -0,0 +1,25 @@
package controllers
import (
"github.com/Xhofe/alist/conf"
"github.com/gin-gonic/gin"
)
func Info(c *gin.Context) {
c.JSON(200, dataResponse(conf.Conf.Info))
}
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
}

View File

@@ -19,7 +19,7 @@ func CrosHandler() gin.HandlerFunc {
//context.Set("content-type", "application/json") //设置返回格式是json
if method == "OPTIONS" {
context.JSON(http.StatusOK, metaResponse(200,"Options Request!"))
context.JSON(http.StatusOK, gin.H{})
}
//处理请求

View File

@@ -2,6 +2,7 @@ package server
import (
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/server/controllers"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
@@ -20,10 +21,11 @@ func InitApiRouter(engine *gin.Engine) {
})
v2:=engine.Group("/api")
{
v2.GET("/info",Info)
v2.POST("/get",Get)
v2.POST("/list",List)
v2.POST("/search",Search)
v2.GET("/info",controllers.Info)
v2.POST("/get",controllers.Get)
v2.POST("/list",controllers.List)
v2.POST("/search",controllers.Search)
}
engine.GET("/d/*file_id",Down)
engine.GET("/d/*file_id",controllers.Down)
engine.GET("/cache/:password",controllers.RefreshCache)
}