dkl-store-upload
This commit is contained in:
parent
a7158e9e56
commit
48ab32f319
@ -1,20 +1,8 @@
|
||||
# ------------------------------------------------------------------------
|
||||
from golang:1.12.3-alpine as build
|
||||
run apk add --update git
|
||||
|
||||
env CGO_ENABLED 0
|
||||
arg GOPROXY
|
||||
|
||||
workdir /src
|
||||
add go.sum go.mod ./
|
||||
run go mod download
|
||||
|
||||
add . ./
|
||||
run go test ./...
|
||||
run go install ./cmd/dkl-store
|
||||
from mcluseau/golang-builder:1.13.1 as build
|
||||
|
||||
# ------------------------------------------------------------------------
|
||||
from alpine:3.9
|
||||
from alpine:3.10
|
||||
volume /srv/dkl-store
|
||||
entrypoint ["/bin/dkl-store"]
|
||||
copy --from=build /go/bin/ /bin/
|
||||
|
68
cmd/dkl-store-upload/main.go
Normal file
68
cmd/dkl-store-upload/main.go
Normal file
@ -0,0 +1,68 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
token = flag.String("token", "", "Upload token")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
args := flag.Args()
|
||||
if len(args) != 2 {
|
||||
fmt.Print("source file and target URL are required")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
inPath := args[0]
|
||||
outURL := args[1]
|
||||
|
||||
in, err := os.Open(inPath)
|
||||
fail(err)
|
||||
|
||||
// hash the file
|
||||
log.Print("hashing...")
|
||||
h := sha1.New()
|
||||
_, err = io.Copy(h, in)
|
||||
fail(err)
|
||||
|
||||
sha1Hex := hex.EncodeToString(h.Sum(nil))
|
||||
|
||||
log.Print("SHA1 of source: ", sha1Hex)
|
||||
|
||||
// rewind
|
||||
_, err = in.Seek(0, os.SEEK_SET)
|
||||
fail(err)
|
||||
|
||||
// upload
|
||||
req, err := http.NewRequest("POST", outURL, in)
|
||||
fail(err)
|
||||
|
||||
req.Header.Set("X-Content-SHA1", sha1Hex)
|
||||
|
||||
log.Print("uploading...")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
fail(err)
|
||||
|
||||
if resp.StatusCode != http.StatusCreated {
|
||||
log.Fatalf("unexpected HTTP status: %s", resp.Status)
|
||||
}
|
||||
|
||||
log.Print("uploaded successfully")
|
||||
}
|
||||
|
||||
func fail(err error) {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
@ -47,7 +47,7 @@ func handleHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
|
||||
switch req.Method {
|
||||
case "GET":
|
||||
case "GET", "HEAD":
|
||||
sha1Hex, err := hashOf(filePath)
|
||||
if err != nil {
|
||||
writeErr(err, w)
|
||||
|
Loading…
Reference in New Issue
Block a user