perf: optimize file naming during download

This commit is contained in:
putyy
2025-09-11 17:03:22 +08:00
committed by putyy
parent da8e8d9641
commit 1809847b8a
2 changed files with 20 additions and 2 deletions

View File

@@ -93,6 +93,11 @@ func (r *Resource) download(mediaInfo MediaInfo, decodeStr string) {
go func(mediaInfo MediaInfo) {
rawUrl := mediaInfo.Url
fileName := shared.Md5(rawUrl)
if v := shared.GetFileNameFromURL(rawUrl); v != "" {
fileName = v
}
if mediaInfo.Description != "" {
fileName = regexp.MustCompile(`[^\w\p{Han}]`).ReplaceAllString(mediaInfo.Description, "")
fileLen := globalConfig.FilenameLen
@@ -107,9 +112,13 @@ func (r *Resource) download(mediaInfo MediaInfo, decodeStr string) {
}
if globalConfig.FilenameTime {
mediaInfo.SavePath = filepath.Join(globalConfig.SaveDirectory, fileName+"_"+shared.GetCurrentDateTimeFormatted()+mediaInfo.Suffix)
mediaInfo.SavePath = filepath.Join(globalConfig.SaveDirectory, fileName+"_"+shared.GetCurrentDateTimeFormatted())
} else {
mediaInfo.SavePath = filepath.Join(globalConfig.SaveDirectory, fileName+mediaInfo.Suffix)
mediaInfo.SavePath = filepath.Join(globalConfig.SaveDirectory, fileName)
}
if !strings.HasSuffix(mediaInfo.SavePath, mediaInfo.Suffix) {
mediaInfo.SavePath = mediaInfo.SavePath + mediaInfo.Suffix
}
if strings.Contains(rawUrl, "qq.com") {

View File

@@ -9,6 +9,7 @@ import (
"net/url"
"os"
"os/exec"
"path"
sysRuntime "runtime"
"time"
)
@@ -61,6 +62,14 @@ func IsDevelopment() bool {
return os.Getenv("APP_ENV") == "development"
}
func GetFileNameFromURL(rawUrl string) string {
parsedURL, err := url.Parse(rawUrl)
if err == nil {
return path.Base(parsedURL.Path)
}
return ""
}
func GetCurrentDateTimeFormatted() string {
now := time.Now()
return fmt.Sprintf("%04d%02d%02d%02d%02d%02d",