🐝 reformat code
This commit is contained in:
@@ -5,9 +5,9 @@ import "github.com/gin-gonic/gin"
|
||||
// common meta response
|
||||
func MetaResponse(code int, msg string) gin.H {
|
||||
return gin.H{
|
||||
"meta":gin.H{
|
||||
"code":code,
|
||||
"msg":msg,
|
||||
"meta": gin.H{
|
||||
"code": code,
|
||||
"msg": msg,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -15,10 +15,10 @@ func MetaResponse(code int, msg string) gin.H {
|
||||
// common data response
|
||||
func DataResponse(data interface{}) gin.H {
|
||||
return gin.H{
|
||||
"meta":gin.H{
|
||||
"code":200,
|
||||
"msg":"success",
|
||||
"meta": gin.H{
|
||||
"code": 200,
|
||||
"msg": "success",
|
||||
},
|
||||
"data":data,
|
||||
"data": data,
|
||||
}
|
||||
}
|
||||
|
@@ -13,30 +13,30 @@ func Info(c *gin.Context) {
|
||||
|
||||
// handle refresh_cache request
|
||||
func RefreshCache(c *gin.Context) {
|
||||
password:=c.Param("password")
|
||||
password := c.Param("password")
|
||||
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
|
||||
}
|
||||
|
||||
// rebuild tree
|
||||
func RebuildTree(c *gin.Context) {
|
||||
if err:=models.Clear();err!=nil{
|
||||
c.JSON(200,MetaResponse(500,err.Error()))
|
||||
if err := models.Clear(); err != nil {
|
||||
c.JSON(200, MetaResponse(500, err.Error()))
|
||||
return
|
||||
}
|
||||
if err:=models.BuildTree();err!=nil {
|
||||
c.JSON(200,MetaResponse(500,err.Error()))
|
||||
if err := models.BuildTree(); err != nil {
|
||||
c.JSON(200, MetaResponse(500, err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(200,MetaResponse(200,"success."))
|
||||
c.JSON(200, MetaResponse(200, "success."))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@@ -13,10 +13,10 @@ import (
|
||||
func Get(c *gin.Context) {
|
||||
var get alidrive.GetReq
|
||||
if err := c.ShouldBindJSON(&get); err != nil {
|
||||
c.JSON(200, controllers.MetaResponse(400,"Bad Request"))
|
||||
c.JSON(200, controllers.MetaResponse(400, "Bad Request"))
|
||||
return
|
||||
}
|
||||
log.Debugf("get:%+v",get)
|
||||
log.Debugf("get:%+v", get)
|
||||
// cache
|
||||
//cacheKey:=fmt.Sprintf("%s-%s","g",get.FileId)
|
||||
//if conf.Conf.Cache.Enable {
|
||||
@@ -27,23 +27,23 @@ func Get(c *gin.Context) {
|
||||
// return
|
||||
// }
|
||||
//}
|
||||
file,err:=alidrive.GetFile(get.FileId)
|
||||
if err !=nil {
|
||||
c.JSON(200, controllers.MetaResponse(500,err.Error()))
|
||||
file, err := alidrive.GetFile(get.FileId)
|
||||
if err != nil {
|
||||
c.JSON(200, controllers.MetaResponse(500, err.Error()))
|
||||
return
|
||||
}
|
||||
paths,err:=alidrive.GetPaths(get.FileId)
|
||||
if err!=nil {
|
||||
c.JSON(200, controllers.MetaResponse(500,err.Error()))
|
||||
paths, err := alidrive.GetPaths(get.FileId)
|
||||
if err != nil {
|
||||
c.JSON(200, controllers.MetaResponse(500, err.Error()))
|
||||
return
|
||||
}
|
||||
file.Paths=*paths
|
||||
download,err:=alidrive.GetDownLoadUrl(get.FileId)
|
||||
if err!=nil {
|
||||
c.JSON(200, controllers.MetaResponse(500,err.Error()))
|
||||
file.Paths = *paths
|
||||
download, err := alidrive.GetDownLoadUrl(get.FileId)
|
||||
if err != nil {
|
||||
c.JSON(200, controllers.MetaResponse(500, err.Error()))
|
||||
return
|
||||
}
|
||||
file.DownloadUrl=download.Url
|
||||
file.DownloadUrl = download.Url
|
||||
//if conf.Conf.Cache.Enable {
|
||||
// conf.Cache.Set(cacheKey,file,cache.DefaultExpiration)
|
||||
//}
|
||||
@@ -51,9 +51,9 @@ func Get(c *gin.Context) {
|
||||
}
|
||||
|
||||
func Down(c *gin.Context) {
|
||||
fileIdParam:=c.Param("file_id")
|
||||
log.Debugf("down:%s",fileIdParam)
|
||||
fileId:=strings.Split(fileIdParam,"/")[1]
|
||||
fileIdParam := c.Param("file_id")
|
||||
log.Debugf("down:%s", fileIdParam)
|
||||
fileId := strings.Split(fileIdParam, "/")[1]
|
||||
//cacheKey:=fmt.Sprintf("%s-%s","d",fileId)
|
||||
//if conf.Conf.Cache.Enable {
|
||||
// downloadUrl,exist:=conf.Cache.Get(cacheKey)
|
||||
@@ -63,14 +63,14 @@ func Down(c *gin.Context) {
|
||||
// return
|
||||
// }
|
||||
//}
|
||||
file,err:=alidrive.GetDownLoadUrl(fileId)
|
||||
file, err := alidrive.GetDownLoadUrl(fileId)
|
||||
if err != nil {
|
||||
c.JSON(200, controllers.MetaResponse(500,err.Error()))
|
||||
c.JSON(200, controllers.MetaResponse(500, err.Error()))
|
||||
return
|
||||
}
|
||||
//if conf.Conf.Cache.Enable {
|
||||
// conf.Cache.Set(cacheKey,file.DownloadUrl,cache.DefaultExpiration)
|
||||
//}
|
||||
c.Redirect(301,file.Url)
|
||||
c.Redirect(301, file.Url)
|
||||
return
|
||||
}
|
||||
|
@@ -12,65 +12,65 @@ import (
|
||||
|
||||
// list request bean
|
||||
type ListReq struct {
|
||||
Password string `json:"password"`
|
||||
Password string `json:"password"`
|
||||
alidrive.ListReq
|
||||
}
|
||||
|
||||
// handle list request
|
||||
func List(c *gin.Context) {
|
||||
var list ListReq
|
||||
if err := c.ShouldBindJSON(&list);err!=nil {
|
||||
c.JSON(200, controllers.MetaResponse(400,"Bad Request"))
|
||||
if err := c.ShouldBindJSON(&list); err != nil {
|
||||
c.JSON(200, controllers.MetaResponse(400, "Bad Request"))
|
||||
return
|
||||
}
|
||||
log.Debugf("list:%+v",list)
|
||||
log.Debugf("list:%+v", list)
|
||||
// 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 {
|
||||
files,exist:=conf.Cache.Get(cacheKey)
|
||||
files, exist := conf.Cache.Get(cacheKey)
|
||||
if exist {
|
||||
log.Debugf("使用了缓存:%s",cacheKey)
|
||||
log.Debugf("使用了缓存:%s", cacheKey)
|
||||
c.JSON(200, controllers.DataResponse(files))
|
||||
return
|
||||
}
|
||||
}
|
||||
var (
|
||||
files *alidrive.Files
|
||||
err error
|
||||
err error
|
||||
)
|
||||
if list.Limit == 0 {
|
||||
list.Limit=50
|
||||
list.Limit = 50
|
||||
}
|
||||
if conf.Conf.AliDrive.MaxFilesCount!=0 {
|
||||
list.Limit=conf.Conf.AliDrive.MaxFilesCount
|
||||
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)
|
||||
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, controllers.MetaResponse(500,err.Error()))
|
||||
if err != nil {
|
||||
c.JSON(200, controllers.MetaResponse(500, err.Error()))
|
||||
return
|
||||
}
|
||||
password:=alidrive.HasPassword(files)
|
||||
if password!="" && password!=list.Password {
|
||||
if list.Password=="" {
|
||||
c.JSON(200, controllers.MetaResponse(401,"need password."))
|
||||
password := alidrive.HasPassword(files)
|
||||
if password != "" && password != list.Password {
|
||||
if list.Password == "" {
|
||||
c.JSON(200, controllers.MetaResponse(401, "need password."))
|
||||
return
|
||||
}
|
||||
c.JSON(200, controllers.MetaResponse(401,"wrong password."))
|
||||
c.JSON(200, controllers.MetaResponse(401, "wrong password."))
|
||||
return
|
||||
}
|
||||
paths,err:=alidrive.GetPaths(list.ParentFileId)
|
||||
if err!=nil {
|
||||
c.JSON(200, controllers.MetaResponse(500,err.Error()))
|
||||
paths, err := alidrive.GetPaths(list.ParentFileId)
|
||||
if err != nil {
|
||||
c.JSON(200, controllers.MetaResponse(500, err.Error()))
|
||||
return
|
||||
}
|
||||
files.Paths=*paths
|
||||
files.Paths = *paths
|
||||
//files.Readme=alidrive.HasReadme(files)
|
||||
if conf.Conf.Cache.Enable {
|
||||
conf.Cache.Set(cacheKey,files,cache.DefaultExpiration)
|
||||
conf.Cache.Set(cacheKey, files, cache.DefaultExpiration)
|
||||
}
|
||||
c.JSON(200, controllers.DataResponse(files))
|
||||
}
|
||||
}
|
||||
|
@@ -11,14 +11,14 @@ import (
|
||||
func OfficePreview(c *gin.Context) {
|
||||
var req alidrive.OfficePreviewUrlReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(200, controllers.MetaResponse(400,"Bad Request"))
|
||||
c.JSON(200, controllers.MetaResponse(400, "Bad Request"))
|
||||
return
|
||||
}
|
||||
log.Debugf("preview_req:%+v",req)
|
||||
preview,err:=alidrive.GetOfficePreviewUrl(req.FileId)
|
||||
if err!=nil {
|
||||
c.JSON(200, controllers.MetaResponse(500,err.Error()))
|
||||
log.Debugf("preview_req:%+v", req)
|
||||
preview, err := alidrive.GetOfficePreviewUrl(req.FileId)
|
||||
if err != nil {
|
||||
c.JSON(200, controllers.MetaResponse(500, err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(200, controllers.DataResponse(preview))
|
||||
}
|
||||
}
|
||||
|
@@ -13,39 +13,39 @@ import (
|
||||
// handle search request
|
||||
func Search(c *gin.Context) {
|
||||
if !conf.Conf.Server.Search {
|
||||
c.JSON(200, controllers.MetaResponse(403,"Not allow search."))
|
||||
c.JSON(200, controllers.MetaResponse(403, "Not allow search."))
|
||||
return
|
||||
}
|
||||
var search alidrive.SearchReq
|
||||
if err := c.ShouldBindJSON(&search); err != nil {
|
||||
c.JSON(200, controllers.MetaResponse(400,"Bad Request"))
|
||||
c.JSON(200, controllers.MetaResponse(400, "Bad Request"))
|
||||
return
|
||||
}
|
||||
log.Debugf("search:%+v",search)
|
||||
log.Debugf("search:%+v", search)
|
||||
// cache
|
||||
cacheKey:=fmt.Sprintf("%s-%s","s",search.Query)
|
||||
cacheKey := fmt.Sprintf("%s-%s", "s", search.Query)
|
||||
if conf.Conf.Cache.Enable {
|
||||
files,exist:=conf.Cache.Get(cacheKey)
|
||||
files, exist := conf.Cache.Get(cacheKey)
|
||||
if exist {
|
||||
log.Debugf("使用了缓存:%s",cacheKey)
|
||||
log.Debugf("使用了缓存:%s", cacheKey)
|
||||
c.JSON(200, controllers.DataResponse(files))
|
||||
return
|
||||
}
|
||||
}
|
||||
if search.Limit == 0 {
|
||||
search.Limit=50
|
||||
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)
|
||||
files, err := alidrive.Search(search.Query, search.Limit, search.OrderBy)
|
||||
if err != nil {
|
||||
c.JSON(200, controllers.MetaResponse(500,err.Error()))
|
||||
c.JSON(200, controllers.MetaResponse(500, err.Error()))
|
||||
return
|
||||
}
|
||||
if conf.Conf.Cache.Enable {
|
||||
conf.Cache.Set(cacheKey,files,cache.DefaultExpiration)
|
||||
conf.Cache.Set(cacheKey, files, cache.DefaultExpiration)
|
||||
}
|
||||
c.JSON(200, controllers.DataResponse(files))
|
||||
}
|
||||
}
|
||||
|
@@ -23,8 +23,8 @@ func Get(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
log.Debugf("list:%+v", get)
|
||||
path,name:=filepath.Split(get.File)
|
||||
file, err := models.GetFileByParentPathAndName(path,name)
|
||||
dir, name := filepath.Split(get.File)
|
||||
file, err := models.GetFileByParentPathAndName(dir, name)
|
||||
if err != nil {
|
||||
c.JSON(200, controllers.MetaResponse(500, err.Error()))
|
||||
return
|
||||
@@ -34,19 +34,19 @@ func Get(c *gin.Context) {
|
||||
|
||||
// handle download request
|
||||
func Down(c *gin.Context) {
|
||||
filePath:=c.Param("file")
|
||||
log.Debugf("down:%s",filePath)
|
||||
path,name:=filepath.Split(filePath)
|
||||
fileModel, err := models.GetFileByParentPathAndName(path,name)
|
||||
filePath := c.Param("file")
|
||||
log.Debugf("down:%s", filePath)
|
||||
dir, name := filepath.Split(filePath)
|
||||
fileModel, err := models.GetFileByParentPathAndName(dir, name)
|
||||
if err != nil {
|
||||
c.JSON(200, controllers.MetaResponse(500, err.Error()))
|
||||
return
|
||||
}
|
||||
file,err:=alidrive.GetDownLoadUrl(fileModel.FileId)
|
||||
file, err := alidrive.GetDownLoadUrl(fileModel.FileId)
|
||||
if err != nil {
|
||||
c.JSON(200, controllers.MetaResponse(500,err.Error()))
|
||||
c.JSON(200, controllers.MetaResponse(500, err.Error()))
|
||||
return
|
||||
}
|
||||
c.Redirect(301,file.Url)
|
||||
c.Redirect(301, file.Url)
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user