added file size

This commit is contained in:
Djeeberjr 2022-05-13 15:33:29 +02:00
parent 632fc05f85
commit fd97d589d4
2 changed files with 14 additions and 1 deletions

View File

@ -1,6 +1,7 @@
<script lang="ts"> <script lang="ts">
import getComponentForShare from "./contentType"; import getComponentForShare from "./contentType";
import NotFound from "./NotFound.svelte"; import NotFound from "./NotFound.svelte";
import sizeToReadable from "./sizeToReadable";
const regex = /filename="(.*)"/gm; const regex = /filename="(.*)"/gm;
const slug = window.location.pathname.split("/").pop(); const slug = window.location.pathname.split("/").pop();
@ -8,6 +9,7 @@
let notFound = false; let notFound = false;
let contentType: string; let contentType: string;
let filename: string; let filename: string;
let fileSize: number = 0;
async function getFileHead() { async function getFileHead() {
const res = await fetch(`/s/${slug}`,{method:"HEAD"}); const res = await fetch(`/s/${slug}`,{method:"HEAD"});
@ -18,6 +20,7 @@
} }
contentType = res.headers.get("Content-Type"); contentType = res.headers.get("Content-Type");
fileSize = parseInt(res.headers.get("Content-Length"));
let match; let match;
while ((match = regex.exec(res.headers.get("Content-Disposition"))) !== null) { while ((match = regex.exec(res.headers.get("Content-Disposition"))) !== null) {
@ -34,7 +37,7 @@
{#if !notFound} {#if !notFound}
<div class="dl"> <div class="dl">
<a href="/s/{slug}">Download</a> <a href="/s/{slug}">Download ({sizeToReadable(fileSize)})</a>
</div> </div>
{/if} {/if}
<main> <main>

10
web/sizeToReadable.ts Normal file
View File

@ -0,0 +1,10 @@
function sizeToReadable(size: number): string {
if (size == 0)
return "0 B"
const i = Math.floor(Math.log(size) / Math.log(1024))
const num = (size / Math.pow(1024, i))
return (num.toFixed(1).endsWith("0")?num.toFixed(0):num.toFixed(1)) + " " + ["B", "kB", "MB", "GB", "TB"][i]
}
export default sizeToReadable