- 将文件地址前缀与上传逻辑迁移至 common/oss - 恢复执行改用 utils.WithLock 自动续期锁 - 转写提示词增加单镜头最小时长约束
48 lines
1.6 KiB
Go
48 lines
1.6 KiB
Go
package gateway
|
||
|
||
import (
|
||
"context"
|
||
"io"
|
||
"net/http"
|
||
"strings"
|
||
|
||
"gitea.redpowerfuture.com/red-future/common/oss"
|
||
"github.com/gogf/gf/v2/errors/gerror"
|
||
"github.com/gogf/gf/v2/frame/g"
|
||
)
|
||
|
||
// Upload 上传文件字节到 OSS,返回可访问 URL。
|
||
// 原签名基于 workflow/model/dto 的 UploadFileBytesReq/Res,抽离时简化为直接传文件名与字节,便于业务侧解耦 workflow。
|
||
// 统一走 common/oss:multipart field=file、X-User-Info 三态注入(透传请求头 / ctx 注入 user / 解析 token)。
|
||
func Upload(ctx context.Context, fileName string, fileBytes []byte) (string, error) {
|
||
res, err := oss.UploadFileBytes(ctx, fileName, fileBytes)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
return res.FileURL, nil
|
||
}
|
||
|
||
// GetFileBytesFromURL 下载远程文件内容(把 filePrefix 前缀替换为 minioPrefix 后经 GoFrame 客户端下载)
|
||
func GetFileBytesFromURL(ctx context.Context, fileUrl string) ([]byte, error) {
|
||
newS := strings.ReplaceAll(fileUrl, g.Cfg().MustGet(ctx, "filePrefix").String(), g.Cfg().MustGet(ctx, "minioPrefix").String())
|
||
// 使用 GoFrame 客户端(自带超时、追踪、日志等能力)
|
||
resp, err := g.Client().Get(ctx, newS)
|
||
if err != nil {
|
||
return nil, gerror.Wrapf(err, "failed to request url: %s", newS)
|
||
}
|
||
defer resp.Close()
|
||
|
||
// 校验状态码
|
||
if resp.StatusCode != http.StatusOK {
|
||
return nil, gerror.Newf("request failed with status code: %d, url: %s", resp.StatusCode, newS)
|
||
}
|
||
|
||
// 读取全部内容
|
||
allBytes, err := io.ReadAll(resp.Body)
|
||
if err != nil {
|
||
return nil, gerror.Wrapf(err, "failed to read response body, url: %s", fileUrl)
|
||
}
|
||
|
||
return allBytes, nil
|
||
}
|