s3-share/web/App.svelte

83 lines
1.6 KiB
Svelte
Raw Permalink Normal View History

2022-05-09 18:01:31 +00:00
<script lang="ts">
2022-05-10 14:01:07 +00:00
import getComponentForShare from "./contentType";
2022-05-13 13:01:33 +00:00
import NotFound from "./NotFound.svelte";
2022-05-13 13:33:29 +00:00
import sizeToReadable from "./sizeToReadable";
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-13 13:01:33 +00:00
let notFound = false;
2022-05-10 20:37:30 +00:00
let contentType: string;
let filename: string;
2022-05-13 13:33:29 +00:00
let fileSize: number = 0;
2022-05-09 18:01:31 +00:00
2022-05-13 13:01:33 +00:00
async function getFileHead() {
2022-05-10 20:37:30 +00:00
const res = await fetch(`/s/${slug}`,{method:"HEAD"});
2022-05-13 13:01:33 +00:00
if (res.status === 404){
notFound = true;
return;
}
2022-05-10 20:37:30 +00:00
contentType = res.headers.get("Content-Type");
2022-05-13 13:33:29 +00:00
fileSize = parseInt(res.headers.get("Content-Length"));
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 13:09:04 +00:00
document.title = filename;
2022-05-13 09:53:55 +00:00
}
2022-05-09 18:01:31 +00:00
</script>
2022-05-13 13:01:33 +00:00
{#if !notFound}
<div class="dl">
2022-05-14 21:06:16 +00:00
<a download href="/s/{slug}">Download ({sizeToReadable(fileSize)})</a>
2022-05-13 13:01:33 +00:00
</div>
{/if}
2022-05-09 18:01:31 +00:00
<main>
2022-05-13 13:01:33 +00:00
{#await getFileHead()}
2022-05-10 21:19:31 +00:00
Loading data...
2022-05-13 13:01:33 +00:00
{:then}
{#if notFound}
<NotFound />
{:else}
{#await getComponentForShare(contentType, filename)}
Loading component...
{:then v}
<svelte:component this={v.default} slug={slug} contentType={contentType} filename={filename} />
{/await}
{/if}
2022-05-13 09:53:55 +00:00
{/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>