🍃 去除文件缓存

This commit is contained in:
微凉
2020-12-31 15:03:25 +08:00
parent ba7c33a2bb
commit d5e3527bfb
12 changed files with 84 additions and 46 deletions

View File

@ -12,7 +12,7 @@ func TokenLogin() (*TokenLoginResp, error) {
log.Infof("尝试使用token登录...") log.Infof("尝试使用token登录...")
url:="https://auth.aliyundrive.com/v2/oauth/token_login" url:="https://auth.aliyundrive.com/v2/oauth/token_login"
req:=TokenLoginReq{Token:conf.Conf.AliDrive.LoginToken} req:=TokenLoginReq{Token:conf.Conf.AliDrive.LoginToken}
log.Debugf("token_login_req:%v",req) log.Debugf("token_login_req:%+v",req)
var tokenLogin TokenLoginResp var tokenLogin TokenLoginResp
if body, err := DoPost(url, req,false); err != nil { if body, err := DoPost(url, req,false); err != nil {
log.Errorf("tokenLogin-doPost出错:%s",err.Error()) log.Errorf("tokenLogin-doPost出错:%s",err.Error())

View File

@ -146,7 +146,7 @@ func DoPost(url string,request interface{},auth bool) (body []byte, err error) {
log.Errorf("创建requestBody出错:%s",err.Error()) log.Errorf("创建requestBody出错:%s",err.Error())
} }
req,err:=http.NewRequest("POST",url,requestBody) req,err:=http.NewRequest("POST",url,requestBody)
log.Debugf("do_post_req:%v",req) log.Debugf("do_post_req:%+v",req)
if err != nil { if err != nil {
log.Errorf("创建request出错:%s",err.Error()) log.Errorf("创建request出错:%s",err.Error())
return return

View File

@ -24,13 +24,13 @@ func InitAliDrive() bool {
}else { }else {
conf.Authorization=conf.Bearer+conf.Conf.AliDrive.AccessToken conf.Authorization=conf.Bearer+conf.Conf.AliDrive.AccessToken
} }
log.Infof("token:%s",conf.Authorization) log.Debugf("token:%s",conf.Authorization)
user,err:=alidrive.GetUserInfo() user,err:=alidrive.GetUserInfo()
if err != nil { if err != nil {
log.Errorf("初始化用户失败:%s",err.Error()) log.Errorf("初始化用户失败:%s",err.Error())
return false return false
} }
log.Infof("当前用户信息:%v",user) log.Infof("当前用户信息:%+v",user)
alidrive.User=user alidrive.User=user
return true return true
} }

View File

@ -39,6 +39,7 @@ func printASC() {
func start() { func start() {
InitLog() InitLog()
printASC() printASC()
CheckUpdate()
if !ReadConf(conf.Con) { if !ReadConf(conf.Con) {
log.Errorf("读取配置文件时出现错误,启动失败.") log.Errorf("读取配置文件时出现错误,启动失败.")
return return

View File

@ -24,6 +24,6 @@ func ReadConf(config string) bool {
log.Errorf("加载配置文件时发生错误:%s",err.Error()) log.Errorf("加载配置文件时发生错误:%s",err.Error())
return false return false
} }
log.Debugf("config:%v",conf.Conf) log.Debugf("config:%+v",conf.Conf)
return true return true
} }

41
bootstrap/update.go Normal file
View File

@ -0,0 +1,41 @@
package bootstrap
import (
"encoding/json"
"github.com/Xhofe/alist/conf"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
)
type GithubRelease struct {
TagName string `json:"tag_name"`
HtmlUrl string `json:"html_url"`
Body string `json:"body"`
}
func CheckUpdate() {
url:="https://api.github.com/repos/Xhofe/alist/releases/latest"
resp,err:=http.Get(url)
if err!=nil {
log.Warnf("检查更新失败:%s",err.Error())
return
}
body,err:=ioutil.ReadAll(resp.Body)
if err!=nil {
log.Warnf("读取更新内容失败:%s",err.Error())
return
}
var release GithubRelease
err = json.Unmarshal(body,&release)
if err!=nil {
log.Warnf("解析更新失败:%s",err.Error())
return
}
if conf.VERSION == release.TagName {
log.Infof("当前已是最新版本:%s",release.TagName)
}else {
log.Infof("发现新版本:%s",release.TagName)
log.Infof("请至'%s'获取更新.",release.HtmlUrl)
}
}

View File

@ -12,8 +12,8 @@ server:
static: dist #前端文件目录 static: dist #前端文件目录
cache: cache:
enable: true #是否开启缓存 enable: true #是否开启缓存
expiration: 5 #缓存失效时间(单位:分钟) expiration: 60 #缓存失效时间(单位:分钟)
cleanup_interval: 10 #清理失效缓存间隔 cleanup_interval: 120 #清理失效缓存间隔
refresh_password: password #手动清理缓存密码 refresh_password: password #手动清理缓存密码
ali_drive: ali_drive:
api_url: https://api.aliyundrive.com/v2 #阿里云盘api,无需修改 api_url: https://api.aliyundrive.com/v2 #阿里云盘api,无需修改

View File

@ -18,7 +18,7 @@ var(
var Conf = new(Config) var Conf = new(Config)
const ( const (
VERSION="0.1.0" VERSION="v0.1.3"
ImageThumbnailProcess="image/resize,w_50" ImageThumbnailProcess="image/resize,w_50"
VideoThumbnailProcess="video/snapshot,t_0,f_jpg,w_50" VideoThumbnailProcess="video/snapshot,t_0,f_jpg,w_50"

View File

@ -1,32 +1,31 @@
package controllers package controllers
import ( import (
"fmt"
"github.com/Xhofe/alist/alidrive" "github.com/Xhofe/alist/alidrive"
"github.com/Xhofe/alist/conf"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/patrickmn/go-cache"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"strings" "strings"
) )
// 因为下载地址有时效,所以去掉了文件请求和直链的缓存
func Get(c *gin.Context) { func Get(c *gin.Context) {
var get alidrive.GetReq var get alidrive.GetReq
if err := c.ShouldBindJSON(&get); err != nil { if err := c.ShouldBindJSON(&get); err != nil {
c.JSON(200,metaResponse(400,"Bad Request")) c.JSON(200,metaResponse(400,"Bad Request"))
return return
} }
log.Debugf("get:%v",get) log.Debugf("get:%+v",get)
// cache // cache
cacheKey:=fmt.Sprintf("%s-%s","g",get.FileId) //cacheKey:=fmt.Sprintf("%s-%s","g",get.FileId)
if conf.Conf.Cache.Enable { //if conf.Conf.Cache.Enable {
file,exist:=conf.Cache.Get(cacheKey) // file,exist:=conf.Cache.Get(cacheKey)
if exist { // if exist {
log.Debugf("使用了缓存:%s",cacheKey) // log.Debugf("使用了缓存:%s",cacheKey)
c.JSON(200,dataResponse(file)) // c.JSON(200,dataResponse(file))
return // return
} // }
} //}
file,err:=alidrive.GetFile(get.FileId) file,err:=alidrive.GetFile(get.FileId)
if err !=nil { if err !=nil {
c.JSON(200,metaResponse(500,err.Error())) c.JSON(200,metaResponse(500,err.Error()))
@ -38,9 +37,9 @@ func Get(c *gin.Context) {
return return
} }
file.Paths=*paths file.Paths=*paths
if conf.Conf.Cache.Enable { //if conf.Conf.Cache.Enable {
conf.Cache.Set(cacheKey,file,cache.DefaultExpiration) // conf.Cache.Set(cacheKey,file,cache.DefaultExpiration)
} //}
c.JSON(200,dataResponse(file)) c.JSON(200,dataResponse(file))
} }
@ -48,23 +47,23 @@ func Down(c *gin.Context) {
fileIdParam:=c.Param("file_id") fileIdParam:=c.Param("file_id")
log.Debugf("down:%s",fileIdParam) log.Debugf("down:%s",fileIdParam)
fileId:=strings.Split(fileIdParam,"/")[1] fileId:=strings.Split(fileIdParam,"/")[1]
cacheKey:=fmt.Sprintf("%s-%s","d",fileId) //cacheKey:=fmt.Sprintf("%s-%s","d",fileId)
if conf.Conf.Cache.Enable { //if conf.Conf.Cache.Enable {
downloadUrl,exist:=conf.Cache.Get(cacheKey) // downloadUrl,exist:=conf.Cache.Get(cacheKey)
if exist { // if exist {
log.Debugf("使用了缓存:%s",cacheKey) // log.Debugf("使用了缓存:%s",cacheKey)
c.Redirect(301,downloadUrl.(string)) // c.Redirect(301,downloadUrl.(string))
return // return
} // }
} //}
file,err:=alidrive.GetFile(fileId) file,err:=alidrive.GetFile(fileId)
if err != nil { if err != nil {
c.JSON(200, metaResponse(500,err.Error())) c.JSON(200, metaResponse(500,err.Error()))
return return
} }
if conf.Conf.Cache.Enable { //if conf.Conf.Cache.Enable {
conf.Cache.Set(cacheKey,file.DownloadUrl,cache.DefaultExpiration) // conf.Cache.Set(cacheKey,file.DownloadUrl,cache.DefaultExpiration)
} //}
c.Redirect(301,file.DownloadUrl) c.Redirect(301,file.DownloadUrl)
return return
} }

View File

@ -9,13 +9,18 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
type ListReq struct {
Password string `json:"password"`
alidrive.ListReq
}
func List(c *gin.Context) { func List(c *gin.Context) {
var list ListReq var list ListReq
if err := c.ShouldBindJSON(&list);err!=nil { if err := c.ShouldBindJSON(&list);err!=nil {
c.JSON(200, metaResponse(400,"Bad Request")) c.JSON(200, metaResponse(400,"Bad Request"))
return return
} }
log.Debugf("list:%v",list) log.Debugf("list:%+v",list)
// cache // cache
cacheKey:=fmt.Sprintf("%s-%s-%s","l",list.ParentFileId,list.Password) cacheKey:=fmt.Sprintf("%s-%s-%s","l",list.ParentFileId,list.Password)
if conf.Conf.Cache.Enable { if conf.Conf.Cache.Enable {

View File

@ -1,8 +0,0 @@
package controllers
import "github.com/Xhofe/alist/alidrive"
type ListReq struct {
Password string `json:"password"`
alidrive.ListReq
}

View File

@ -19,7 +19,7 @@ func Search(c *gin.Context) {
c.JSON(200, metaResponse(400,"Bad Request")) c.JSON(200, metaResponse(400,"Bad Request"))
return return
} }
log.Debugf("search:%v",search) log.Debugf("search:%+v",search)
// cache // cache
cacheKey:=fmt.Sprintf("%s-%s","s",search.Query) cacheKey:=fmt.Sprintf("%s-%s","s",search.Query)
if conf.Conf.Cache.Enable { if conf.Conf.Cache.Enable {