去除缓存

This commit is contained in:
微凉
2021-03-07 21:02:56 +08:00
parent 443067b80f
commit b677d6ad21
19 changed files with 162 additions and 351 deletions

View File

@ -1,16 +0,0 @@
package bootstrap
import (
"github.com/Xhofe/alist/conf"
"github.com/patrickmn/go-cache"
log "github.com/sirupsen/logrus"
"time"
)
// init cache
func InitCache() {
if conf.Conf.Cache.Enable {
log.Infof("初始化缓存...")
conf.Cache = cache.New(time.Duration(conf.Conf.Cache.Expiration)*time.Minute, time.Duration(conf.Conf.Cache.CleanupInterval)*time.Minute)
}
}

View File

@ -65,7 +65,6 @@ func start() {
log.Errorf("初始化数据库出现错误,启动失败.")
return
}
InitCache()
InitCron()
server()
}

View File

@ -25,12 +25,6 @@ type Config struct {
Static string `yaml:"static"`
SiteUrl string `yaml:"site_url" json:"site_url"` //网站url
} `yaml:"server"`
Cache struct {
Enable bool `yaml:"enable"`
Expiration int `yaml:"expiration"`
CleanupInterval int `yaml:"cleanup_interval"`
RefreshPassword string `yaml:"refresh_password"`
}
AliDrive struct {
ApiUrl string `yaml:"api_url"` //阿里云盘api
RootFolder string `yaml:"root_folder"` //根目录id

View File

@ -1,7 +1,6 @@
package conf
import (
"github.com/patrickmn/go-cache"
"gorm.io/gorm"
"net/http"
)
@ -16,8 +15,6 @@ var (
Client *http.Client // request client
Authorization string // authorization string
Cache *cache.Cache // cache
DB *gorm.DB
Origins []string // allow origins

1
go.mod
View File

@ -12,7 +12,6 @@ require (
github.com/mattn/go-sqlite3 v1.14.6 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/robfig/cron/v3 v3.0.0
github.com/sirupsen/logrus v1.7.0
github.com/ugorji/go v1.2.2 // indirect

2
go.sum
View File

@ -77,8 +77,6 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLD
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=

84
server/controllers/get.go Normal file
View 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
}

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

View File

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

View File

@ -0,0 +1 @@
package controllers

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,6 +2,7 @@ package test
import (
"fmt"
"path/filepath"
"strings"
"testing"
)
@ -19,3 +20,8 @@ func TestPassword(t *testing.T) {
password:=fullName[index+10:]
fmt.Printf("name:%s, password:%s\n",name,password)
}
func TestDir(t *testing.T) {
dir,file:=filepath.Split("/root/")
fmt.Printf("dir:%s\nfile:%s\n",dir,file)
}