hotime/dri/download/download.go

41 lines
720 B
Go
Raw Permalink Normal View History

2017-11-07 01:21:48 +00:00
package download
import (
"bytes"
2022-03-12 17:12:29 +00:00
. "code.hoteas.com/golang/hotime/common"
2017-11-07 01:21:48 +00:00
"io"
"io/ioutil"
"net/http"
"os"
)
2022-03-12 17:48:54 +00:00
// Down 下载文件
2021-05-25 12:27:24 +00:00
func Down(url, path, name string, e ...*Error) bool {
2017-11-07 01:21:48 +00:00
os.MkdirAll(path, os.ModeDir)
2021-05-23 23:27:41 +00:00
if Substr(path, len(path)-1, len(path)) != "/" {
2017-11-07 01:21:48 +00:00
path = path + "/"
}
out, err := os.Create(path + name)
2021-05-25 12:27:24 +00:00
2022-08-31 11:10:08 +00:00
if err != nil && len(e) != 0 {
2021-05-25 12:27:24 +00:00
e[0].SetError(err)
2017-11-07 01:21:48 +00:00
return false
}
defer out.Close()
resp, err := http.Get(url)
2022-08-31 11:10:08 +00:00
if err != nil && len(e) != 0 {
2021-05-25 12:27:24 +00:00
e[0].SetError(err)
2017-12-01 07:33:19 +00:00
return false
}
2017-11-07 01:21:48 +00:00
defer resp.Body.Close()
2017-12-01 07:33:19 +00:00
2017-11-07 01:21:48 +00:00
pix, err := ioutil.ReadAll(resp.Body)
_, err = io.Copy(out, bytes.NewReader(pix))
2022-08-31 11:15:47 +00:00
if err != nil && len(e) != 0 {
2021-05-25 12:27:24 +00:00
e[0].SetError(err)
2017-11-07 01:21:48 +00:00
return false
}
return true
}