feat(backend): implement download endpoint for shared files with password protection

This commit is contained in:
keven1024
2025-05-11 23:48:42 +08:00
parent 1b2f6a49e2
commit fe4a00aa09
2 changed files with 57 additions and 0 deletions

View 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,
})
}

View File

@@ -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"))
}