From 6934bba7a1f5876ec3d7be07d1e9baccd83fbd75 Mon Sep 17 00:00:00 2001 From: keven1024 Date: Thu, 30 Apr 2026 08:28:05 +0800 Subject: [PATCH] feat(backend): enhance share creation with notification settings and webhook support --- backend/internal/controllers/download.go | 9 +++++ backend/internal/controllers/share.go | 42 ++++++++++++++++++++---- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/backend/internal/controllers/download.go b/backend/internal/controllers/download.go index f1f4624..00b8d9d 100644 --- a/backend/internal/controllers/download.go +++ b/backend/internal/controllers/download.go @@ -3,12 +3,14 @@ package controllers import ( "backend/internal/utils" "context" + "encoding/json" "fmt" "pkg/models" u "pkg/utils" "time" "github.com/golang-jwt/jwt/v5" + "github.com/hibiken/asynq" "github.com/labstack/echo/v5" "github.com/samber/lo" "github.com/spf13/cast" @@ -134,6 +136,13 @@ func VaildateShare(c *echo.Context) error { return utils.HTTPErrorHandler(c, err) } + if len(shareInfo.NotifyEmails) > 0 || len(shareInfo.NotifyWebhooks) > 0 { + payload, err := json.Marshal(map[string]string{"share_id": r.ShareId}) + if err == nil { + _, _ = u.GetQueueClient().Enqueue(asynq.NewTask("share:notify", payload)) + } + } + if shareInfo.Type == models.ShareTypeFile { return utils.HTTPSuccessHandler(c, map[string]any{ "token": downloadToken, diff --git a/backend/internal/controllers/share.go b/backend/internal/controllers/share.go index 47ac970..f2d0219 100644 --- a/backend/internal/controllers/share.go +++ b/backend/internal/controllers/share.go @@ -23,13 +23,16 @@ type CreateShareProps struct { } type ShareConfig struct { - ExpireAt int `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"` + ExpireAt int `json:"expire_time"` // 分钟 + ViewNum int64 `json:"download_nums"` + HasPassword bool `json:"has_password"` + Password string `json:"password"` + HasNotify bool `json:"has_notify"` + NotifyTypes []string `json:"notify_types"` + NotifyEmails []string `json:"notify_emails"` + NotifyWebhooks []models.NotifyWebhook `json:"notify_webhooks"` + Locale string `json:"locale"` + HasPickupCode bool `json:"has_pickup_code"` } func CreateShareInfo(c *echo.Context) error { @@ -73,6 +76,28 @@ func CreateShareInfo(c *echo.Context) error { password = hash } + var notifyEmails []string + var notifyWebhooks []models.NotifyWebhook + if r.Config.HasNotify { + hasEmail, hasWebhook := false, false + for _, nt := range r.Config.NotifyTypes { + switch nt { + case "email": + hasEmail = true + case "webhook": + hasWebhook = true + default: + return utils.HTTPErrorHandler(c, ErrInvalidRequest) + } + } + if hasEmail { + notifyEmails = r.Config.NotifyEmails + } + if hasWebhook { + notifyWebhooks = r.Config.NotifyWebhooks + } + } + _, err = models.SetRedisShareInfo(id, func(shareInfo *models.RedisShareInfo) *models.RedisShareInfo { shareInfo.Data = r.Data shareInfo.Type = r.Type @@ -81,6 +106,9 @@ func CreateShareInfo(c *echo.Context) error { shareInfo.ViewNum = r.Config.ViewNum shareInfo.Password = password shareInfo.FileName = r.FileName + shareInfo.NotifyEmails = notifyEmails + shareInfo.NotifyWebhooks = notifyWebhooks + shareInfo.Locale = r.Config.Locale shareInfo.ExpireAt = ExpireTime.Unix() return shareInfo })