Files
2026-09-11 01:47:20 +08:00

36 lines
997 B
Go

// Package cos 提供 COS 下载 URL 生成(CDN 鉴权签名)。
package cos
import (
"crypto/md5"
"fmt"
"strconv"
"time"
"git.lzcwy.cn/zhengchiliu/zt-closet-manage-backend/global"
)
// GetCosUrl 将 COS 相对 key 转为带 CDN 鉴权签名的完整 URL(复刻业务服务 upload.GetCosUrl)。
//
// 签名规则:sign = md5(CdnDownloadSecretKey + "/" + fileName + ts)。
// 保护:fileName 为空、Cos 配置缺失或下载域名为空时,原样返回 fileName,避免未配置时 panic 或生成无效 URL。
func GetCosUrl(fileName string) string {
if fileName == "" {
return ""
}
if global.CosSetting == nil || global.CosSetting.CosDownloadDomain == "" {
return fileName
}
ts := time.Now().Unix()
raw := global.CosSetting.CdnDownloadSecretKey + "/" + fileName + strconv.FormatInt(ts, 10)
sign := fmt.Sprintf("%x", md5.Sum([]byte(raw)))
return fmt.Sprintf("https://%s/%s?sign=%s&t=%d",
global.CosSetting.CosDownloadDomain,
fileName,
sign,
ts,
)
}