mirror of
https://github.com/keven1024/015.git
synced 2026-05-26 07:08:02 +00:00
feat(backend): add pickup code generation and retrieval functionality for file sharing
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"backend/middleware"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/hibiken/asynq"
|
||||
@@ -75,6 +76,11 @@ func CreateShareInfo(c echo.Context) error {
|
||||
FileName: r.FileName,
|
||||
ExpireAt: ExpireTime.Unix(),
|
||||
})
|
||||
var pickupCode string
|
||||
if r.Config.HasPickupCode {
|
||||
pickupCode = utils.GeneratePickupCode()
|
||||
models.SetRedisPickupData(pickupCode, id)
|
||||
}
|
||||
|
||||
if r.Type == models.ShareTypeFile {
|
||||
shareIDs, err := models.GetRedisFileShareRelational(r.Data)
|
||||
@@ -99,6 +105,7 @@ func CreateShareInfo(c echo.Context) error {
|
||||
"file_name": r.FileName,
|
||||
"download_nums": r.Config.ViewNum,
|
||||
"expire_at": ExpireTime.Unix(),
|
||||
"pickup_code": pickupCode,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -153,3 +160,21 @@ func GetShareInfo(c echo.Context) error {
|
||||
"owner": shareInfo.Owner,
|
||||
})
|
||||
}
|
||||
|
||||
func GetShareByPickupCode(c echo.Context) error {
|
||||
cc := c.(*middleware.CustomContext)
|
||||
pickupCode := cc.Param("code")
|
||||
if pickupCode == "" {
|
||||
return utils.HTTPErrorHandler(c, errors.New("缺少提取码"))
|
||||
}
|
||||
shareId, err := models.GetRedisPickupData(strings.ToUpper(pickupCode))
|
||||
if err != nil {
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
if shareId == "" {
|
||||
return utils.HTTPErrorHandler(c, errors.New("分享不存在"))
|
||||
}
|
||||
return utils.HTTPSuccessHandler(c, map[string]any{
|
||||
"share_id": shareId,
|
||||
})
|
||||
}
|
||||
|
||||
27
backend/internal/models/pickupcode.go
Normal file
27
backend/internal/models/pickupcode.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"backend/internal/utils"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func GetRedisPickupData(pickupCode string) (string, error) {
|
||||
rdb, ctx := utils.GetRedisClient()
|
||||
ShareId, err := rdb.Get(ctx, fmt.Sprintf("015:pickupCode:%s", pickupCode)).Result()
|
||||
if err == redis.Nil {
|
||||
return "", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return ShareId, nil
|
||||
}
|
||||
|
||||
func SetRedisPickupData(pickupCode string, shareId string) error {
|
||||
rdb, ctx := utils.GetRedisClient()
|
||||
_, err := rdb.Set(ctx, fmt.Sprintf("015:pickupCode:%s", pickupCode), shareId, time.Until(time.Now().Add(24*time.Hour))).Result()
|
||||
return err
|
||||
}
|
||||
15
backend/internal/utils/pickupcode.go
Normal file
15
backend/internal/utils/pickupcode.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base32"
|
||||
)
|
||||
|
||||
func GeneratePickupCode() string {
|
||||
bytes := make([]byte, 4)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
encoding := base32.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567")
|
||||
return encoding.EncodeToString(bytes)[:4]
|
||||
}
|
||||
@@ -34,6 +34,7 @@ func main() {
|
||||
e.POST("/share", controllers.CreateShareInfo)
|
||||
e.GET("/download", controllers.DownloadShare)
|
||||
e.POST("/download", controllers.VaildateShare)
|
||||
e.GET("/share/pickup/:code", controllers.GetShareByPickupCode)
|
||||
|
||||
e.POST("/image/compress", controllers.GenCompressImage)
|
||||
e.GET("/image/compress/:id", controllers.GetCompressImage)
|
||||
|
||||
Reference in New Issue
Block a user