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) {
|
go func(mediaInfo MediaInfo) {
|
||||||
rawUrl := mediaInfo.Url
|
rawUrl := mediaInfo.Url
|
||||||
fileName := shared.Md5(rawUrl)
|
fileName := shared.Md5(rawUrl)
|
||||||
|
|
||||||
|
if v := shared.GetFileNameFromURL(rawUrl); v != "" {
|
||||||
|
fileName = v
|
||||||
|
}
|
||||||
|
|
||||||
if mediaInfo.Description != "" {
|
if mediaInfo.Description != "" {
|
||||||
fileName = regexp.MustCompile(`[^\w\p{Han}]`).ReplaceAllString(mediaInfo.Description, "")
|
fileName = regexp.MustCompile(`[^\w\p{Han}]`).ReplaceAllString(mediaInfo.Description, "")
|
||||||
fileLen := globalConfig.FilenameLen
|
fileLen := globalConfig.FilenameLen
|
||||||
@@ -107,9 +112,13 @@ func (r *Resource) download(mediaInfo MediaInfo, decodeStr string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if globalConfig.FilenameTime {
|
if globalConfig.FilenameTime {
|
||||||
mediaInfo.SavePath = filepath.Join(globalConfig.SaveDirectory, fileName+"_"+shared.GetCurrentDateTimeFormatted()+mediaInfo.Suffix)
|
mediaInfo.SavePath = filepath.Join(globalConfig.SaveDirectory, fileName+"_"+shared.GetCurrentDateTimeFormatted())
|
||||||
} else {
|
} 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") {
|
if strings.Contains(rawUrl, "qq.com") {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path"
|
||||||
sysRuntime "runtime"
|
sysRuntime "runtime"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -61,6 +62,14 @@ func IsDevelopment() bool {
|
|||||||
return os.Getenv("APP_ENV") == "development"
|
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 {
|
func GetCurrentDateTimeFormatted() string {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
return fmt.Sprintf("%04d%02d%02d%02d%02d%02d",
|
return fmt.Sprintf("%04d%02d%02d%02d%02d%02d",
|
||||||
|
|||||||
Reference in New Issue
Block a user