added file size
This commit is contained in:
parent
632fc05f85
commit
fd97d589d4
@ -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
10
web/sizeToReadable.ts
Normal 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
|
Loading…
Reference in New Issue
Block a user