mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2025-09-21 05:06:10 +08:00
fix(simplehttp): logic bug when unable to parse file name (#761)
This commit is contained in:
@ -2,12 +2,13 @@ package http
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/rand/v2"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/OpenListTeam/OpenList/v4/internal/model"
|
"github.com/OpenListTeam/OpenList/v4/internal/model"
|
||||||
"github.com/OpenListTeam/OpenList/v4/internal/offline_download/tool"
|
"github.com/OpenListTeam/OpenList/v4/internal/offline_download/tool"
|
||||||
@ -48,18 +49,12 @@ func (s SimpleHttp) Status(task *tool.DownloadTask) (*tool.Status, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s SimpleHttp) Run(task *tool.DownloadTask) error {
|
func (s SimpleHttp) Run(task *tool.DownloadTask) error {
|
||||||
u := task.Url
|
|
||||||
// parse url
|
|
||||||
_u, err := url.Parse(u)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
streamPut := task.DeletePolicy == tool.UploadDownloadStream
|
streamPut := task.DeletePolicy == tool.UploadDownloadStream
|
||||||
method := http.MethodGet
|
method := http.MethodGet
|
||||||
if streamPut {
|
if streamPut {
|
||||||
method = http.MethodHead
|
method = http.MethodHead
|
||||||
}
|
}
|
||||||
req, err := http.NewRequestWithContext(task.Ctx(), method, u, nil)
|
req, err := http.NewRequestWithContext(task.Ctx(), method, task.Url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -74,14 +69,13 @@ func (s SimpleHttp) Run(task *tool.DownloadTask) error {
|
|||||||
if resp.StatusCode >= 400 {
|
if resp.StatusCode >= 400 {
|
||||||
return fmt.Errorf("http status code %d", resp.StatusCode)
|
return fmt.Errorf("http status code %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
// If Path is empty, use Hostname; otherwise, filePath euqals TempDir which causes os.Create to fail
|
filename, err := parseFilenameFromContentDisposition(resp.Header.Get("Content-Disposition"))
|
||||||
urlPath := _u.Path
|
if err != nil {
|
||||||
if urlPath == "" {
|
filename = path.Base(resp.Request.URL.Path)
|
||||||
urlPath = strings.ReplaceAll(_u.Host, ".", "_")
|
|
||||||
}
|
}
|
||||||
filename := path.Base(urlPath)
|
filename = strings.Trim(filename, "/")
|
||||||
if n, err := parseFilenameFromContentDisposition(resp.Header.Get("Content-Disposition")); err == nil {
|
if len(filename) == 0 {
|
||||||
filename = n
|
filename = fmt.Sprintf("%s-%d-%x", strings.ReplaceAll(req.URL.Host, ".", "_"), time.Now().UnixMilli(), rand.Uint32())
|
||||||
}
|
}
|
||||||
fileSize := resp.ContentLength
|
fileSize := resp.ContentLength
|
||||||
if streamPut {
|
if streamPut {
|
||||||
|
Reference in New Issue
Block a user