Skip to content

Commit

Permalink
Added paste expiry
Browse files Browse the repository at this point in the history
  • Loading branch information
Soham Sen committed Jun 13, 2023
1 parent c39d06b commit df589ee
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 8 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@sveltejs/adapter-auto": "^2.0.0",
"@sveltejs/adapter-node": "^1.2.4",
"@sveltejs/kit": "^1.5.0",
"@types/node-cron": "^3.0.7",
"@types/prismjs": "^1.26.0",
"@types/sanitize-html": "^2.9.0",
"@typescript-eslint/eslint-plugin": "^5.45.0",
Expand All @@ -39,6 +40,7 @@
"dependencies": {
"@prisma/client": "^4.15.0",
"base64-js": "^1.5.1",
"node-cron": "^3.0.2",
"prism-themes": "^1.9.0",
"prismjs": "^1.29.0",
"sanitize-html": "^2.10.0"
Expand Down
15 changes: 11 additions & 4 deletions src/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
@tailwind utilities;

:root {
--color-primary: #E5E1D1;
--color-background: #101419;
--color-dark: rgba(0, 0, 0, 0.4);
--color-primary: #e5e1d1;
--color-background: #101419;
--color-dark: rgba(0, 0, 0, 0.7);
}

body {
@apply bg-background;
@apply text-primary;
@apply text-primary;
}

@layer utilities {
input[type='number']::-webkit-inner-spin-button,
input[type='number']::-webkit-outer-spin-button {
@apply appearance-none;
}
}
8 changes: 8 additions & 0 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { deleteExpiredPastes } from '$lib/server/services';
import cron from 'node-cron';

export async function handle({ event, resolve }) {
const searchParams = event.url.searchParams;
if (searchParams.get('r') !== null || searchParams.get('raw') !== null) {
Expand All @@ -6,3 +9,8 @@ export async function handle({ event, resolve }) {

return await resolve(event);
}

// Cron-job to delete expired pastes
cron.schedule('*/5 * * * *', async () => {
await deleteExpiredPastes();
});
12 changes: 11 additions & 1 deletion src/lib/server/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@ export async function getPaste(key: string) {
data: { readCount: { increment: 1 } }
});

let { expiresCount, readCount } = data;
const { expiresCount, readCount } = data;
if (expiresCount !== null && expiresCount < readCount) {
await prisma.paste.delete({ where: { key } });
throw error(404, 'Not found');
}

return data;
}

export async function deleteExpiredPastes() {
await prisma.paste.deleteMany({
where: {
expiresAt: {
lt: new Date()
}
}
});
}
2 changes: 1 addition & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface PasteConfig {
language: string;
encrypted: boolean;
expiresAfter: string;
expiresAfter: number;
burnAfterRead: boolean;
}

Expand Down
71 changes: 70 additions & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,55 @@
const initialConfig: PasteConfig = {
language: 'plaintext',
encrypted: true,
expiresAfter: 'never',
expiresAfter: 5,
burnAfterRead: false
};
let expiresAfter: {
days?: number;
hours?: number;
minutes?: number;
} = {};
$: {
if (expiresAfter.days) {
expiresAfter.days = Math.max(0, Math.round(expiresAfter.days));
}
if (expiresAfter.hours) {
expiresAfter.hours = Math.max(0, Math.round(expiresAfter.hours));
if (expiresAfter.hours > 23) {
expiresAfter.days ??= 0;
expiresAfter.days += Math.floor(expiresAfter.hours / 24);
expiresAfter.hours = expiresAfter.hours % 24;
}
}
if (expiresAfter.minutes) {
expiresAfter.minutes = Math.max(0, Math.round(expiresAfter.minutes));
if (expiresAfter.minutes > 59) {
expiresAfter.days ??= 0;
expiresAfter.hours ??= 0;
expiresAfter.days += Math.floor(expiresAfter.minutes / 1440);
expiresAfter.hours += Math.floor((expiresAfter.minutes % 1440) / 60);
expiresAfter.minutes = expiresAfter.minutes % 60;
}
if (
!expiresAfter.days &&
!expiresAfter.hours &&
expiresAfter.minutes > 0 &&
expiresAfter.minutes < 5
) {
expiresAfter.minutes = 5;
}
}
config.expiresAfter =
((expiresAfter.days ?? 0) * 1440 +
(expiresAfter.hours ?? 0) * 60 +
(expiresAfter.minutes ?? 0)) *
60;
}
let inputRef: HTMLTextAreaElement;
let placeholderRef: HTMLDivElement;
let cmdKey = 'Ctrl';
Expand Down Expand Up @@ -212,6 +257,30 @@
<input id="burn" type="checkbox" bind:checked={config.burnAfterRead} />
</div>

<div class="w-full">
<span>Expires in:</span>
<div class="grid grid-cols-3 gap-2 justify-center items-center">
<input
type="number"
class="bg-dark py-1 text-center"
placeholder="DD"
bind:value={expiresAfter.days}
/>
<input
type="number"
class="bg-dark py-1 text-center"
placeholder="HH"
bind:value={expiresAfter.hours}
/>
<input
type="number"
class="bg-dark py-1 text-center"
placeholder="MM"
bind:value={expiresAfter.minutes}
/>
</div>
</div>

<a class="underline underline-offset-4 px-2 py-1" href="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/Yureien/YABin">
Source Code
</a>
Expand Down
3 changes: 2 additions & 1 deletion src/routes/api/create/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export async function POST({ request }) {
encrypted: config.encrypted,
passwordProtected,
expiresCount: config.burnAfterRead ? 1 : null,
initVector
initVector,
expiresAt: config.expiresAfter ? new Date(Date.now() + config.expiresAfter * 1000) : null,
}
});

