Compare commits
2 Commits
copilot/fi
...
v0.53.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a02e8a8d90 | ||
|
|
4d2c345003 |
@@ -45,16 +45,17 @@ func (t *Task) Execute(ctx context.Context) error {
|
||||
fetchedTotalBytes.Add(resp.ContentLength)
|
||||
file.Size = resp.ContentLength
|
||||
if name := resp.Header.Get("Content-Disposition"); name != "" {
|
||||
// Set file name from Content-Disposition header
|
||||
filename := parseFilename(name)
|
||||
file.Name = filename
|
||||
if filename != "" {
|
||||
file.Name = filename
|
||||
}
|
||||
}
|
||||
// Fallback: extract filename from URL if no filename was determined from Content-Disposition
|
||||
// extract filename from URL if Content-Disposition is empty or invalid
|
||||
if file.Name == "" {
|
||||
file.Name = filenameFromURL(file.URL)
|
||||
file.Name = parseFilenameFromURL(file.URL)
|
||||
}
|
||||
if file.Name == "" {
|
||||
return fmt.Errorf("could not determine filename for %s", file.URL)
|
||||
return fmt.Errorf("failed to determine filename for %s: Content-Disposition header is empty and URL does not contain a valid filename", file.URL)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -144,6 +144,41 @@ func tryDecodeGBK(s string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// parseFilenameFromURL extracts filename from URL path
|
||||
// This is used as a fallback when Content-Disposition is not available
|
||||
func parseFilenameFromURL(rawURL string) string {
|
||||
parsed, err := url.Parse(rawURL)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Get the path part and extract the last segment
|
||||
path := parsed.Path
|
||||
if path == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
// URL decode the path first
|
||||
decodedPath, err := url.PathUnescape(path)
|
||||
if err != nil {
|
||||
decodedPath = path
|
||||
}
|
||||
|
||||
// Get the last segment of the path
|
||||
lastSlash := strings.LastIndex(decodedPath, "/")
|
||||
if lastSlash == -1 {
|
||||
return decodedPath
|
||||
}
|
||||
filename := decodedPath[lastSlash+1:]
|
||||
|
||||
// Remove query string if somehow still present
|
||||
if idx := strings.Index(filename, "?"); idx != -1 {
|
||||
filename = filename[:idx]
|
||||
}
|
||||
|
||||
return filename
|
||||
}
|
||||
|
||||
// parseFilenameFallback manually parses filename= when mime.ParseMediaType fails
|
||||
func parseFilenameFallback(cd string) string {
|
||||
// Look for filename= (case-insensitive)
|
||||
@@ -173,35 +208,6 @@ func parseFilenameFallback(cd string) string {
|
||||
return decodeFilenameParam(value)
|
||||
}
|
||||
|
||||
// filenameFromURL extracts filename from a URL path.
|
||||
// It uses the last path segment and removes any query parameters.
|
||||
// Returns empty string if the URL cannot be parsed or has no valid path.
|
||||
func filenameFromURL(rawURL string) string {
|
||||
u, err := url.Parse(rawURL)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Get the path and extract the base name
|
||||
path := u.Path
|
||||
if path == "" || path == "/" {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Find the last path segment
|
||||
idx := strings.LastIndex(path, "/")
|
||||
if idx >= 0 && idx < len(path)-1 {
|
||||
filename := path[idx+1:]
|
||||
// URL decode the filename
|
||||
if decoded, err := url.QueryUnescape(filename); err == nil {
|
||||
return decoded
|
||||
}
|
||||
return filename
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
var progressUpdatesLevels = []struct {
|
||||
size int64 // 文件大小阈值
|
||||
stepPercent int // 每多少 % 更新一次
|
||||
|
||||
@@ -4,41 +4,41 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFilenameFromURL(t *testing.T) {
|
||||
func TestParseFilenameFromURL(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
url string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "simple file",
|
||||
url: "https://example.com/file.zip",
|
||||
expected: "file.zip",
|
||||
},
|
||||
{
|
||||
name: "file with path",
|
||||
url: "https://example.com/path/to/document.pdf",
|
||||
name: "simple filename",
|
||||
url: "https://example.com/files/document.pdf",
|
||||
expected: "document.pdf",
|
||||
},
|
||||
{
|
||||
name: "url with query params",
|
||||
url: "https://example.com/file.mp4?token=abc123",
|
||||
expected: "file.mp4",
|
||||
},
|
||||
{
|
||||
name: "url with fragment",
|
||||
url: "https://example.com/file.txt#section",
|
||||
expected: "file.txt",
|
||||
},
|
||||
{
|
||||
name: "url encoded filename",
|
||||
url: "https://example.com/%E6%B5%8B%E8%AF%95.zip",
|
||||
name: "filename with encoded characters",
|
||||
url: "https://example.com/files/%E6%B5%8B%E8%AF%95.zip",
|
||||
expected: "测试.zip",
|
||||
},
|
||||
{
|
||||
name: "url encoded Chinese filename",
|
||||
url: "https://example.com/10%E6%9C%8817%E6%97%A5(6).mp4",
|
||||
expected: "10月17日(6).mp4",
|
||||
name: "filename with query string in URL",
|
||||
url: "https://example.com/files/image.png?token=abc123",
|
||||
expected: "image.png",
|
||||
},
|
||||
{
|
||||
name: "nested path",
|
||||
url: "https://example.com/a/b/c/file.txt",
|
||||
expected: "file.txt",
|
||||
},
|
||||
{
|
||||
name: "URL with port",
|
||||
url: "https://example.com:8080/downloads/archive.tar.gz",
|
||||
expected: "archive.tar.gz",
|
||||
},
|
||||
{
|
||||
name: "empty path",
|
||||
url: "https://example.com",
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
name: "root path only",
|
||||
@@ -46,37 +46,27 @@ func TestFilenameFromURL(t *testing.T) {
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
name: "no path",
|
||||
url: "https://example.com",
|
||||
expected: "",
|
||||
name: "filename with spaces encoded",
|
||||
url: "https://example.com/my%20file%20name.pdf",
|
||||
expected: "my file name.pdf",
|
||||
},
|
||||
{
|
||||
name: "empty url",
|
||||
url: "",
|
||||
expected: "",
|
||||
name: "complex encoded filename",
|
||||
url: "https://example.com/downloads/%E4%B8%AD%E6%96%87%E6%96%87%E4%BB%B6.docx",
|
||||
expected: "中文文件.docx",
|
||||
},
|
||||
{
|
||||
name: "file with spaces encoded",
|
||||
url: "https://example.com/my%20file.txt",
|
||||
expected: "my file.txt",
|
||||
},
|
||||
{
|
||||
name: "complex path with multiple slashes",
|
||||
url: "https://cdn.example.com/a/b/c/d/e/video.mkv",
|
||||
expected: "video.mkv",
|
||||
},
|
||||
{
|
||||
name: "malformed url with invalid characters",
|
||||
url: "://invalid url",
|
||||
name: "invalid URL",
|
||||
url: "://invalid-url",
|
||||
expected: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := filenameFromURL(tt.url)
|
||||
result := parseFilenameFromURL(tt.url)
|
||||
if result != tt.expected {
|
||||
t.Errorf("filenameFromURL(%q) = %q, expected %q", tt.url, result, tt.expected)
|
||||
t.Errorf("parseFilenameFromURL(%q) = %q, want %q", tt.url, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
module github.com/krau/SaveAny-Bot/docs
|
||||
|
||||
go 1.24.4
|
||||
|
||||
require github.com/alex-shpak/hugo-book v0.0.0-20250530233833-f2c703e15588 // indirect
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
github.com/alex-shpak/hugo-book v0.0.0-20250530233833-f2c703e15588 h1:pwxkzpzw/iJSxMBgQLWjYMQubhIemLG3UrNjeWoCkSM=
|
||||
github.com/alex-shpak/hugo-book v0.0.0-20250530233833-f2c703e15588/go.mod h1:L4NMyzbn15fpLIpmmtDg9ZFFyTZzw87/lk7M2bMQ7ds=
|
||||
|
||||
@@ -104,7 +104,7 @@ func (a *Alist) Name() string {
|
||||
|
||||
func (a *Alist) Save(ctx context.Context, reader io.Reader, storagePath string) error {
|
||||
a.logger.Infof("Saving file to %s", storagePath)
|
||||
|
||||
storagePath = a.JoinStoragePath(storagePath)
|
||||
ext := path.Ext(storagePath)
|
||||
base := strings.TrimSuffix(storagePath, ext)
|
||||
candidate := storagePath
|
||||
|
||||
Reference in New Issue
Block a user