🉑 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
|
||||
}
|
@@ -2,31 +2,28 @@ package server
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/server/controllers"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func CrosHandler() gin.HandlerFunc {
|
||||
return func(context *gin.Context) {
|
||||
method := context.Request.Method
|
||||
// 设置跨域
|
||||
if conf.Conf.Info.SiteUrl=="*"||utils.ContainsString(conf.Origins,context.GetHeader("Origin"))!=-1 {
|
||||
context.Header("Access-Control-Allow-Origin",context.GetHeader("Origin"))
|
||||
}else {
|
||||
context.Header("Access-Control-Allow-Origin", conf.Conf.Info.SiteUrl)//跨域访问
|
||||
}
|
||||
context.Header("Access-Control-Allow-Origin",context.GetHeader("Origin"))
|
||||
context.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE,UPDATE")
|
||||
context.Header("Access-Control-Allow-Headers", "Content-Length,session,Accept, Origin, Host, Connection, Accept-Encoding, Accept-Language, Keep-Alive, User-Agent, Cache-Control, Content-Type, Pragma")
|
||||
context.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers,Cache-Control,Content-Language,Content-Type,Expires,Last-Modified,Pragma,FooBar")
|
||||
context.Header("Access-Control-Allow-Headers", "Content-Length,session,Accept, Origin, Host, Connection, Accept-Encoding, Accept-Language, Keep-Alive, User-Agent, Cache-Control, Content-Type")
|
||||
context.Header("Access-Control-Expose-Headers", "Content-Length,Cache-Control,Content-Language,Content-Type,Expires,Last-Modified")
|
||||
context.Header("Access-Control-Max-Age", "172800")
|
||||
context.Header("Access-Control-Allow-Credentials", "true")
|
||||
//context.Set("content-type", "application/json") //设置返回格式是json
|
||||
|
||||
if method == "OPTIONS" {
|
||||
context.JSON(http.StatusOK, gin.H{})
|
||||
// 信任域名
|
||||
if conf.Conf.Server.SiteUrl!="*"&&utils.ContainsString(conf.Origins,context.GetHeader("Origin"))==-1 {
|
||||
context.JSON(200,controllers.MetaResponse(413,"The origin is not in the site_url list, please configure it correctly."))
|
||||
context.Abort()
|
||||
}
|
||||
if method == "OPTIONS" {
|
||||
context.AbortWithStatus(204)
|
||||
}
|
||||
|
||||
//处理请求
|
||||
context.Next()
|
||||
}
|
||||
|
Reference in New Issue
Block a user