mirror of
https://github.com/putyy/res-downloader.git
synced 2026-01-12 06:04:55 +08:00
perf: optimize file naming during download
This commit is contained in:
@@ -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") {
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user