package util
|
|
|
|
import (
|
|
"io"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
func DownloadFile(url string) (string, error) {
|
|
var (
|
|
nameSplit []string
|
|
resp *http.Response
|
|
tempFile *os.File
|
|
err error
|
|
)
|
|
|
|
// Used to get the file extension
|
|
// This could be improved with a mime-type lookup
|
|
nameSplit = strings.Split(url, ".")
|
|
|
|
resp, err = http.Get(url)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
tempFile, err = ioutil.TempFile("./uploads", "upload-*."+nameSplit[len(nameSplit)-1])
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer tempFile.Close()
|
|
|
|
_, err = io.Copy(tempFile, resp.Body)
|
|
return filepath.Base(tempFile.Name()), err
|
|
}
|