@@ -2,12 +2,13 @@ package http
22
33import (
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
5051func (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