Expand Down
17 changes: 17 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,11 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.12.tgz#d70faba7039d5fca54c83c7dbab41051d2b6f6cb"
integrity sha512-Hr5Jfhc9eYOQNPYO5WLDq/n4jqijdHNlDXjuAQkkt+mWdQR+XJToOHrsD4cPaMXpn6KO7y2+wM8AZEs8VpBLVA==

"@types/node-cron@^3.0.7":
version "3.0.7"
resolved "https://registry.yarnpkg.com/@types/node-cron/-/node-cron-3.0.7.tgz#978bf75f7247385c61d23b6a060ba9eedb03e2f4"
integrity sha512-9PuLtBboc/+JJ7FshmJWv769gDonTpItN0Ol5TMwclpSQNjVyB2SRxSKBcTtbSysSL5R7Oea06kTTFNciCoYwA==

"@types/prismjs@^1.26.0":
version "1.26.0"
resolved "https://registry.yarnpkg.com/@types/prismjs/-/prismjs-1.26.0.tgz#a1c3809b0ad61c62cac6d4e0c56d610c910b7654"
Expand Down Expand Up @@ -1445,6 +1450,13 @@ natural-compare@^1.4.0:
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
integrity sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==

node-cron@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/node-cron/-/node-cron-3.0.2.tgz#bb0681342bd2dfb568f28e464031280e7f06bd01"
integrity sha512-iP8l0yGlNpE0e6q1o185yOApANRe47UPbLf4YxfbiNHt/RU5eBcGB/e0oudruheSf+LQeDMezqC5BVAb5wwRcQ==
dependencies:
uuid "8.3.2"

node-releases@^2.0.12:
version "2.0.12"
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-2.0.12.tgz#35627cc224a23bfb06fb3380f2b3afaaa7eb1039"
Expand Down Expand Up @@ -2054,6 +2066,11 @@ util-deprecate@^1.0.2:
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
integrity sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==

[email protected]:
version "8.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==

vite@^4.3.0:
version "4.3.9"
resolved "https://registry.yarnpkg.com/vite/-/vite-4.3.9.tgz#db896200c0b1aa13b37cdc35c9e99ee2fdd5f96d"
Expand Down

0 comments on commit df589ee

Please sign in to comment.