added file size
This commit is contained in:
		
							parent
							
								
									632fc05f85
								
							
						
					
					
						commit
						fd97d589d4
					
				@ -1,6 +1,7 @@
 | 
			
		||||
<script lang="ts">
 | 
			
		||||
	import getComponentForShare from "./contentType";
 | 
			
		||||
	import NotFound from "./NotFound.svelte";
 | 
			
		||||
	import sizeToReadable from "./sizeToReadable";
 | 
			
		||||
 | 
			
		||||
	const regex = /filename="(.*)"/gm;
 | 
			
		||||
	const slug = window.location.pathname.split("/").pop();
 | 
			
		||||
@ -8,6 +9,7 @@
 | 
			
		||||
	let notFound = false;
 | 
			
		||||
	let contentType: string;
 | 
			
		||||
	let filename: string;
 | 
			
		||||
	let fileSize: number = 0;
 | 
			
		||||
 | 
			
		||||
	async function getFileHead() {
 | 
			
		||||
		const res = await fetch(`/s/${slug}`,{method:"HEAD"});
 | 
			
		||||
@ -18,6 +20,7 @@
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		contentType = res.headers.get("Content-Type");
 | 
			
		||||
		fileSize = parseInt(res.headers.get("Content-Length"));
 | 
			
		||||
 | 
			
		||||
		let match;
 | 
			
		||||
		while ((match = regex.exec(res.headers.get("Content-Disposition"))) !== null) {
 | 
			
		||||
@ -34,7 +37,7 @@
 | 
			
		||||
 | 
			
		||||
{#if !notFound}
 | 
			
		||||
	<div class="dl">
 | 
			
		||||
		<a href="/s/{slug}">Download</a>
 | 
			
		||||
		<a href="/s/{slug}">Download ({sizeToReadable(fileSize)})</a>
 | 
			
		||||
	</div>
 | 
			
		||||
{/if}
 | 
			
		||||
<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…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user