mirror of
https://github.com/putyy/res-downloader.git
synced 2026-01-12 06:04:55 +08:00
fix: windows file naming during download
This commit is contained in:
@@ -14,7 +14,7 @@
|
||||
!define INFO_PRODUCTNAME "res-downloader"
|
||||
!endif
|
||||
!ifndef INFO_PRODUCTVERSION
|
||||
!define INFO_PRODUCTVERSION "3.1.0"
|
||||
!define INFO_PRODUCTVERSION "3.1.1"
|
||||
!endif
|
||||
!ifndef INFO_COPYRIGHT
|
||||
!define INFO_COPYRIGHT "Copyright © 2023"
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
sysRuntime "runtime"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -66,12 +67,43 @@ func IsDevelopment() bool {
|
||||
|
||||
func GetFileNameFromURL(rawUrl string) string {
|
||||
parsedURL, err := url.Parse(rawUrl)
|
||||
if err == nil {
|
||||
return path.Base(parsedURL.Path)
|
||||
}
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
fileName := path.Base(parsedURL.Path)
|
||||
if fileName == "" || fileName == "/" {
|
||||
return ""
|
||||
}
|
||||
|
||||
if decoded, err := url.QueryUnescape(fileName); err == nil {
|
||||
fileName = decoded
|
||||
}
|
||||
|
||||
re := regexp.MustCompile(`[<>:"/\\|?*]`)
|
||||
fileName = re.ReplaceAllString(fileName, "_")
|
||||
|
||||
fileName = strings.TrimRightFunc(fileName, func(r rune) bool {
|
||||
return r == '.' || r == ' '
|
||||
})
|
||||
|
||||
const maxFileNameLen = 255
|
||||
runes := []rune(fileName)
|
||||
if len(runes) > maxFileNameLen {
|
||||
ext := path.Ext(fileName)
|
||||
name := strings.TrimSuffix(fileName, ext)
|
||||
|
||||
runes = []rune(name)
|
||||
if len(runes) > maxFileNameLen-len(ext) {
|
||||
runes = runes[:maxFileNameLen-len(ext)]
|
||||
}
|
||||
name = string(runes)
|
||||
fileName = name + ext
|
||||
}
|
||||
|
||||
return fileName
|
||||
}
|
||||
|
||||
func GetCurrentDateTimeFormatted() string {
|
||||
now := time.Now()
|
||||
return fmt.Sprintf("%04d%02d%02d%02d%02d%02d",
|
||||
|
||||
Reference in New Issue
Block a user