Compare commits

...

4 Commits

Author SHA1 Message Date
copilot-swe-agent[bot]
ce2a7f6ae4 Address code review feedback: add validation and improve error handling
Co-authored-by: krau <71133316+krau@users.noreply.github.com>
2026-01-28 09:01:53 +00:00
copilot-swe-agent[bot]
6fbde66415 Address code review feedback: add validation and improve error handling
Co-authored-by: krau <71133316+krau@users.noreply.github.com>
2026-01-28 08:59:56 +00:00
copilot-swe-agent[bot]
943ad190e6 Fix empty filename when Content-Disposition header is missing in /dl command
Add filenameFromURL helper to extract filename from URL path when
Content-Disposition header is empty or not present. This fixes the issue
where direct link downloads fail due to empty filename.

Co-authored-by: krau <71133316+krau@users.noreply.github.com>
2026-01-28 08:57:05 +00:00
copilot-swe-agent[bot]
cfcca79a12 Initial plan 2026-01-28 08:52:23 +00:00
5 changed files with 120 additions and 5 deletions

View File

@@ -45,10 +45,17 @@ func (t *Task) Execute(ctx context.Context) error {
fetchedTotalBytes.Add(resp.ContentLength) fetchedTotalBytes.Add(resp.ContentLength)
file.Size = resp.ContentLength file.Size = resp.ContentLength
if name := resp.Header.Get("Content-Disposition"); name != "" { if name := resp.Header.Get("Content-Disposition"); name != "" {
// Set file name // Set file name from Content-Disposition header
filename := parseFilename(name) filename := parseFilename(name)
file.Name = filename file.Name = filename
} }
// Fallback: extract filename from URL if no filename was determined from Content-Disposition
if file.Name == "" {
file.Name = filenameFromURL(file.URL)
}
if file.Name == "" {
return fmt.Errorf("could not determine filename for %s", file.URL)
}
return nil return nil
}) })

View File

@@ -173,6 +173,35 @@ func parseFilenameFallback(cd string) string {
return decodeFilenameParam(value) 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 { var progressUpdatesLevels = []struct {
size int64 // 文件大小阈值 size int64 // 文件大小阈值
stepPercent int // 每多少 % 更新一次 stepPercent int // 每多少 % 更新一次

View File

@@ -0,0 +1,83 @@
package directlinks
import (
"testing"
)
func TestFilenameFromURL(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",
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",
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: "root path only",
url: "https://example.com/",
expected: "",
},
{
name: "no path",
url: "https://example.com",
expected: "",
},
{
name: "empty url",
url: "",
expected: "",
},
{
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",
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := filenameFromURL(tt.url)
if result != tt.expected {
t.Errorf("filenameFromURL(%q) = %q, expected %q", tt.url, result, tt.expected)
}
})
}
}

View File

@@ -1,5 +1,3 @@
module github.com/krau/SaveAny-Bot/docs module github.com/krau/SaveAny-Bot/docs
go 1.24.4 go 1.24.4
require github.com/alex-shpak/hugo-book v0.0.0-20250530233833-f2c703e15588 // indirect

View File

@@ -1,2 +0,0 @@
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=