github.com/pavlo67/common@v0.5.3/common/httplib/download.go (about) 1 package httplib 2 3 import ( 4 "errors" 5 "io/ioutil" 6 "net/http" 7 "os" 8 "strconv" 9 "strings" 10 ) 11 12 func Download(url string) ([]byte, error) { 13 resp, err := http.Get(url) 14 if err != nil { 15 return nil, err 16 } 17 defer resp.Body.Close() 18 body, err := ioutil.ReadAll(resp.Body) 19 if err != nil { 20 return nil, err 21 } 22 return body, nil 23 } 24 25 func DownloadFile(url, pathToLoad string, fileIndex int, perm os.FileMode) (fileName, fileType string, err error) { 26 url = strings.TrimSpace(url) 27 if url == "" { 28 return "", "", errors.New("empty URL to download data") 29 } 30 31 resp, err := http.Get(url) 32 if err != nil { 33 return "", "", err 34 } 35 defer resp.Body.Close() 36 body, err := ioutil.ReadAll(resp.Body) 37 if err != nil { 38 return "", "", err 39 } 40 41 // TODO!!! 42 fileType = "html" 43 fileName = pathToLoad + strconv.Itoa(fileIndex) + "." + fileType 44 45 err = ioutil.WriteFile(fileName, body, perm) 46 if err != nil { 47 return "", "", err 48 } 49 50 return fileName, fileType, nil 51 }