feat: paste compression & verification enforcement settings; enforce 2fa on admins setting; increase paste validation; anonymous uploads

fix: minor styling issue on main elements; resolve lint issue
refactor: remove 2FA badge on user pages; enhance paste creation flow; body background color always neutral-950; improve paste creation paste info (lines, cchars etc) for mobile
This commit is contained in:
2026-01-08 15:25:42 -05:00
parent fabef95159
commit 9f55aa1b22
16 changed files with 186 additions and 75 deletions
+112 -34
View File
@@ -3,10 +3,12 @@
import { displayNames } from '$lib/consts';
import { user } from '$lib/stores/user';
import { getToken } from '$lib/utils/auth';
import { onMount } from 'svelte';
import { fly } from 'svelte/transition';
import { encryptAES } from '$lib/utils/encryption';
import { formatBytes } from '$lib/utils/misc';
import { formatBytes, formatNumber } from '$lib/utils/misc';
import Dropdown from '$lib/components/Dropdown.svelte';
import { SvelteURLSearchParams } from 'svelte/reactivity';
export let data: PageData;
@@ -17,6 +19,32 @@
let expiresIn: number = 0;
let selectedVisibility = data.options?.visibilities?.[0]?.value;
let password = '';
let anonymousUpload = false;
onMount(() => {
const render = () => {
if (
window.turnstile &&
data.options?.requiresVerification &&
data.auth?.cf_turnstile_site_key
) {
window.turnstile.render('.cf-turnstile', {
sitekey: data.auth.cf_turnstile_site_key,
theme: 'dark'
});
}
};
if (window.turnstile) {
render();
} else {
window.addEventListener('turnstile:loaded', render, { once: true });
}
return () => {
window.removeEventListener('turnstile:loaded', render);
};
});
const syntaxOptions =
data.options?.syntaxes?.map((lang: string) => ({
@@ -48,12 +76,24 @@
async function handleSubmit(event: Event) {
event.preventDefault();
const params = new URLSearchParams({
const params = new SvelteURLSearchParams({
title: title.trim(),
syntax: selectedSyntax,
visibility: selectedVisibility.toString(),
expiresAt: expiresIn > 0 ? (Date.now() + Number(expiresIn)).toString() : '0'
});
if (
data.options?.requiresVerification &&
data.auth?.cf_turnstile_site_key &&
window.turnstile
) {
const token = window.turnstile.getResponse();
if (token) {
params.append('token', token);
}
}
let body = content;
if (selectedVisibility === 2) {
body = await encryptAES(content, password);
@@ -64,7 +104,7 @@
headers: { 'Content-Type': 'text/plain' },
body
};
if ($user) {
if ($user && !anonymousUpload) {
const token = getToken();
if (token) {
options.headers = {
@@ -83,10 +123,24 @@
}
window.location.href = `/${result.id}`;
} else {
if (window.turnstile) {
window.turnstile.reset();
}
if (response.status === 403) {
error = 'You are banned from creating new pastes.';
} else {
error = 'Failed to create paste. Please try again.';
const resData = await response.json();
if (resData.errors) {
const messages = Object.values(resData.errors).flat();
error = messages.join('\n');
} else {
if (resData.message === 'Verification failed.') {
if (window.turnstile) {
window.turnstile.reset();
}
}
error = resData.message || 'Registration failed. Please try again.';
}
}
setInterval(() => {
error = '';
@@ -105,7 +159,7 @@
</svelte:head>
<main
class="flex min-h-[calc(100vh-60px)] w-full flex-col items-center justify-center bg-neutral-950 pt-5 text-white"
class="flex min-h-[calc(100vh-60px)] w-full flex-col items-center justify-center pt-5 pb-5 text-white"
>
<div class="w-[95%] max-w-7xl rounded border-2 border-neutral-800 bg-neutral-900 p-6">
<h1 class="mb-6 text-center text-4xl font-bold">Create a Paste</h1>
@@ -138,51 +192,75 @@
}}
></textarea>
<div
class="mb-4 flex items-center h-8 justify-between rounded-b border border-neutral-700 bg-neutral-800 p-1 pl-2 pr-2 text-sm text-neutral-400 monospace"
class="monospace mb-4 flex h-12 items-center justify-between rounded-b border border-neutral-700 bg-neutral-800 p-1 pr-2 pl-2 text-sm text-neutral-400 md:h-8"
>
<div>
{content.split('\n').length} line{content.split('\n').length !== 1 ? 's' : ''}, {content.length}
character{content.length !== 1 ? 's' : ''}
<div class="flex flex-col md:flex-row md:gap-2">
<span>
{content.split('\n').length} line{content.split('\n').length !== 1 ? 's' : ''}
</span>
<span class="hidden text-neutral-600 md:block">•</span>
<span>{formatNumber(content.length)} char{content.length !== 1 ? 's' : ''}</span>
</div>
<div class="flex flex-col text-center md:flex-row md:gap-2">
<span class="border-b border-neutral-600 md:border-0">
{formatBytes(new TextEncoder().encode(content).length)}
</span>
<span class="hidden md:block">/</span>
{formatBytes(data.options?.maxPasteSize ?? 0)}
</div>
{#if data.options?.maxPasteSize}
{formatBytes(new TextEncoder().encode(content).length)} / {formatBytes(
data.options.maxPasteSize
)}
{:else}
{formatBytes(new TextEncoder().encode(content).length)}
{/if}
</div>
</div>
<Dropdown
options={syntaxOptions}
bind:value={selectedSyntax}
placeholder="Select syntax..."
searchable={true}
/>
<Dropdown
options={expiresOptions}
bind:value={expiresIn}
placeholder="Select expiration..."
/>
<Dropdown
options={visibilityOptions}
bind:value={selectedVisibility}
placeholder="Select visibility..."
/>
<div class="grid grid-cols-1 md:grid-cols-3 md:gap-2">
<Dropdown
options={syntaxOptions}
bind:value={selectedSyntax}
placeholder="Select syntax..."
searchable={true}
/>
<Dropdown
options={expiresOptions}
bind:value={expiresIn}
placeholder="Select expiration..."
/>
<Dropdown
options={visibilityOptions}
bind:value={selectedVisibility}
placeholder="Select visibility..."
/>
</div>
{#if selectedVisibility === 2}
<input
type="password"
placeholder="Password for private paste"
class="mb-4 w-full rounded border border-neutral-700 bg-neutral-800 p-2"
class="w-full rounded border border-neutral-700 bg-neutral-800 p-2"
spellcheck="false"
autocomplete="off"
bind:value={password}
/>
{/if}
{#if $user}
<label
class="flex w-full items-center gap-2 rounded border border-neutral-700 bg-neutral-800 p-2"
>
<input
type="checkbox"
bind:checked={anonymousUpload}
class="h-4 w-4 rounded border-neutral-600 bg-neutral-700 text-neutral-500"
/>
<span>Upload anonymously</span>
</label>
{/if}
{#if data.options?.requiresVerification && data.auth?.cf_turnstile_site_key}
<div
class="mt-2 flex w-auto items-center justify-center rounded border border-neutral-700 bg-neutral-800 p-2 pb-1"
>
<div class="cf-turnstile" data-sitekey={data.auth.cf_turnstile_site_key}></div>
</div>
{/if}
<button
type="submit"
disabled={$user?.roles && $user.roles.includes(403)}
class="w-full cursor-pointer rounded bg-neutral-700 px-4 py-2 font-semibold text-white transition-colors duration-200 hover:bg-neutral-800 active:bg-neutral-900 disabled:cursor-not-allowed disabled:bg-neutral-950/50 disabled:text-neutral-400"
class="mt-2 w-full cursor-pointer rounded bg-neutral-700 px-4 py-2 font-semibold text-white transition-colors duration-200 hover:bg-neutral-800 active:bg-neutral-900 disabled:cursor-not-allowed disabled:bg-neutral-950/50 disabled:text-neutral-400"
>Create Paste</button
>
{#if error}