local-server/cmd/dkl-local-server/upstream.go

104 lines
1.9 KiB
Go
Raw Normal View History

2018-06-12 10:09:47 +00:00
package main
import (
2019-04-12 16:17:49 +00:00
"crypto/sha1"
"encoding/hex"
2018-06-12 10:09:47 +00:00
"flag"
2019-04-12 09:15:57 +00:00
"fmt"
2018-06-12 10:09:47 +00:00
"io"
"log"
"net/http"
"os"
gopath "path"
"path/filepath"
"time"
)
var (
upstreamURL = flag.String("upstream", "https://direktil.novit.nc/dist", "Upstream server for dist elements")
)
func (ctx *renderContext) distFetch(path ...string) (outPath string, err error) {
outPath = ctx.distFilePath(path...)
if _, err = os.Stat(outPath); err == nil {
return
} else if !os.IsNotExist(err) {
return
}
subPath := gopath.Join(path...)
log.Print("need to fetch ", subPath)
if err = os.MkdirAll(filepath.Dir(outPath), 0755); err != nil {
return
}
fullURL := *upstreamURL + "/" + subPath
resp, err := http.Get(fullURL)
if err != nil {
return
}
2019-04-12 16:17:49 +00:00
defer resp.Body.Close()
2019-04-12 09:15:57 +00:00
if resp.StatusCode != 200 {
err = fmt.Errorf("wrong status: %s", resp.Status)
return
}
2019-04-12 16:17:49 +00:00
fOut, err := os.Create(filepath.Join(filepath.Dir(outPath), "._part_"+filepath.Base(outPath)))
if err != nil {
return
}
hash := sha1.New()
out := io.MultiWriter(fOut, hash)
2018-06-12 10:09:47 +00:00
done := make(chan error, 1)
go func() {
2019-04-12 16:17:49 +00:00
_, err = io.Copy(out, resp.Body)
fOut.Close()
2018-06-12 10:09:47 +00:00
if err != nil {
2019-04-12 16:17:49 +00:00
os.Remove(fOut.Name())
2018-06-12 10:09:47 +00:00
}
done <- err
2019-04-12 16:17:49 +00:00
close(done)
2018-06-12 10:09:47 +00:00
}()
wait:
select {
case <-time.After(10 * time.Second):
log.Print("still fetching ", subPath, "...")
goto wait
case err = <-done:
if err != nil {
log.Print("fetch of ", subPath, " failed: ", err)
return
}
}
2019-04-12 16:17:49 +00:00
hexSum := hex.EncodeToString(hash.Sum(nil))
log.Printf("fetch of %s finished (SHA1 checksum: %s)", subPath, hexSum)
if remoteSum := resp.Header.Get("X-Content-SHA1"); remoteSum != "" {
log.Printf("fetch of %s: remote SHA1 checksum: %s", subPath, remoteSum)
if remoteSum != hexSum {
err = fmt.Errorf("wrong SHA1 checksum: server=%s local=%s", remoteSum, hexSum)
log.Print("fetch of ", subPath, ": ", err)
os.Remove(fOut.Name())
return
}
}
2019-04-12 09:22:39 +00:00
2019-04-12 16:17:49 +00:00
err = os.Rename(fOut.Name(), outPath)
2018-06-12 10:09:47 +00:00
return
}