mirror of
https://github.com/vbatts/imgsrv.git
synced 2025-07-27 01:20:26 +00:00
separating out more code
This commit is contained in:
parent
b38c81e23c
commit
ae688c93a9
4 changed files with 34 additions and 23 deletions
55
util/util.go
Normal file
55
util/util.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
func FetchFileFromURL(url string) (filename string, err error) {
|
||||
var t time.Time
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
log.Println(resp)
|
||||
|
||||
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)
|
||||
|
||||
// lastly, return
|
||||
return filepath.Join(os.TempDir(), url_filename), nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue