🚧 合并get与list

This commit is contained in:
微凉
2021-03-08 20:26:02 +08:00
parent c0f50ffeff
commit 8636014397
11 changed files with 146 additions and 41 deletions

View File

@@ -10,7 +10,7 @@ import (
// get request bean
type GetReq struct {
File string `json:"file" binding:"required"`
Path string `json:"path" binding:"required"`
Password string `json:"password"`
}
@@ -22,11 +22,11 @@ func Get(c *gin.Context) {
return
}
log.Debugf("list:%+v", get)
dir, name := filepath.Split(get.File)
file, err := models.GetFileByParentPathAndName(dir, name)
dir, name := filepath.Split(get.Path)
file, err := models.GetFileByDirAndName(dir, name)
if err != nil {
if file == nil {
c.JSON(200, MetaResponse(404, "File not found."))
c.JSON(200, MetaResponse(404, "Path not found."))
return
}
c.JSON(200, MetaResponse(500, err.Error()))
@@ -49,7 +49,7 @@ type DownReq struct {
// handle download request
func Down(c *gin.Context) {
filePath := c.Param("file")
filePath := c.Param("path")[1:]
var down DownReq
if err := c.ShouldBindQuery(&down); err != nil {
c.JSON(200, MetaResponse(400, "Bad Request."))
@@ -57,10 +57,10 @@ func Down(c *gin.Context) {
}
log.Debugf("down:%s", filePath)
dir, name := filepath.Split(filePath)
fileModel, err := models.GetFileByParentPathAndName(dir, name)
fileModel, err := models.GetFileByDirAndName(dir, name)
if err != nil {
if fileModel == nil {
c.JSON(200, MetaResponse(404, "File not found."))
c.JSON(200, MetaResponse(404, "Path not found."))
return
}
c.JSON(200, MetaResponse(500, err.Error()))
@@ -74,6 +74,10 @@ func Down(c *gin.Context) {
}
return
}
if fileModel.Type == "folder" {
c.JSON(200, MetaResponse(406, "无法下载目录."))
return
}
file, err := alidrive.GetDownLoadUrl(fileModel.FileId)
if err != nil {
c.JSON(200, MetaResponse(500, err.Error()))

View File

@@ -9,7 +9,7 @@ import (
// list request bean
type ListReq struct {
Path string `json:"path" binding:"required"`
Dir string `json:"dir" binding:"required"`
Password string `json:"password"`
}
@@ -22,11 +22,11 @@ func List(c *gin.Context) {
}
log.Debugf("list:%+v", list)
// find folder model
dir, file := filepath.Split(list.Path)
fileModel, err := models.GetFileByParentPathAndName(dir, file)
dir, name := filepath.Split(list.Dir)
file, err := models.GetFileByDirAndName(dir, name)
if err != nil {
// folder model not exist
if fileModel == nil {
if file == nil {
c.JSON(200, MetaResponse(404, "folder not found."))
return
}
@@ -34,7 +34,7 @@ func List(c *gin.Context) {
return
}
// check password
if fileModel.Password != "" && fileModel.Password != list.Password {
if file.Password != "" && file.Password != list.Password {
if list.Password == "" {
c.JSON(200, MetaResponse(401, "need password."))
} else {
@@ -42,14 +42,14 @@ func List(c *gin.Context) {
}
return
}
files, err := models.GetFilesByParentPath(list.Path + "/")
// delete password
for i, _ := range *files {
(*files)[i].Password = ""
}
files, err := models.GetFilesByDir(list.Dir + "/")
if err != nil {
c.JSON(200, MetaResponse(500, err.Error()))
return
}
// delete password
for i, _ := range *files {
(*files)[i].Password = ""
}
c.JSON(200, DataResponse(files))
}

View File

@@ -0,0 +1,61 @@
package controllers
import (
"github.com/Xhofe/alist/server/models"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"path/filepath"
)
// path request bean
type PathReq struct {
Path string `json:"path" binding:"required"`
Password string `json:"password"`
}
// handle path request
func Path(c *gin.Context) {
var req PathReq
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(200, MetaResponse(400, "Bad Request:"+err.Error()))
return
}
log.Debugf("path:%+v", req)
// find model
dir, name := filepath.Split(req.Path)
file, err := models.GetFileByDirAndName(dir, name)
if err != nil {
// folder model not exist
if file == nil {
c.JSON(200, MetaResponse(404, "path not found."))
return
}
c.JSON(200, MetaResponse(500, err.Error()))
return
}
// check password
if file.Password != "" && file.Password != req.Password {
if req.Password == "" {
c.JSON(200, MetaResponse(401, "need password."))
} else {
c.JSON(200, MetaResponse(401, "wrong password."))
}
return
}
// file
if file.Type == "file" {
c.JSON(200, DataResponse(file))
return
}
// folder
files, err := models.GetFilesByDir(req.Path + "/")
if err != nil {
c.JSON(200, MetaResponse(500, err.Error()))
return
}
// delete password
for i, _ := range *files {
(*files)[i].Password = ""
}
c.JSON(200, DataResponse(files))
}

View File

@@ -1,11 +1,35 @@
package controllers
import "github.com/gin-gonic/gin"
import (
"github.com/Xhofe/alist/server/models"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
type SearchReq struct {
Keyword string `json:"keyword" binding:"required"`
Dir string `json:"dir" binding:"required"`
}
func LocalSearch(c *gin.Context) {
var req SearchReq
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(200, MetaResponse(400, "Bad Request:"+err.Error()))
return
}
log.Debugf("list:%+v", req)
files, err := models.SearchByNameInDir(req.Keyword, req.Dir)
if err != nil {
if files == nil {
c.JSON(200, MetaResponse(404, "Path not found."))
return
}
c.JSON(200, MetaResponse(500, err.Error()))
return
}
c.JSON(200, DataResponse(files))
}
func GlobalSearch(c *gin.Context) {
}
}

View File

@@ -13,6 +13,15 @@ func Info(c *gin.Context) {
// rebuild tree
func RebuildTree(c *gin.Context) {
password := c.Param("password")[1:]
if password != conf.Conf.Server.Password {
if password == "" {
c.JSON(200, MetaResponse(401, "need password."))
return
}
c.JSON(200, MetaResponse(401, "wrong password."))
return
}
if err := models.Clear(); err != nil {
c.JSON(200, MetaResponse(500, err.Error()))
return