s3-share/web/App.svelte

63 lines
1.3 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-13 09:53:55 +00:00
const regex = /filename="(.*)"/gm;
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
2022-05-13 09:53:55 +00:00
async function getFileHeadAndComponent() {
2022-05-10 20:37:30 +00:00
const res = await fetch(`/s/${slug}`,{method:"HEAD"});
contentType = res.headers.get("Content-Type");
2022-05-13 09:53:55 +00:00
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
}
2022-05-13 09:53:55 +00:00
return getComponentForShare(contentType,filename);
}
2022-05-09 18:01:31 +00:00
</script>
2022-05-12 21:21:50 +00:00
<div class="dl">
<a href="/s/{slug}">Download</a>
</div>
2022-05-09 18:01:31 +00:00
<main>
2022-05-13 09:53:55 +00:00
{#await getFileHeadAndComponent()}
2022-05-10 21:19:31 +00:00
Loading data...
2022-05-13 09:53:55 +00:00
{:then v }
<svelte:component this={v.default} slug={slug} contentType={contentType} filename={filename} />
{/await}
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-12 21:21:50 +00:00
.dl {
top: 0;
right: 0;
position: absolute;
padding-top: 1rem;
padding-right: 1rem;
color: var(--color-secondary);
}
.dl a {
color: var(--color-secondary);
text-decoration: none;
}
2022-05-10 14:01:07 +00:00
</style>