Skip to content

Commit 74d32fd

Browse files
authored
fix(simplehttp): logic bug when unable to parse file name (#761)
1 parent cedb3d4 commit 74d32fd

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

internal/offline_download/http/client.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package http
22

33
import (
44
"fmt"
5+
"math/rand/v2"
56
"net/http"
6-
"net/url"
77
"os"
88
"path"
99
"path/filepath"
1010
"strings"
11+
"time"
1112

1213
"github.com/OpenListTeam/OpenList/v4/internal/model"
1314
"github.com/OpenListTeam/OpenList/v4/internal/offline_download/tool"
@@ -48,18 +49,12 @@ func (s SimpleHttp) Status(task *tool.DownloadTask) (*tool.Status, error) {
4849
}
4950

5051
func (s SimpleHttp) Run(task *tool.DownloadTask) error {
51-
u := task.Url
52-
// parse url
53-
_u, err := url.Parse(u)
54-
if err != nil {
55-
return err
56-
}
5752
streamPut := task.DeletePolicy == tool.UploadDownloadStream
5853
method := http.MethodGet
5954
if streamPut {
6055
method = http.MethodHead
6156
}
62-
req, err := http.NewRequestWithContext(task.Ctx(), method, u, nil)
57+
req, err := http.NewRequestWithContext(task.Ctx(), method, task.Url, nil)
6358
if err != nil {
6459
return err
6560
}
@@ -74,14 +69,13 @@ func (s SimpleHttp) Run(task *tool.DownloadTask) error {
7469
if resp.StatusCode >= 400 {
7570
return fmt.Errorf("http status code %d", resp.StatusCode)
7671
}
77-
// If Path is empty, use Hostname; otherwise, filePath euqals TempDir which causes os.Create to fail
78-
urlPath := _u.Path
79-
if urlPath == "" {
80-
urlPath = strings.ReplaceAll(_u.Host, ".", "_")
72+
filename, err := parseFilenameFromContentDisposition(resp.Header.Get("Content-Disposition"))
73+
if err != nil {
74+
filename = path.Base(resp.Request.URL.Path)
8175
}
82-
filename := path.Base(urlPath)
83-
if n, err := parseFilenameFromContentDisposition(resp.Header.Get("Content-Disposition")); err == nil {
84-
filename = n
76+
filename = strings.Trim(filename, "/")
77+
if len(filename) == 0 {
78+
filename = fmt.Sprintf("%s-%d-%x", strings.ReplaceAll(req.URL.Host, ".", "_"), time.Now().UnixMilli(), rand.Uint32())
8579
}
8680
fileSize := resp.ContentLength
8781
if streamPut {

0 commit comments

Comments
 (0)