🉑 origin优化
This commit is contained in:
@@ -2,7 +2,7 @@ package controllers
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func metaResponse(code int, msg string) gin.H {
|
||||
func MetaResponse(code int, msg string) gin.H {
|
||||
return gin.H{
|
||||
"meta":gin.H{
|
||||
"code":code,
|
||||
@@ -11,7 +11,7 @@ func metaResponse(code int, msg string) gin.H {
|
||||
}
|
||||
}
|
||||
|
||||
func dataResponse(data interface{}) gin.H {
|
||||
func DataResponse(data interface{}) gin.H {
|
||||
return gin.H{
|
||||
"meta":gin.H{
|
||||
"code":200,
|
||||
|
@@ -12,7 +12,7 @@ import (
|
||||
func Get(c *gin.Context) {
|
||||
var get alidrive.GetReq
|
||||
if err := c.ShouldBindJSON(&get); err != nil {
|
||||
c.JSON(200,metaResponse(400,"Bad Request"))
|
||||
c.JSON(200, MetaResponse(400,"Bad Request"))
|
||||
return
|
||||
}
|
||||
log.Debugf("get:%+v",get)
|
||||
@@ -22,25 +22,25 @@ func Get(c *gin.Context) {
|
||||
// file,exist:=conf.Cache.Get(cacheKey)
|
||||
// if exist {
|
||||
// log.Debugf("使用了缓存:%s",cacheKey)
|
||||
// c.JSON(200,dataResponse(file))
|
||||
// c.JSON(200,DataResponse(file))
|
||||
// return
|
||||
// }
|
||||
//}
|
||||
file,err:=alidrive.GetFile(get.FileId)
|
||||
if err !=nil {
|
||||
c.JSON(200,metaResponse(500,err.Error()))
|
||||
c.JSON(200, MetaResponse(500,err.Error()))
|
||||
return
|
||||
}
|
||||
paths,err:=alidrive.GetPaths(get.FileId)
|
||||
if err!=nil {
|
||||
c.JSON(200,metaResponse(500,err.Error()))
|
||||
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))
|
||||
c.JSON(200, DataResponse(file))
|
||||
}
|
||||
|
||||
func Down(c *gin.Context) {
|
||||
@@ -58,7 +58,7 @@ func Down(c *gin.Context) {
|
||||
//}
|
||||
file,err:=alidrive.GetFile(fileId)
|
||||
if err != nil {
|
||||
c.JSON(200, metaResponse(500,err.Error()))
|
||||
c.JSON(200, MetaResponse(500,err.Error()))
|
||||
return
|
||||
}
|
||||
//if conf.Conf.Cache.Enable {
|
||||
|
@@ -17,7 +17,7 @@ type ListReq struct {
|
||||
func List(c *gin.Context) {
|
||||
var list ListReq
|
||||
if err := c.ShouldBindJSON(&list);err!=nil {
|
||||
c.JSON(200, metaResponse(400,"Bad Request"))
|
||||
c.JSON(200, MetaResponse(400,"Bad Request"))
|
||||
return
|
||||
}
|
||||
log.Debugf("list:%+v",list)
|
||||
@@ -27,7 +27,7 @@ func List(c *gin.Context) {
|
||||
files,exist:=conf.Cache.Get(cacheKey)
|
||||
if exist {
|
||||
log.Debugf("使用了缓存:%s",cacheKey)
|
||||
c.JSON(200, dataResponse(files))
|
||||
c.JSON(200, DataResponse(files))
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -47,21 +47,21 @@ func List(c *gin.Context) {
|
||||
files,err=alidrive.GetList(list.ParentFileId,list.Limit,list.Marker,list.OrderBy,list.OrderDirection)
|
||||
}
|
||||
if err!=nil {
|
||||
c.JSON(200, metaResponse(500,err.Error()))
|
||||
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."))
|
||||
c.JSON(200, MetaResponse(401,"need password."))
|
||||
return
|
||||
}
|
||||
c.JSON(200, metaResponse(401,"wrong password."))
|
||||
c.JSON(200, MetaResponse(401,"wrong password."))
|
||||
return
|
||||
}
|
||||
paths,err:=alidrive.GetPaths(list.ParentFileId)
|
||||
if err!=nil {
|
||||
c.JSON(200, metaResponse(500,err.Error()))
|
||||
c.JSON(200, MetaResponse(500,err.Error()))
|
||||
return
|
||||
}
|
||||
files.Paths=*paths
|
||||
@@ -69,5 +69,5 @@ func List(c *gin.Context) {
|
||||
if conf.Conf.Cache.Enable {
|
||||
conf.Cache.Set(cacheKey,files,cache.DefaultExpiration)
|
||||
}
|
||||
c.JSON(200, dataResponse(files))
|
||||
c.JSON(200, DataResponse(files))
|
||||
}
|
@@ -11,12 +11,12 @@ import (
|
||||
|
||||
func Search(c *gin.Context) {
|
||||
if !conf.Conf.Server.Search {
|
||||
c.JSON(200, metaResponse(403,"Not allow 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"))
|
||||
c.JSON(200, MetaResponse(400,"Bad Request"))
|
||||
return
|
||||
}
|
||||
log.Debugf("search:%+v",search)
|
||||
@@ -26,7 +26,7 @@ func Search(c *gin.Context) {
|
||||
files,exist:=conf.Cache.Get(cacheKey)
|
||||
if exist {
|
||||
log.Debugf("使用了缓存:%s",cacheKey)
|
||||
c.JSON(200, dataResponse(files))
|
||||
c.JSON(200, DataResponse(files))
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -39,11 +39,11 @@ func Search(c *gin.Context) {
|
||||
//}
|
||||
files,err:=alidrive.Search(search.Query,search.Limit,search.OrderBy)
|
||||
if err != nil {
|
||||
c.JSON(200, metaResponse(500,err.Error()))
|
||||
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))
|
||||
c.JSON(200, DataResponse(files))
|
||||
}
|
@@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func Info(c *gin.Context) {
|
||||
c.JSON(200, dataResponse(conf.Conf.Info))
|
||||
c.JSON(200, DataResponse(conf.Conf.Info))
|
||||
}
|
||||
|
||||
func RefreshCache(c *gin.Context) {
|
||||
@@ -14,12 +14,12 @@ func RefreshCache(c *gin.Context) {
|
||||
if conf.Conf.Cache.Enable {
|
||||
if password == conf.Conf.Cache.RefreshPassword {
|
||||
conf.Cache.Flush()
|
||||
c.JSON(200,metaResponse(200,"flush success."))
|
||||
c.JSON(200, MetaResponse(200,"flush success."))
|
||||
return
|
||||
}
|
||||
c.JSON(200,metaResponse(401,"wrong password."))
|
||||
c.JSON(200, MetaResponse(401,"wrong password."))
|
||||
return
|
||||
}
|
||||
c.JSON(200,metaResponse(400,"disabled cache."))
|
||||
c.JSON(200, MetaResponse(400,"disabled cache."))
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user