Files
OpenList/server/controllers/get.go

76 lines
1.8 KiB
Go
Raw Normal View History

2020-12-29 13:37:58 +08:00
package controllers
import (
"github.com/Xhofe/alist/alidrive"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"strings"
)
2021-02-04 10:02:34 +08:00
// handle get request
2020-12-31 15:03:25 +08:00
// 因为下载地址有时效,所以去掉了文件请求和直链的缓存
2020-12-29 13:37:58 +08:00
func Get(c *gin.Context) {
var get alidrive.GetReq
if err := c.ShouldBindJSON(&get); err != nil {
2021-01-11 16:53:48 +08:00
c.JSON(200, MetaResponse(400,"Bad Request"))
2020-12-29 13:37:58 +08:00
return
}
2020-12-31 15:03:25 +08:00
log.Debugf("get:%+v",get)
2020-12-29 13:37:58 +08:00
// cache
2020-12-31 15:03:25 +08:00
//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)
2021-01-11 16:53:48 +08:00
// c.JSON(200,DataResponse(file))
2020-12-31 15:03:25 +08:00
// return
// }
//}
2020-12-29 13:37:58 +08:00
file,err:=alidrive.GetFile(get.FileId)
if err !=nil {
2021-01-11 16:53:48 +08:00
c.JSON(200, MetaResponse(500,err.Error()))
2020-12-29 13:37:58 +08:00
return
}
paths,err:=alidrive.GetPaths(get.FileId)
if err!=nil {
2021-01-11 16:53:48 +08:00
c.JSON(200, MetaResponse(500,err.Error()))
2020-12-29 13:37:58 +08:00
return
}
file.Paths=*paths
download,err:=alidrive.GetDownLoadUrl(get.FileId)
if err!=nil {
c.JSON(200, MetaResponse(500,err.Error()))
return
}
file.DownloadUrl=download.Url
2020-12-31 15:03:25 +08:00
//if conf.Conf.Cache.Enable {
// conf.Cache.Set(cacheKey,file,cache.DefaultExpiration)
//}
2021-01-11 16:53:48 +08:00
c.JSON(200, DataResponse(file))
2020-12-29 13:37:58 +08:00
}
func Down(c *gin.Context) {
fileIdParam:=c.Param("file_id")
log.Debugf("down:%s",fileIdParam)
fileId:=strings.Split(fileIdParam,"/")[1]
2020-12-31 15:03:25 +08:00
//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)
2020-12-29 13:37:58 +08:00
if err != nil {
2021-01-11 16:53:48 +08:00
c.JSON(200, MetaResponse(500,err.Error()))
2020-12-29 13:37:58 +08:00
return
}
2020-12-31 15:03:25 +08:00
//if conf.Conf.Cache.Enable {
// conf.Cache.Set(cacheKey,file.DownloadUrl,cache.DefaultExpiration)
//}
c.Redirect(301,file.Url)
2020-12-29 13:37:58 +08:00
return
}