refactored HEAD fetch

This commit is contained in:
Djeeberjr 2022-05-13 11:53:55 +02:00
parent 2ae7923efc
commit c1b4225b34

View File

@ -1,18 +1,18 @@
<script lang="ts"> <script lang="ts">
import { onMount } from "svelte"; import { onMount } from "svelte";
import getComponentForShare from "./contentType"; import getComponentForShare from "./contentType";
const regex = /filename="(.*)"/gm;
const slug = window.location.pathname.split("/").pop(); const slug = window.location.pathname.split("/").pop();
let component = null; let component = null;
let contentType: string; let contentType: string;
let filename: string; let filename: string;
onMount(async () => { async function getFileHeadAndComponent() {
const regex = /filename="(.*)"/gm;
const res = await fetch(`/s/${slug}`,{method:"HEAD"}); const res = await fetch(`/s/${slug}`,{method:"HEAD"});
contentType = res.headers.get("Content-Type"); contentType = res.headers.get("Content-Type");
let match; let match;
while ((match = regex.exec(res.headers.get("Content-Disposition"))) !== null) { while ((match = 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
@ -21,26 +21,20 @@
} }
filename = match[1]; filename = match[1];
} }
document.title = filename; return getComponentForShare(contentType,filename);
}
component = getComponentForShare(contentType,filename);
});
</script> </script>
<div class="dl"> <div class="dl">
<a href="/s/{slug}">Download</a> <a href="/s/{slug}">Download</a>
</div> </div>
<main> <main>
{#if component === null} {#await getFileHeadAndComponent()}
Loading data... Loading data...
{:else} {:then v }
{#await component} <svelte:component this={v.default} slug={slug} contentType={contentType} filename={filename} />
Loading data... {/await}
{:then v }
<svelte:component this={v.default} slug={slug} contentType={contentType} filename={filename} />
{/await}
{/if}
</main> </main>
<style> <style>