mirror of
https://github.com/keven1024/015.git
synced 2026-05-26 07:08:02 +00:00
136 lines
3.7 KiB
Go
136 lines
3.7 KiB
Go
package controllers
|
|
|
|
import (
|
|
"backend/internal/models"
|
|
"backend/internal/utils"
|
|
"backend/middleware"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
gonanoid "github.com/matoous/go-nanoid/v2"
|
|
)
|
|
|
|
type CreateShareProps struct {
|
|
Type models.ShareType `json:"type"`
|
|
// ShareId string `json:"id"`
|
|
Config ShareConfig `json:"config"`
|
|
Data string `json:"data"`
|
|
FileName string `json:"file_name"`
|
|
}
|
|
|
|
type ShareConfig struct {
|
|
ExpireAt string `json:"expire_time"`
|
|
ViewNum int64 `json:"download_nums"`
|
|
HasPassword bool `json:"has_password"`
|
|
Password string `json:"password"`
|
|
HasNotify bool `json:"has_notify"`
|
|
NotifyEmail []string `json:"notify_email"`
|
|
HasPickupCode bool `json:"has_pickup_code"`
|
|
}
|
|
|
|
func CreateShareInfo(c echo.Context) error {
|
|
cc := c.(*middleware.CustomContext)
|
|
|
|
r := new(CreateShareProps)
|
|
if err := cc.Bind(r); err != nil {
|
|
return utils.HTTPErrorHandler(c, err)
|
|
}
|
|
|
|
ExpireTime, err := time.Parse(time.RFC3339, r.Config.ExpireAt)
|
|
if err != nil {
|
|
return utils.HTTPErrorHandler(c, errors.New("分享过期时间格式错误"))
|
|
}
|
|
|
|
if r.Data == "" || (r.Type != models.ShareTypeFile && r.Type != models.ShareTypeText) || ExpireTime.Before(time.Now()) || r.Config.ViewNum < 1 {
|
|
return utils.HTTPErrorHandler(c, errors.New("调用接口参数错误"))
|
|
}
|
|
|
|
id, err := gonanoid.New()
|
|
if err != nil {
|
|
return utils.HTTPErrorHandler(c, err)
|
|
}
|
|
|
|
if r.Type == models.ShareTypeFile {
|
|
fileInfo, err := models.GetRedisFileInfo(r.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("分享文件状态错误"))
|
|
}
|
|
}
|
|
|
|
models.SetRedisShareInfo(id, models.RedisShareInfo{
|
|
Data: r.Data,
|
|
Type: r.Type,
|
|
CreatedAt: time.Now().Unix(),
|
|
Owner: cc.Auth.(string),
|
|
// ExpireAt: r.Config.ExpireAt,
|
|
ViewNum: r.Config.ViewNum,
|
|
Password: r.Config.Password,
|
|
NotifyEmail: r.Config.NotifyEmail,
|
|
FileName: r.FileName,
|
|
}, time.Until(ExpireTime))
|
|
|
|
return utils.HTTPSuccessHandler(c, map[string]any{
|
|
"id": id,
|
|
"file_name": r.FileName,
|
|
"download_nums": r.Config.ViewNum,
|
|
"expire_at": ExpireTime.Unix(),
|
|
})
|
|
}
|
|
|
|
type GetShareProps struct {
|
|
ShareId string `param:"id"`
|
|
}
|
|
|
|
func GetShareInfo(c echo.Context) error {
|
|
cc := c.(*middleware.CustomContext)
|
|
shareId := cc.Param("id")
|
|
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.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{
|
|
"id": shareId,
|
|
"file_name": shareInfo.FileName,
|
|
"download_nums": shareInfo.ViewNum,
|
|
"expire_at": shareInfo.ExpireAt,
|
|
"owner": shareInfo.Owner,
|
|
"size": fileInfo.FileSize,
|
|
"mime_type": fileInfo.MimeType,
|
|
})
|
|
}
|
|
|
|
return utils.HTTPSuccessHandler(c, map[string]any{
|
|
"id": shareId,
|
|
"file_name": shareInfo.FileName,
|
|
"download_nums": shareInfo.ViewNum,
|
|
"expire_at": shareInfo.ExpireAt,
|
|
"owner": shareInfo.Owner,
|
|
})
|
|
}
|