s3-share/web/App.svelte

52 lines
1.1 KiB
Svelte
Raw Normal View History

2022-05-09 18:01:31 +00:00
<script lang="ts">
2022-05-10 14:01:07 +00:00
import { onMount } from "svelte";
import getComponentForShare from "./contentType";
2022-05-09 18:01:31 +00:00
const slug = window.location.pathname.split("/").pop();
2022-05-10 14:01:07 +00:00
let component = null;
2022-05-10 20:37:30 +00:00
let contentType: string;
let filename: string;
2022-05-09 18:01:31 +00:00
onMount(async () => {
2022-05-10 14:01:07 +00:00
const regex = /filename="(.*)"/gm;
2022-05-10 20:37:30 +00:00
const res = await fetch(`/s/${slug}`,{method:"HEAD"});
2022-05-09 18:01:31 +00:00
2022-05-10 20:37:30 +00:00
contentType = res.headers.get("Content-Type");
2022-05-10 21:19:31 +00:00
let match;
while ((match = regex.exec(res.headers.get("Content-Disposition"))) !== null) {
2022-05-10 14:01:07 +00:00
// This is necessary to avoid infinite loops with zero-width matches
2022-05-10 21:19:31 +00:00
if (match.index === regex.lastIndex) {
2022-05-10 14:01:07 +00:00
regex.lastIndex++;
}
2022-05-10 21:19:31 +00:00
filename = match[1];
2022-05-10 14:01:07 +00:00
}
document.title = filename;
component = getComponentForShare(contentType,filename);
});
2022-05-09 18:01:31 +00:00
</script>
<main>
2022-05-10 21:19:31 +00:00
{#if component === null}
Loading data...
{:else}
{#await component}
2022-05-10 14:01:07 +00:00
Loading data...
2022-05-10 21:19:31 +00:00
{:then v }
<svelte:component this={v.default} slug={slug} contentType={contentType} filename={filename} />
{/await}
{/if}
2022-05-09 18:01:31 +00:00
</main>
<style>
2022-05-10 21:19:31 +00:00
main {
display: flex;
justify-content: center;
2022-05-11 18:06:19 +00:00
padding-top: 5rem;
2022-05-10 21:19:31 +00:00
}
2022-05-10 14:01:07 +00:00
</style>