2021-08-26 18:50:14 +08:00
|
|
|
/**
|
2022-10-31 17:22:49 +08:00
|
|
|
* @Author: Echo
|
|
|
|
* @Email: 1711788888@qq.com
|
2021-08-26 18:50:14 +08:00
|
|
|
* @Date: 2021/8/26 1:59 下午
|
|
|
|
* @Desc: TODO
|
|
|
|
*/
|
|
|
|
|
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2022-10-31 17:22:49 +08:00
|
|
|
"git.echol.cn/loser/http/internal"
|
2021-08-26 18:50:14 +08:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var contentTypeToFileSuffix = map[string]string{
|
|
|
|
"application/x-001": ".001",
|
|
|
|
"text/h323": ".323",
|
|
|
|
"drawing/907": ".907",
|
|
|
|
"audio/x-mei-aac": ".acp",
|
|
|
|
"audio/aiff": ".aif",
|
|
|
|
"text/asa": ".asa",
|
|
|
|
"text/asp": ".asp",
|
|
|
|
"audio/basic": ".au",
|
|
|
|
"application/vnd.adobe.workflow": ".awf",
|
|
|
|
"application/x-bmp": ".bmp",
|
|
|
|
"application/x-c4t": ".c4t",
|
|
|
|
"application/x-cals": ".cal",
|
|
|
|
"application/x-netcdf": ".cdf",
|
|
|
|
"application/x-cel": ".cel",
|
|
|
|
"application/x-g4": ".cg4",
|
|
|
|
"application/x-cit": ".cit",
|
|
|
|
"text/xml": ".cml",
|
|
|
|
"application/x-cmx": ".cmx",
|
|
|
|
"application/pkix-crl": ".crl",
|
|
|
|
"application/x-csi": ".csi",
|
|
|
|
"application/x-cut": ".cut",
|
|
|
|
"application/x-dbm": ".dbm",
|
|
|
|
}
|
|
|
|
|
|
|
|
type Download struct {
|
|
|
|
request *Request
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDownload(c *Client) *Download {
|
|
|
|
return &Download{
|
|
|
|
request: NewRequest(c),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Download download a file from the network address to the local.
|
|
|
|
func (d *Download) Download(url, dir string, filename ...string) (string, error) {
|
|
|
|
resp, err := d.request.request(MethodGet, url)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-10-31 17:22:49 +08:00
|
|
|
|
2021-08-26 18:50:14 +08:00
|
|
|
var path string
|
2022-10-31 17:22:49 +08:00
|
|
|
|
2021-08-26 18:50:14 +08:00
|
|
|
if len(filename) > 0 {
|
|
|
|
path = strings.TrimRight(dir, string(os.PathSeparator)) + string(os.PathSeparator) + filename[0]
|
|
|
|
} else {
|
|
|
|
path = d.genFilePath(resp, dir)
|
|
|
|
}
|
2022-10-31 17:22:49 +08:00
|
|
|
|
2021-08-26 18:50:14 +08:00
|
|
|
if err = internal.SaveToFile(path, resp.ReadBytes()); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-10-31 17:22:49 +08:00
|
|
|
|
2021-08-26 18:50:14 +08:00
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// genFilePath generate file path based on response content type
|
|
|
|
func (d *Download) genFilePath(resp *Response, dir string) string {
|
|
|
|
path := strings.TrimRight(dir, string(os.PathSeparator)) + string(os.PathSeparator) + internal.RandStr(16)
|
2022-10-31 17:22:49 +08:00
|
|
|
|
2021-08-26 18:50:14 +08:00
|
|
|
if suffix := internal.GetFileType(resp.ReadBytes()); suffix != "" {
|
|
|
|
path += "." + suffix
|
|
|
|
}
|
2022-10-31 17:22:49 +08:00
|
|
|
|
2021-08-26 18:50:14 +08:00
|
|
|
if internal.Exists(path) {
|
|
|
|
return d.genFilePath(resp, dir)
|
|
|
|
}
|
2022-10-31 17:22:49 +08:00
|
|
|
|
2021-08-26 18:50:14 +08:00
|
|
|
return path
|
|
|
|
}
|