mirror of
https://github.com/keven1024/015.git
synced 2026-05-31 17:39:35 +00:00
116 lines
3.2 KiB
Go
116 lines
3.2 KiB
Go
package controllers
|
|
|
|
import (
|
|
"backend/internal/models"
|
|
"backend/internal/services"
|
|
"backend/internal/utils"
|
|
"backend/middleware"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
type DownloadShareClaims struct {
|
|
ShareId string `json:"share_id"`
|
|
jwt.RegisteredClaims
|
|
}
|
|
|
|
func DownloadShare(c echo.Context) error {
|
|
cc := c.(*middleware.CustomContext)
|
|
token := cc.FormValue("token")
|
|
if token == "" {
|
|
return utils.HTTPErrorHandler(c, errors.New("缺少token"))
|
|
}
|
|
claims := DownloadShareClaims{}
|
|
t, err := jwt.ParseWithClaims(token, &claims, func(token *jwt.Token) (interface{}, error) {
|
|
return []byte(utils.GetEnv("download_secret")), nil
|
|
})
|
|
if err != nil {
|
|
return utils.HTTPErrorHandler(c, err)
|
|
}
|
|
if !t.Valid {
|
|
return utils.HTTPErrorHandler(c, errors.New("token格式错误"))
|
|
}
|
|
shareInfo, _ := models.GetRedisShareInfo(claims.ShareId)
|
|
|
|
if shareInfo.Type == models.ShareTypeFile {
|
|
fileInfo, _ := models.GetRedisFileInfo(shareInfo.Data)
|
|
uploadPath, err := services.GetUploadDirPath()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
models.SetRedisShareInfo(claims.ShareId, models.RedisShareInfo{
|
|
ViewNum: shareInfo.ViewNum - 1,
|
|
})
|
|
return cc.Attachment(fmt.Sprintf("%s/%s", uploadPath, utils.GetFileId(fileInfo.FileHash, fileInfo.FileSize)), shareInfo.FileName)
|
|
}
|
|
models.SetRedisShareInfo(claims.ShareId, models.RedisShareInfo{
|
|
ViewNum: shareInfo.ViewNum - 1,
|
|
})
|
|
return utils.HTTPSuccessHandler(c, map[string]any{
|
|
"data": shareInfo.Data,
|
|
})
|
|
}
|
|
|
|
type VaildateShareProps struct {
|
|
ShareId string `json:"share_id"`
|
|
Password string `json:"password"`
|
|
}
|
|
|
|
func VaildateShare(c echo.Context) error {
|
|
cc := c.(*middleware.CustomContext)
|
|
|
|
r := new(VaildateShareProps)
|
|
if err := cc.Bind(r); err != nil {
|
|
return utils.HTTPErrorHandler(c, err)
|
|
}
|
|
|
|
if r.ShareId == "" {
|
|
return utils.HTTPErrorHandler(c, errors.New("缺少分享ID"))
|
|
}
|
|
|
|
shareInfo, err := models.GetRedisShareInfo(r.ShareId)
|
|
if err != nil {
|
|
return utils.HTTPErrorHandler(c, err)
|
|
}
|
|
if shareInfo == nil {
|
|
return utils.HTTPErrorHandler(c, errors.New("分享不存在"))
|
|
}
|
|
if shareInfo.Password != "" && shareInfo.Password != r.Password {
|
|
return utils.HTTPErrorHandler(c, errors.New("分享密码错误"))
|
|
}
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, DownloadShareClaims{
|
|
ShareId: r.ShareId,
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
|
ExpiresAt: jwt.NewNumericDate(time.Now().Add(60 * time.Minute)),
|
|
},
|
|
})
|
|
|
|
// Sign and get the complete encoded token as a string using the secret
|
|
downloadToken, err := token.SignedString([]byte(utils.GetEnv("download_secret")))
|
|
if err != nil {
|
|
return utils.HTTPErrorHandler(c, err)
|
|
}
|
|
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("分享文件状态错误"))
|
|
}
|
|
return utils.HTTPSuccessHandler(c, map[string]any{
|
|
"token": downloadToken,
|
|
})
|
|
}
|
|
return utils.HTTPSuccessHandler(c, map[string]any{
|
|
"token": downloadToken,
|
|
})
|
|
}
|