From 1809847b8aae6d3ad23f7aee03090ee58266a466 Mon Sep 17 00:00:00 2001 From: putyy Date: Thu, 11 Sep 2025 17:03:22 +0800 Subject: [PATCH] perf: optimize file naming during download --- core/resource.go | 13 +++++++++++-- core/shared/utils.go | 9 +++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/core/resource.go b/core/resource.go index cbe115a..48aaff2 100644 --- a/core/resource.go +++ b/core/resource.go @@ -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") { diff --git a/core/shared/utils.go b/core/shared/utils.go index f4d8f30..791e11f 100644 --- a/core/shared/utils.go +++ b/core/shared/utils.go @@ -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",