mirror of
https://github.com/keven1024/015.git
synced 2026-05-26 07:08:02 +00:00
feat(backend): implement download endpoint for shared files with password protection
This commit is contained in:
55
backend/internal/controllers/download.go
Normal file
55
backend/internal/controllers/download.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"backend/internal/models"
|
||||
"backend/internal/services"
|
||||
"backend/internal/utils"
|
||||
"backend/middleware"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func DownloadShare(c echo.Context) error {
|
||||
cc := c.(*middleware.CustomContext)
|
||||
shareId := cc.Param("id")
|
||||
password := cc.QueryParam("password")
|
||||
|
||||
if shareId == "" {
|
||||
return utils.HTTPErrorHandler(c, errors.New("缺少分享ID"))
|
||||
}
|
||||
|
||||
shareInfo, err := models.GetRedisShareInfo(shareId)
|
||||
if err != nil {
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
if shareInfo == nil {
|
||||
return utils.HTTPErrorHandler(c, errors.New("分享不存在"))
|
||||
}
|
||||
if shareInfo.Password != "" && shareInfo.Password != password {
|
||||
return utils.HTTPErrorHandler(c, errors.New("分享密码错误"))
|
||||
}
|
||||
|
||||
if shareInfo.Type == models.ShareTypeFile {
|
||||
fileInfo, err := models.GetRedisFileInfo(shareInfo.Data)
|
||||
if err != nil {
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
if fileInfo == nil {
|
||||
return utils.HTTPErrorHandler(c, errors.New("分享文件不存在"))
|
||||
}
|
||||
if fileInfo.FileType != models.FileTypeUpload {
|
||||
return utils.HTTPErrorHandler(c, errors.New("分享文件状态错误"))
|
||||
}
|
||||
uploadPath, err := services.GetUploadDirPath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return cc.Attachment(fmt.Sprintf("%s/%s", uploadPath, utils.GetFileId(fileInfo.FileHash, fileInfo.FileSize)), shareInfo.FileName)
|
||||
}
|
||||
|
||||
return utils.HTTPSuccessHandler(c, map[string]any{
|
||||
"data": shareInfo.Data,
|
||||
})
|
||||
}
|
||||
@@ -21,5 +21,7 @@ func main() {
|
||||
e.POST("/share", controllers.CreateShareInfo)
|
||||
e.GET("/config", controllers.GetConfig)
|
||||
|
||||
e.GET("/download/:id", controllers.DownloadShare)
|
||||
|
||||
e.Logger.Fatal(e.Start(":1323"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user