🚧 合并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()))