You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

38 lines
685 B

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
}