improved opening images

This commit is contained in:
Djeeberjr 2022-05-14 23:06:16 +02:00
parent 1ee9916970
commit f31f1bbf02
3 changed files with 14 additions and 9 deletions

View File

@ -32,7 +32,7 @@ func StartWebserver(addr string, client client.Client, username, password string
http.ServeFile(w, r, "./public/index.html") 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) share := getShareHead(client, w, r)
if share == nil { if share == nil {
return return
@ -48,12 +48,12 @@ func StartWebserver(addr string, client client.Client, username, password string
_, err = io.Copy(w, obj) _, err = io.Copy(w, obj)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) logrus.Error(err.Error())
return 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) getShareHead(client, w, r)
}).Methods("HEAD") }).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 { func getShareHead(client client.Client, w http.ResponseWriter, r *http.Request) *types.Share {
vars := mux.Vars(r) vars := mux.Vars(r)
slug := vars["path"][0:6]
share, err := client.GetShare(r.Context(), vars["slug"]) share, err := client.GetShare(r.Context(), slug)
if err != nil { if err != nil {
logrus.Error(err.Error()) logrus.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
@ -139,9 +139,12 @@ func getShareHead(client client.Client, w http.ResponseWriter, r *http.Request)
return nil 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-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 != "" { if metadata.ETag != "" {
w.Header().Set("ETag", metadata.ETag) w.Header().Set("ETag", metadata.ETag)
} }

View File

@ -37,7 +37,7 @@
{#if !notFound} {#if !notFound}
<div class="dl"> <div class="dl">
<a href="/s/{slug}">Download ({sizeToReadable(fileSize)})</a> <a download href="/s/{slug}">Download ({sizeToReadable(fileSize)})</a>
</div> </div>
{/if} {/if}
<main> <main>

View File

@ -2,9 +2,11 @@
export let contentType: string; export let contentType: string;
export let filename: string; export let filename: string;
export let slug: string; export let slug: string;
let ext = filename.split('.').pop();
</script> </script>
<img src="/s/{slug}" alt="shared" /> <img src="/s/{slug}.{ext}" alt="shared" />
<style> <style>
img { img {