diff --git a/SharpbinV3.Client/src/lib/utils/misc.ts b/SharpbinV3.Client/src/lib/utils/misc.ts index 3dce5df..f3d0a51 100644 --- a/SharpbinV3.Client/src/lib/utils/misc.ts +++ b/SharpbinV3.Client/src/lib/utils/misc.ts @@ -24,6 +24,19 @@ export function extractDateFromUUIDv7(uuid?: string): Date | null { return new Date(parseInt(timestampHex, 16)); } +export function isBinaryData(buffer: ArrayBuffer | Uint8Array): boolean { + const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer); + + if (bytes.includes(0)) return true; + + try { + new TextDecoder('utf-8', { fatal: true }).decode(bytes); + return false; + } catch { + return true; + } +} + export function extractError(resData: unknown): string | null { if (!resData || typeof resData !== 'object') return null; diff --git a/SharpbinV3.Client/src/routes/+page.svelte b/SharpbinV3.Client/src/routes/+page.svelte index 79dd881..3d09a29 100644 --- a/SharpbinV3.Client/src/routes/+page.svelte +++ b/SharpbinV3.Client/src/routes/+page.svelte @@ -4,11 +4,12 @@ import { user } from '$lib/stores/user'; import { getToken } from '$lib/utils/auth'; import { onMount } from 'svelte'; - import { fly } from 'svelte/transition'; + import { fly, fade } from 'svelte/transition'; import { encryptAES } from '$lib/utils/encryption'; - import { formatBytes, formatNumber } from '$lib/utils/misc'; + import { formatBytes, formatNumber, isBinaryData } from '$lib/utils/misc'; import Dropdown from '$lib/components/Dropdown.svelte'; import { SvelteURLSearchParams } from 'svelte/reactivity'; + import { FileUp, X } from '@lucide/svelte'; export let data: PageData; @@ -20,6 +21,8 @@ let selectedVisibility = data.options?.visibilities?.[0]?.value; let password = ''; let anonymousUpload = false; + let isDragging = false; + let dragError = ''; onMount(() => { const render = () => { @@ -98,12 +101,14 @@ if (selectedVisibility === 2) { body = await encryptAES(content, password); } + const url = `/api/paste/create?${params.toString()}`; const options: RequestInit = { method: 'POST', headers: { 'Content-Type': 'text/plain' }, body }; + if ($user && !anonymousUpload) { const token = getToken(); if (token) { @@ -142,7 +147,7 @@ error = resData.message || 'Registration failed. Please try again.'; } } - setInterval(() => { + setTimeout(() => { error = ''; }, 5000); } @@ -173,24 +178,88 @@ bind:value={title} maxlength={data.options?.maxTitleLength ?? 500} /> -
- + isDragging = true; + }} + on:dragover={(e) => { + e.preventDefault(); + isDragging = true; + }} + on:dragleave={(e) => { + e.preventDefault(); + isDragging = false; + }} + on:drop={(e) => { + e.preventDefault(); + isDragging = false; + + const file = e.dataTransfer?.files[0]; + if (!file) return; + + const reader = new FileReader(); + + reader.onload = (e) => { + const arrayBuffer = e.target?.result as ArrayBuffer; + if (isBinaryData(new Uint8Array(arrayBuffer))) { + content = ''; + dragError = 'The dropped file appears to be binary and cannot be pasted as text.'; + setTimeout(() => { + dragError = ''; + }, 5000); + return; + } + const decoder = new TextDecoder(); + content = decoder.decode(arrayBuffer); + }; + + reader.readAsArrayBuffer(file); + }} + > + {#if isDragging || (dragError && dragError !== '')} +
+
+ {#if dragError && dragError !== ''} +
+ +

{dragError}

+
+ {:else} +
+ +
+ {/if} +
+
+ {/if} +
diff --git a/SharpbinV3.Client/src/routes/[id]/+page.svelte b/SharpbinV3.Client/src/routes/[id]/+page.svelte index 985d782..6e63775 100644 --- a/SharpbinV3.Client/src/routes/[id]/+page.svelte +++ b/SharpbinV3.Client/src/routes/[id]/+page.svelte @@ -28,7 +28,8 @@ formatNumber, extractDateFromUUIDv7, dateToRelativeString, - tooltip + tooltip, + isBinaryData } from '$lib/utils/misc'; import { displayNames } from '$lib/consts'; import { resolve } from '$app/paths'; @@ -47,6 +48,7 @@ let editMetadata: Paste | null = data.paste ? { ...data.paste } : null; let editError = ''; let editLoading = false; + let isBinary = false; let pasteContent = ''; let decryptedContent: string | null = null; let decryptError = ''; @@ -124,6 +126,15 @@ function renderCode(content: string | null = null) { if (!data?.paste) return; let code = content ?? decryptedContent ?? data.content ?? ''; + + const encoder = new TextEncoder(); + const buffer = encoder.encode(code).buffer; + isBinary = isBinaryData(buffer); + if (isBinary) { + contentRendered = true; + return; + } + const lang = (data.paste.syntax ?? '').toLowerCase(); let highlighted = ''; try { @@ -385,7 +396,12 @@
-
+
{formatBytes(data.paste.trueSize)}
@@ -509,53 +525,59 @@
-
- {formatNumber((editContent ?? decryptedContent ?? data.content).length)} - chars - • - {formatNumber( - (editing ? (editContent ?? '') : (decryptedContent ?? data.content)).split('\n') - .length - )} - line{(editing ? (editContent ?? '') : (decryptedContent ?? data.content)).split('\n') - .length !== 1 - ? 's' - : ''} -
+ {#if !isBinary} +
+ {formatNumber((editContent ?? decryptedContent ?? data.content).length)} + chars + • + {formatNumber( + (editing ? (editContent ?? '') : (decryptedContent ?? data.content)).split('\n') + .length + )} + line{(editing ? (editContent ?? '') : (decryptedContent ?? data.content)).split('\n') + .length !== 1 + ? 's' + : ''} +
+ {:else} +
+ {/if}
- + Copy + + {/if} + {#if !isBinary} + + {/if}
- {#if editing} + {#if isBinary} +
+

We cannot display this paste because it is not text

+
+ {:else if editing}