-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(offline_download): add simple http tool (close #4002)
- Loading branch information
Showing
8 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package http | ||
|
||
import ( | ||
"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/url" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
) | ||
|
||
type SimpleHttp struct { | ||
client http.Client | ||
} | ||
|
||
func (s SimpleHttp) Name() string { | ||
return "SimpleHttp" | ||
} | ||
|
||
func (s SimpleHttp) Items() []model.SettingItem { | ||
return nil | ||
} | ||
|
||
func (s SimpleHttp) Init() (string, error) { | ||
return "ok", nil | ||
} | ||
|
||
func (s SimpleHttp) IsReady() bool { | ||
return true | ||
} | ||
|
||
func (s SimpleHttp) AddURL(args *tool.AddUrlArgs) (string, error) { | ||
panic("should not be called") | ||
} | ||
|
||
func (s SimpleHttp) Remove(task *tool.DownloadTask) error { | ||
panic("should not be called") | ||
} | ||
|
||
func (s SimpleHttp) Status(task *tool.DownloadTask) (*tool.Status, error) { | ||
panic("should not be called") | ||
} | ||
|
||
func (s SimpleHttp) Run(task *tool.DownloadTask) error { | ||
u := task.Url | ||
// parse url | ||
_u, err := url.Parse(u) | ||
if err != nil { | ||
return err | ||
} | ||
req, err := http.NewRequestWithContext(task.Ctx(), http.MethodGet, u, nil) | ||
if err != nil { | ||
return err | ||
} | ||
resp, err := s.client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
if resp.StatusCode >= 400 { | ||
return fmt.Errorf("http status code %d", resp.StatusCode) | ||
} | ||
filename := path.Base(_u.Path) | ||
if n, err := parseFilenameFromContentDisposition(resp.Header.Get("Content-Disposition")); err == nil { | ||
filename = n | ||
} | ||
// save to temp dir | ||
_ = os.MkdirAll(task.TempDir, os.ModePerm) | ||
filePath := filepath.Join(task.TempDir, filename) | ||
file, err := os.Create(filePath) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
fileSize := resp.ContentLength | ||
err = utils.CopyWithCtx(task.Ctx(), file, resp.Body, fileSize, task.SetProgress) | ||
return err | ||
} | ||
|
||
func init() { | ||
tool.Tools.Add(&SimpleHttp{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package http | ||
|
||
import ( | ||
"fmt" | ||
"mime" | ||
) | ||
|
||
func parseFilenameFromContentDisposition(contentDisposition string) (string, error) { | ||
if contentDisposition == "" { | ||
return "", fmt.Errorf("Content-Disposition is empty") | ||
} | ||
_, params, err := mime.ParseMediaType(contentDisposition) | ||
if err != nil { | ||
return "", err | ||
} | ||
filename := params["filename"] | ||
if filename == "" { | ||
return "", fmt.Errorf("filename not found in Content-Disposition: [%s]", contentDisposition) | ||
} | ||
return filename, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters