Files
OpenList/server/controllers/down.go

61 lines
1.4 KiB
Go
Raw Normal View History

2021-03-07 21:02:56 +08:00
package controllers
import (
"github.com/Xhofe/alist/alidrive"
"github.com/Xhofe/alist/server/models"
2021-03-14 19:13:09 +08:00
"github.com/Xhofe/alist/utils"
2021-03-07 21:02:56 +08:00
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"path/filepath"
2021-03-16 21:25:16 +08:00
"strings"
2021-03-07 21:02:56 +08:00
)
type DownReq struct {
Password string `form:"pw"`
}
// handle download request
func Down(c *gin.Context) {
2021-03-08 20:26:02 +08:00
filePath := c.Param("path")[1:]
2021-03-07 21:02:56 +08:00
var down DownReq
if err := c.ShouldBindQuery(&down); err != nil {
c.JSON(200, MetaResponse(400, "Bad Request."))
return
}
log.Debugf("down:%s", filePath)
dir, name := filepath.Split(filePath)
2021-03-08 20:26:02 +08:00
fileModel, err := models.GetFileByDirAndName(dir, name)
2021-03-07 21:02:56 +08:00
if err != nil {
if fileModel == nil {
2021-03-08 20:26:02 +08:00
c.JSON(200, MetaResponse(404, "Path not found."))
2021-03-07 21:02:56 +08:00
return
}
c.JSON(200, MetaResponse(500, err.Error()))
return
}
2021-03-16 16:25:02 +08:00
if fileModel.Password != "" && down.Password != utils.Get16MD5Encode(fileModel.Password) {
2021-03-07 21:02:56 +08:00
if down.Password == "" {
c.JSON(200, MetaResponse(401, "need password."))
} else {
c.JSON(200, MetaResponse(401, "wrong password."))
}
return
}
2021-03-08 20:26:02 +08:00
if fileModel.Type == "folder" {
c.JSON(200, MetaResponse(406, "无法下载目录."))
return
}
2021-03-16 23:15:37 +08:00
drive := utils.GetDriveByName(strings.Split(dir, "/")[0])
2021-03-16 21:25:16 +08:00
if drive == nil {
c.JSON(200, MetaResponse(500, "找不到drive."))
return
}
file, err := alidrive.GetDownLoadUrl(fileModel.FileId, drive)
2021-03-07 21:02:56 +08:00
if err != nil {
c.JSON(200, MetaResponse(500, err.Error()))
return
}
c.Redirect(301, file.Url)
return
}