From f31f1bbf02b9333012cd4334e62d150c671a7524 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Sat, 14 May 2022 23:06:16 +0200 Subject: [PATCH] improved opening images --- internal/web/web.go | 17 ++++++++++------- web/App.svelte | 2 +- web/opener/Image.svelte | 4 +++- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/internal/web/web.go b/internal/web/web.go index 2945104..a227152 100644 --- a/internal/web/web.go +++ b/internal/web/web.go @@ -32,7 +32,7 @@ func StartWebserver(addr string, client client.Client, username, password string http.ServeFile(w, r, "./public/index.html") }) - r.HandleFunc("/s/{slug:[a-zA-Z0-9]{6}}", func(w http.ResponseWriter, r *http.Request) { + r.HandleFunc("/s/{path:.{6,}}", func(w http.ResponseWriter, r *http.Request) { share := getShareHead(client, w, r) if share == nil { return @@ -48,12 +48,12 @@ func StartWebserver(addr string, client client.Client, username, password string _, err = io.Copy(w, obj) if err != nil { - w.WriteHeader(http.StatusInternalServerError) + logrus.Error(err.Error()) return } }) - r.HandleFunc("/s/{slug:[a-zA-Z0-9]{6}}", func(w http.ResponseWriter, r *http.Request) { + r.HandleFunc("/s/{path:.{6,}}", func(w http.ResponseWriter, r *http.Request) { getShareHead(client, w, r) }).Methods("HEAD") @@ -119,8 +119,8 @@ func StartWebserver(addr string, client client.Client, username, password string 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"]) + slug := vars["path"][0:6] + share, err := client.GetShare(r.Context(), slug) if err != nil { logrus.Error(err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -139,9 +139,12 @@ func getShareHead(client client.Client, w http.ResponseWriter, r *http.Request) return nil } - w.Header().Set("Content-Type", metadata.ContentType) + // Prevent using the default Content-Type. Can cause some confusion when the browser tries to open images in a new tab. + if metadata.ContentType != "application/octet-stream" { + 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+"\"") + w.Header().Set("Content-Disposition", "inline; filename=\""+metadata.Filename+"\"") if metadata.ETag != "" { w.Header().Set("ETag", metadata.ETag) } diff --git a/web/App.svelte b/web/App.svelte index c62a50d..60c3e69 100644 --- a/web/App.svelte +++ b/web/App.svelte @@ -37,7 +37,7 @@ {#if !notFound}
- Download ({sizeToReadable(fileSize)}) + Download ({sizeToReadable(fileSize)})
{/if}
diff --git a/web/opener/Image.svelte b/web/opener/Image.svelte index 99d808c..b473980 100644 --- a/web/opener/Image.svelte +++ b/web/opener/Image.svelte @@ -2,9 +2,11 @@ export let contentType: string; export let filename: string; export let slug: string; + + let ext = filename.split('.').pop(); -shared +shared