Files
gen2d/backend/internal/handler/storage.go
T

38 lines
991 B
Go
Raw Normal View History

package handler
import (
"net/http"
"gen2d/internal/logger"
"gen2d/internal/model"
"gen2d/internal/service"
"github.com/gin-gonic/gin"
)
var storageSvc *service.StorageService
// InitStorageService 由 main 在启动时调用,注入七牛云存储服务。
func InitStorageService(svc *service.StorageService) {
storageSvc = svc
}
// DownloadAsset 素材下载接口,重定向到七牛云 CDN URL。
// GET /api/v1/assets/download?key=...
func DownloadAsset(c *gin.Context) {
key := c.Query("key")
if key == "" {
c.JSON(http.StatusBadRequest, model.Fail(http.StatusBadRequest, "缺少 key 参数"))
return
}
downloadURL, err := storageSvc.GetDownloadURL(c.Request.Context(), key)
if err != nil {
logger.FromCtx(c.Request.Context()).Error("生成下载链接失败", "key", key, "error", err)
c.JSON(http.StatusInternalServerError, model.Fail(http.StatusInternalServerError, "生成下载链接失败"))
return
}
c.Redirect(http.StatusFound, downloadURL)
}