Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for custom paths #30

Merged
merged 2 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ cli
**/.env
.env.example
k8s
.github
.github
.yarn
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ MAIL_PASSWORD=
MAIL_FROM=YABin <yabin@sohamsen.me>

PUBLIC_REGISRATION_ENABLED=true
PUBLIC_CUSTOM_PATHS_ENABLED=true
PUBLIC_URL=http://localhost:5173
ORIGIN=http://localhost:5173
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ node_modules
!.env.example
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
.vercel
.vercel
.yarn
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ Well, cause no pastebin I could find had ALL of the following features:
- View raw pastes. Normally, encrypted pastebins do not have this. With this site, you can either get the Base64-encoded encrypted paste, or decrypt it on the server side (even with the password) and get the raw paste.
- Keyboard shortcuts!
- And of course, being fully open-source and easily self-hostable.
- **NEW** Ability to edit pastes after creation, and a dashboard for viewing all your pastes.
- Ability to edit pastes after creation, and a dashboard for viewing all your pastes.
- **NEW** Feature to use custom path names.
- **Comes with a CLI tool to create and read pastes from the command line!**
- **It can even be run on edge servers and in serverless environments!**

## API Documentation

Expand Down Expand Up @@ -59,6 +59,8 @@ Remember to modify `SALT` to something secure if you plan on using user accounts

You can disable or enable public registration by modifying the `PUBLIC_REGISRATION_ENABLED` variable to `true` or `false`.

You can enable custom paste paths for everyone with the variable `PUBLIC_CUSTOM_PATHS_ENABLED`. If it is `false`, only users who are logged in can use custom paths.

By default, if no e-mail services are configured, all user accounts will be marked as validated. To enable e-mail validation, please configure the `MAIL_*` variables.

#### Locally
Expand Down
6 changes: 2 additions & 4 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface PasteConfig {
encrypted?: boolean;
expiresAfter?: number;
burnAfterRead?: boolean;
customPath?: string;
}

export interface Paste {
Expand All @@ -24,10 +25,7 @@ export interface PasteCreateResponse {
data?: {
key: string;
};
error?: {
message: string;
code: number;
};
error?: string;
}

export interface PastePatchResponse {
Expand Down
12 changes: 12 additions & 0 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,15 @@
_sessionStorage?.removeItem('contentBackup');
await goto(`/${json.data?.key}${urlParams}`);
} else {
alert(json.error);
console.log(json);
}
} catch (e) {
console.log(e);
}
};

$: config.customPath = config.customPath ? config.customPath.substring(0, 16) : undefined;
</script>

<div class="sm:hidden flex flex-row gap-2 items-center px-4 py-2">
Expand Down Expand Up @@ -242,6 +245,15 @@
--border="0"
/>

{#if env.PUBLIC_CUSTOM_PATHS_ENABLED === 'true' || data.loggedIn}
<input
type="text"
class="bg-dark px-2 py-1 w-full"
placeholder="Custom Path"
bind:value={config.customPath}
/>
{/if}

<div>
<label for="encrypted" class="py-1">Encrypted?</label>
<input id="encrypted" type="checkbox" bind:checked={config.encrypted} />
Expand Down
30 changes: 22 additions & 8 deletions src/routes/api/paste/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Paste, PasteCreateResponse, PastePatch, PastePatchResponse } from
import prisma from '@db';
import { getPaste } from '$lib/server/services.js';
import { getUserIdFromCookie } from '$lib/server/auth';
import { env } from '$env/dynamic/public';

export const GET: RequestHandler = async ({ url }) => {
const key = url.searchParams.get('key');
Expand Down Expand Up @@ -40,15 +41,28 @@ export const POST: RequestHandler = async ({ cookies, request }) => {

const userId = await getUserIdFromCookie(cookies);

let attempts = 0;
let keyLength = 5;
let key = randomString(keyLength);
while (await prisma.paste.findUnique({ where: { key } })) {
let key: string | undefined = undefined;
if (config?.customPath && (env.PUBLIC_CUSTOM_PATHS_ENABLED === 'true' || userId)) {
key = config.customPath.substring(0, 16);

if (await prisma.paste.findUnique({ where: { key } })) {
return json({ success: false, error: 'Custom path already exists' } as PasteCreateResponse, {
status: 400
});
}
}

if (!key) {
let attempts = 0;
let keyLength = 5;
key = randomString(keyLength);
attempts++;
if (attempts > 1) {
keyLength++;
attempts = 0;
while (await prisma.paste.findUnique({ where: { key } })) {
key = randomString(keyLength);
attempts++;
if (attempts > 1) {
keyLength++;
attempts = 0;
}
}
}

Expand Down
Loading
Loading