display a text share
This commit is contained in:
parent
ca2b2c661c
commit
b5a326324f
@ -7,6 +7,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"git.kapelle.org/niklas/s3share/internal/client"
|
"git.kapelle.org/niklas/s3share/internal/client"
|
||||||
|
"git.kapelle.org/niklas/s3share/internal/types"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/sirupsen/logrus"
|
"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) {
|
r.HandleFunc("/s/{slug:[a-zA-Z0-9]{6}}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
share := getShareHead(client, w, r)
|
||||||
|
|
||||||
share, err := client.GetShare(r.Context(), vars["slug"])
|
|
||||||
if err != nil {
|
|
||||||
logrus.Error(err.Error())
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if share == nil {
|
if share == nil {
|
||||||
http.NotFound(w, r)
|
|
||||||
return
|
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)
|
obj, err := client.GetObjectFromShare(r.Context(), share)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Error(err.Error())
|
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) {
|
r.HandleFunc("/api/share", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
}).Methods("GET")
|
}).Methods("GET")
|
||||||
@ -119,3 +104,32 @@ func StartWebserver(addr string, client client.Client) error {
|
|||||||
logrus.Info("Starting webserver")
|
logrus.Info("Starting webserver")
|
||||||
return http.ListenAndServe(addr, r)
|
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
|
||||||
|
}
|
||||||
|
@ -4,14 +4,15 @@
|
|||||||
const slug = window.location.pathname.split("/").pop();
|
const slug = window.location.pathname.split("/").pop();
|
||||||
|
|
||||||
let component = null;
|
let component = null;
|
||||||
|
let contentType: string;
|
||||||
|
let filename: string;
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
const regex = /filename="(.*)"/gm;
|
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;
|
let m;
|
||||||
while ((m = regex.exec(res.headers.get("Content-Disposition"))) !== null) {
|
while ((m = regex.exec(res.headers.get("Content-Disposition"))) !== null) {
|
||||||
// This is necessary to avoid infinite loops with zero-width matches
|
// This is necessary to avoid infinite loops with zero-width matches
|
||||||
@ -36,7 +37,7 @@
|
|||||||
{#await component}
|
{#await component}
|
||||||
Loading data...
|
Loading data...
|
||||||
{:then v }
|
{:then v }
|
||||||
<svelte:component this={v.default} />
|
<svelte:component this={v.default} slug={slug} contentType={contentType} filename={filename} />
|
||||||
{/await}
|
{/await}
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
export let contentType: string;
|
export let contentType: string;
|
||||||
export let filename: string;
|
export let filename: string;
|
||||||
export let slug: string;
|
export let slug: string;
|
||||||
export let response: Response;
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
Default opener
|
Default opener
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,11 +1,18 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
export let contentType: string;
|
import { onMount } from "svelte";
|
||||||
export let filename: string;
|
export let contentType: string;
|
||||||
export let slug: string;
|
export let filename: string;
|
||||||
export let response: Response;
|
export let slug: string;
|
||||||
|
|
||||||
|
let content = "";
|
||||||
|
|
||||||
|
onMount(async () => {
|
||||||
|
const res = await fetch(`/s/${slug}`);
|
||||||
|
content = await res.text();
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
Text opener
|
Text opener
|
||||||
|
<pre><code>{ content }</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
Reference in New Issue
Block a user