From c338522b33d4ec01e31c2018463decb31336534e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Cluseau?= Date: Sat, 19 Aug 2023 21:17:23 +0200 Subject: [PATCH] allow store upload, + upload UIs for store and config --- cmd/dkl-local-server/ws-public.go | 68 +++++++++++++++++++++++++++++++ cmd/dkl-local-server/ws.go | 3 ++ html/ui/index.html | 14 ++++++- html/ui/js/app.js | 24 +++++++++-- 4 files changed, 103 insertions(+), 6 deletions(-) diff --git a/cmd/dkl-local-server/ws-public.go b/cmd/dkl-local-server/ws-public.go index 5e3195c..67e0576 100644 --- a/cmd/dkl-local-server/ws-public.go +++ b/cmd/dkl-local-server/ws-public.go @@ -5,10 +5,13 @@ import ( "bytes" "io" "io/fs" + "log" "net/http" "os" + "path/filepath" restful "github.com/emicklei/go-restful" + "m.cluseau.fr/go/httperr" ) func wsUnlockStore(req *restful.Request, resp *restful.Response) { @@ -96,3 +99,68 @@ func wsStoreDownload(req *restful.Request, resp *restful.Response) { buf.WriteTo(resp) } + +func wsStoreUpload(req *restful.Request, resp *restful.Response) { + if !secStore.IsNew() { + wsError(resp, httperr.BadRequest("store is not new")) + return + } + + buf := new(bytes.Buffer) + + _, err := io.Copy(buf, req.Request.Body) + if err != nil { + wsError(resp, err) + return + } + + arch := tar.NewReader(buf) + + root := secStoreRoot() + + for { + hdr, err := arch.Next() + if err == io.EOF { + err = nil + break + } else if err != nil { + wsError(resp, err) + return + } + + log.Print(hdr.Name) + + fullPath := filepath.Join(root, hdr.Name) + + switch { + case hdr.FileInfo().IsDir(): + err = os.MkdirAll(fullPath, 0700) + if err != nil { + wsError(resp, err) + return + } + + default: + content, err := io.ReadAll(io.LimitReader(arch, hdr.Size)) + if err != nil { + wsError(resp, err) + return + } + + err = os.WriteFile(fullPath, content, 0600) + if err != nil { + wsError(resp, err) + return + } + } + } + + if err != nil { + wsError(resp, err) + return + } + + openSecretStore() + + resp.WriteEntity(map[string]any{"ok": true}) +} diff --git a/cmd/dkl-local-server/ws.go b/cmd/dkl-local-server/ws.go index 1639322..fb15aa8 100644 --- a/cmd/dkl-local-server/ws.go +++ b/cmd/dkl-local-server/ws.go @@ -34,6 +34,9 @@ func registerWS(rest *restful.Container) { Produces(mime.TAR). Param(ws.QueryParameter("token", "the download token")). Doc("Fetch the encrypted store")). + Route(ws.POST("/store.tar").To(wsStoreUpload). + Consumes(mime.TAR). + Doc("Upload an existing store")). Route(ws.GET("/downloads/{token}/{asset}").To(wsDownload). Param(ws.PathParameter("token", "the download token")). Param(ws.PathParameter("asset", "the requested asset")). diff --git a/html/ui/index.html b/html/ui/index.html index 709fcec..a4defcc 100644 --- a/html/ui/index.html +++ b/html/ui/index.html @@ -39,15 +39,21 @@