30a46ff74b
- service/storage.go: StorageService 提供 Upload/GetDownloadURL/Delete - handler/storage.go: GET /api/v1/assets/download 重定向到 CDN URL - main.go: 注入 StorageService 并注册下载路由
36 lines
868 B
Go
36 lines
868 B
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"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 {
|
|
c.JSON(http.StatusInternalServerError, model.Fail(http.StatusInternalServerError, "生成下载链接失败"))
|
|
return
|
|
}
|
|
|
|
c.Redirect(http.StatusFound, downloadURL)
|
|
}
|