display a text share

This commit is contained in:
Djeeberjr 2022-05-10 22:37:30 +02:00
parent ca2b2c661c
commit b5a326324f
4 changed files with 57 additions and 36 deletions

View File

@ -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
}

View File

@ -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");
contentType = res.headers.get("Content-Type");
let filename: string;
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 }
<svelte:component this={v.default} />
<svelte:component this={v.default} slug={slug} contentType={contentType} filename={filename} />
{/await}
{/if}
</div>

View File

@ -2,7 +2,6 @@
export let contentType: string;
export let filename: string;
export let slug: string;
export let response: Response;
</script>
<div>

View File

@ -1,11 +1,18 @@
<script lang="ts">
import { onMount } from "svelte";
export let contentType: string;
export let filename: string;
export let slug: string;
export let response: Response;
let content = "";
onMount(async () => {
const res = await fetch(`/s/${slug}`);
content = await res.text();
});
</script>
<div>
Text opener
<pre><code>{ content }</code></pre>
</div>