fix download url expired calc error issue #544 #543 #542

This commit is contained in:
tickstep studio team
2026-06-29 09:29:05 +08:00
parent db28ea6732
commit acc408f0a0
+21 -4
View File
@@ -15,8 +15,6 @@ package downloader
import ( import (
"errors" "errors"
"github.com/tickstep/library-go/logger"
"github.com/tickstep/library-go/requester"
mathrand "math/rand" mathrand "math/rand"
"mime" "mime"
"net/url" "net/url"
@@ -24,6 +22,9 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"time" "time"
"github.com/tickstep/library-go/logger"
"github.com/tickstep/library-go/requester"
) )
var ( var (
@@ -106,8 +107,24 @@ func IsUrlExpired(urlStr string) bool {
if err != nil { if err != nil {
return true return true
} }
expiredTimeSecStr := u.Query().Get("x-oss-expires") // OSS V4签名:x-oss-date=20260629T010316Z&x-oss-expires=900
expiredTimeSec, _ := strconv.ParseInt(expiredTimeSecStr, 10, 64) expiredDateStr := u.Query().Get("x-oss-date") // 该请求/签名生成的时间
expiredTimeSecStr := u.Query().Get("x-oss-expires") // Presigned URL 的过期时间(单位:秒)
// 解析 x-oss-date(ISO8601,UTC)
expiredDate, err := time.Parse("20060102T150405Z", expiredDateStr)
if err != nil {
// 非法时间格式
return true
}
// 解析 x-oss-expires(秒)
expireSec, err := strconv.ParseInt(expiredTimeSecStr, 10, 64)
if err != nil || expireSec <= 0 {
// 非法过期时间
return true
}
// 计算过期时间戳(Unix seconds)
expiredTimeSec := expiredDate.Unix() + expireSec
if (expiredTimeSec - time.Now().Unix()) <= 5 { // 小于5秒钟 if (expiredTimeSec - time.Now().Unix()) <= 5 { // 小于5秒钟
// expired // expired
return true return true