Initial commit
This commit is contained in:
22
server/common.go
Normal file
22
server/common.go
Normal file
@ -0,0 +1,22 @@
|
||||
package server
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func metaResponse(code int, msg string) gin.H {
|
||||
return gin.H{
|
||||
"meta":gin.H{
|
||||
"code":code,
|
||||
"msg":msg,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func dataResponse(data interface{}) gin.H {
|
||||
return gin.H{
|
||||
"meta":gin.H{
|
||||
"code":200,
|
||||
"msg":"success",
|
||||
},
|
||||
"data":data,
|
||||
}
|
||||
}
|
102
server/controllers.go
Normal file
102
server/controllers.go
Normal file
@ -0,0 +1,102 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/alidrive"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// TODO:添加前置路由信息
|
||||
|
||||
func Get(c *gin.Context) {
|
||||
var get alidrive.GetReq
|
||||
if err := c.ShouldBindJSON(&get); err != nil {
|
||||
c.JSON(200,metaResponse(400,"Bad Request"))
|
||||
return
|
||||
}
|
||||
log.Debugf("get:%v",get)
|
||||
file,err:=alidrive.GetFile(get.FileId)
|
||||
if err !=nil {
|
||||
c.JSON(200,metaResponse(500,err.Error()))
|
||||
return
|
||||
}
|
||||
paths,err:=alidrive.GetPaths(get.FileId)
|
||||
if err!=nil {
|
||||
c.JSON(200,metaResponse(500,err.Error()))
|
||||
return
|
||||
}
|
||||
file.Paths=*paths
|
||||
c.JSON(200,dataResponse(file))
|
||||
}
|
||||
|
||||
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)
|
||||
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,metaResponse(500,err.Error()))
|
||||
return
|
||||
}
|
||||
password:=alidrive.HasPassword(files)
|
||||
if password!="" && password!=list.Password {
|
||||
if list.Password=="" {
|
||||
c.JSON(200,metaResponse(401,"need password."))
|
||||
return
|
||||
}
|
||||
c.JSON(200,metaResponse(401,"wrong password."))
|
||||
return
|
||||
}
|
||||
paths,err:=alidrive.GetPaths(list.ParentFileId)
|
||||
if err!=nil {
|
||||
c.JSON(200,metaResponse(500,err.Error()))
|
||||
return
|
||||
}
|
||||
files.Paths=*paths
|
||||
files.Readme=alidrive.HasReadme(files)
|
||||
c.JSON(200,dataResponse(files))
|
||||
}
|
||||
|
||||
func Search(c *gin.Context) {
|
||||
if !conf.Conf.Server.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"))
|
||||
return
|
||||
}
|
||||
log.Debugf("search:%v",search)
|
||||
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,metaResponse(500,err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(200,dataResponse(files))
|
||||
}
|
27
server/middlewares.go
Normal file
27
server/middlewares.go
Normal file
@ -0,0 +1,27 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func CrosHandler() gin.HandlerFunc {
|
||||
return func(context *gin.Context) {
|
||||
method := context.Request.Method
|
||||
context.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
context.Header("Access-Control-Allow-Origin", "*") // 设置允许访问所有域
|
||||
context.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE,UPDATE")
|
||||
context.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, Token,session,X_Requested_With,Accept, Origin, Host, Connection, Accept-Encoding, Accept-Language,DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Pragma,token,openid,opentoken")
|
||||
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-Max-Age", "172800")
|
||||
context.Header("Access-Control-Allow-Credentials", "false")
|
||||
context.Set("content-type", "application/json") //设置返回格式是json
|
||||
|
||||
if method == "OPTIONS" {
|
||||
context.JSON(http.StatusOK, metaResponse(200,"Options Request!"))
|
||||
}
|
||||
|
||||
//处理请求
|
||||
context.Next()
|
||||
}
|
||||
}
|
8
server/req_bean.go
Normal file
8
server/req_bean.go
Normal file
@ -0,0 +1,8 @@
|
||||
package server
|
||||
|
||||
import "github.com/Xhofe/alist/alidrive"
|
||||
|
||||
type ListReq struct {
|
||||
Password string `json:"password"`
|
||||
alidrive.ListReq
|
||||
}
|
17
server/router.go
Normal file
17
server/router.go
Normal file
@ -0,0 +1,17 @@
|
||||
package server
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func InitRouter(engine *gin.Engine) {
|
||||
engine.Use(CrosHandler())
|
||||
InitApiRouter(engine)
|
||||
}
|
||||
|
||||
func InitApiRouter(engine *gin.Engine) {
|
||||
v2:=engine.Group("/api/v2")
|
||||
{
|
||||
v2.POST("/get",Get)
|
||||
v2.POST("/list",List)
|
||||
v2.POST("/search",Search)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user