From b5a326324f02029945808b3320f70aa50e8dfdcb Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Tue, 10 May 2022 22:37:30 +0200 Subject: [PATCH] display a text share --- internal/web/web.go | 56 ++++++++++++++++++++++++--------------- web/App.svelte | 11 ++++---- web/opener/Default.svelte | 9 +++---- web/opener/Text.svelte | 17 ++++++++---- 4 files changed, 57 insertions(+), 36 deletions(-) diff --git a/internal/web/web.go b/internal/web/web.go index fae4088..7a3e76f 100644 --- a/internal/web/web.go +++ b/internal/web/web.go @@ -7,6 +7,7 @@ import ( "strconv" "git.kapelle.org/niklas/s3share/internal/client" + "git.kapelle.org/niklas/s3share/internal/types" "github.com/gorilla/mux" "github.com/sirupsen/logrus" ) @@ -27,31 +28,11 @@ func StartWebserver(addr string, client client.Client) error { }) r.HandleFunc("/s/{slug:[a-zA-Z0-9]{6}}", func(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - - share, err := client.GetShare(r.Context(), vars["slug"]) - if err != nil { - logrus.Error(err.Error()) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - + share := getShareHead(client, w, r) if share == nil { - http.NotFound(w, r) return } - metadata, err := client.GetObjectMetadata(r.Context(), share.Key) - if err != nil { - logrus.Error(err.Error()) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - w.Header().Set("Content-Type", metadata.ContentType) - w.Header().Set("Content-Length", strconv.FormatInt(metadata.Size, 10)) - w.Header().Set("Content-Disposition", "attachment; filename=\""+metadata.Filename+"\"") - obj, err := client.GetObjectFromShare(r.Context(), share) if err != nil { logrus.Error(err.Error()) @@ -67,6 +48,10 @@ func StartWebserver(addr string, client client.Client) error { } }) + r.HandleFunc("/s/{slug:[a-zA-Z0-9]{6}}", func(w http.ResponseWriter, r *http.Request) { + getShareHead(client, w, r) + }).Methods("HEAD") + r.HandleFunc("/api/share", func(w http.ResponseWriter, r *http.Request) { }).Methods("GET") @@ -119,3 +104,32 @@ func StartWebserver(addr string, client client.Client) error { logrus.Info("Starting webserver") return http.ListenAndServe(addr, r) } + +func getShareHead(client client.Client, w http.ResponseWriter, r *http.Request) *types.Share { + vars := mux.Vars(r) + + share, err := client.GetShare(r.Context(), vars["slug"]) + if err != nil { + logrus.Error(err.Error()) + http.Error(w, err.Error(), http.StatusInternalServerError) + return nil + } + + if share == nil { + http.NotFound(w, r) + return nil + } + + metadata, err := client.GetObjectMetadata(r.Context(), share.Key) + if err != nil { + logrus.Error(err.Error()) + http.Error(w, err.Error(), http.StatusInternalServerError) + return nil + } + + w.Header().Set("Content-Type", metadata.ContentType) + w.Header().Set("Content-Length", strconv.FormatInt(metadata.Size, 10)) + w.Header().Set("Content-Disposition", "attachment; filename=\""+metadata.Filename+"\"") + + return share +} diff --git a/web/App.svelte b/web/App.svelte index 81c760f..d8c5c21 100644 --- a/web/App.svelte +++ b/web/App.svelte @@ -4,14 +4,15 @@ const slug = window.location.pathname.split("/").pop(); let component = null; + let contentType: string; + let filename: string; onMount(async () => { const regex = /filename="(.*)"/gm; - const res = await fetch(`/s/${slug}`); + const res = await fetch(`/s/${slug}`,{method:"HEAD"}); - const contentType: string = res.headers.get("Content-Type"); - - let filename: string; + contentType = res.headers.get("Content-Type"); + let m; while ((m = regex.exec(res.headers.get("Content-Disposition"))) !== null) { // This is necessary to avoid infinite loops with zero-width matches @@ -36,7 +37,7 @@ {#await component} Loading data... {:then v } - + {/await} {/if} diff --git a/web/opener/Default.svelte b/web/opener/Default.svelte index 4cda31e..1d4d718 100644 --- a/web/opener/Default.svelte +++ b/web/opener/Default.svelte @@ -1,10 +1,9 @@
- Default opener + Default opener
diff --git a/web/opener/Text.svelte b/web/opener/Text.svelte index be26505..e355703 100644 --- a/web/opener/Text.svelte +++ b/web/opener/Text.svelte @@ -1,11 +1,18 @@
- Text opener + Text opener +
{ content }