fix(offline_download): os.create failure while the name of downloaded file is empty (#7041)

renovate/github.com-meilisearch-meilisearch-go-0.x
Wang Xiaoqing 2024-08-22 00:44:23 +08:00 committed by GitHub
parent bcb24d61ea
commit 74887922b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 4 deletions

View File

@ -2,14 +2,16 @@ package http
import ( import (
"fmt" "fmt"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/offline_download/tool"
"github.com/alist-org/alist/v3/pkg/utils"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"strings"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/offline_download/tool"
"github.com/alist-org/alist/v3/pkg/utils"
) )
type SimpleHttp struct { type SimpleHttp struct {
@ -63,7 +65,12 @@ 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)
} }
filename := path.Base(_u.Path) // If Path is empty, use Hostname; otherwise, filePath euqals TempDir which causes os.Create to fail
urlPath := _u.Path
if urlPath == "" {
urlPath = strings.ReplaceAll(_u.Host, ".", "_")
}
filename := path.Base(urlPath)
if n, err := parseFilenameFromContentDisposition(resp.Header.Get("Content-Disposition")); err == nil { if n, err := parseFilenameFromContentDisposition(resp.Header.Get("Content-Disposition")); err == nil {
filename = n filename = n
} }