1
0
Fork 0
mirror of https://github.com/vbatts/imgsrv.git synced 2024-11-30 12:05:42 +00:00
imgsrv/util/util.go

57 lines
1 KiB
Go
Raw Normal View History

2013-05-10 19:03:54 +00:00
package util
2013-02-09 04:43:39 +00:00
import (
2013-03-27 20:30:24 +00:00
"crypto/tls"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"time"
2013-02-09 04:43:39 +00:00
)
func FetchFileFromURL(url string) (filename string, err error) {
2013-03-27 20:30:24 +00:00
var t time.Time
2013-02-09 04:43:39 +00:00
2013-03-27 20:30:24 +00:00
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
//CheckRedirect: redirectPolicyFunc,
Transport: tr,
}
resp, err := client.Get(url)
defer resp.Body.Close()
if err != nil {
return
}
2013-02-09 04:43:39 +00:00
2013-03-27 20:30:24 +00:00
mtime := resp.Header.Get("last-modified")
if len(mtime) > 0 {
t, err = time.Parse(http.TimeFormat, mtime)
if err != nil {
return
}
} else {
log.Println("Last-Modified not present. Using current time")
t = time.Now()
}
_, url_filename := filepath.Split(url)
2013-02-09 04:43:39 +00:00
2013-03-27 20:30:24 +00:00
log.Println(resp)
2013-02-09 04:43:39 +00:00
2013-03-27 20:30:24 +00:00
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = ioutil.WriteFile(filepath.Join(os.TempDir(), url_filename), bytes, 0644)
if err != nil {
return
}
err = os.Chtimes(filepath.Join(os.TempDir(), url_filename), t, t)
2013-02-09 04:43:39 +00:00
2013-03-27 20:30:24 +00:00
// lastly, return
return filepath.Join(os.TempDir(), url_filename), nil
2013-02-09 04:43:39 +00:00
}