From 1f2fa12b8f7237660d9ca3907c7498f77bddeeb1 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Wed, 5 Jun 2024 18:05:55 -1000 Subject: [PATCH] feat: :sparkles: Add icon to non-video or audio files, with download --- .../social-elements/notes/attachment.vue | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/components/social-elements/notes/attachment.vue b/components/social-elements/notes/attachment.vue index 35fe9a5..aa35a08 100644 --- a/components/social-elements/notes/attachment.vue +++ b/components/social-elements/notes/attachment.vue @@ -8,6 +8,15 @@ controls :alt="attachment.description ?? undefined" :src="attachment.url"> Your browser does not support the video tag. + +
+ +

{{ getFilename(attachment.url) }}

+

{{ + formatBytes(Number(attachment.meta?.length)) }}

+
+
{ + if (bytes === 0) return "0 Bytes"; + const k = 1000; + const dm = 2; + const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; + const i = Math.floor(Math.log(bytes) / Math.log(k)); + return `${Number.parseFloat((bytes / k ** i).toFixed(dm))} ${sizes[i]}`; +}; + +const getFilename = (url: string) => { + // Handle proxy case + if (url.includes("/media/proxy")) { + // Decode last part of URL as base64url, which is the real URL + const realUrl = atob(url.split("/").pop() ?? ""); + return realUrl.substring(realUrl.lastIndexOf("/") + 1); + } + const path = new URL(url).pathname; + return path.substring(path.lastIndexOf("/") + 1); +}; \ No newline at end of file