Skip to content

Commit

Permalink
feat: config opt deleteonmaxviews
Browse files Browse the repository at this point in the history
  • Loading branch information
diced committed Jul 21, 2023
1 parent 7ca9a33 commit 485a1ae
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/lib/config/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const PROP_TO_ENV: Record<string, string> = {
'features.invites': 'FEATURES_INVITES',
'features.userRegistration': 'FEATURES_USER_REGISTRATION',
'features.oauthRegistration': 'FEATURES_OAUTH_REGISTRATION',
'features.deleteOnMaxViews': 'FEATURES_DELETE_ON_MAX_VIEWS',

'website.title': 'WEBSITE_TITLE',
'website.externalLinks': 'WEBSITE_EXTERNAL_LINKS',
Expand Down Expand Up @@ -99,6 +100,7 @@ export function readEnv() {
env(PROP_TO_ENV['features.invites'], 'features.invites', 'boolean'),
env(PROP_TO_ENV['features.userRegistration'], 'features.userRegistration', 'boolean'),
env(PROP_TO_ENV['features.oauthRegistration'], 'features.oauthRegistration', 'boolean'),
env(PROP_TO_ENV['features.deleteOnMaxViews'], 'features.deleteOnMaxViews', 'boolean'),

env(PROP_TO_ENV['website.title'], 'website.title', 'string'),
env(PROP_TO_ENV['website.externalLinks'], 'website.externalLinks', 'json[]'),
Expand Down Expand Up @@ -151,6 +153,7 @@ export function readEnv() {
invites: undefined,
userRegistration: undefined,
oauthRegistration: undefined,
deleteOnMaxViews: undefined,
},
website: {
title: undefined,
Expand Down
1 change: 1 addition & 0 deletions src/lib/config/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export const schema = z.object({
invites: z.boolean().default(true),
userRegistration: z.boolean().default(false),
oauthRegistration: z.boolean().default(false),
deleteOnMaxViews: z.boolean().default(true),
}),
website: z.object({
title: z.string().default('Zipline'),
Expand Down
2 changes: 2 additions & 0 deletions src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ async function main() {
await app.prepare();

if (config.files.route === '/' && config.urls.route === '/') {
logger.debug('files & urls route are both /, using catch-all route');

server.get('/:id', async (req, res) => {
const { id } = req.params;
const parsedUrl = parse(req.url!, true);
Expand Down
21 changes: 20 additions & 1 deletion src/server/routes/files.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { config } from '@/lib/config';
import { verifyPassword } from '@/lib/crypto';
import { datasource } from '@/lib/datasource';
import { prisma } from '@/lib/db';
import { log } from '@/lib/logger';
import express, { Request, Response } from 'express';
import { NextServer } from 'next/dist/server/next';
import { parse } from 'url';

const logger = log('server').c('files');

export async function filesRoute(
this: ReturnType<typeof express>,
app: NextServer,
Expand All @@ -27,7 +31,22 @@ export async function filesRoute(

if (!file) return app.render404(req, res, parsedUrl);

if (file.maxViews && file.views >= file.maxViews) return app.render404(req, res, parsedUrl);
if (file.maxViews && file.views >= file.maxViews) {
if (config.features.deleteOnMaxViews) {
await prisma.file.delete({
where: {
id: file.id,
},
});

logger.info(`${file.name} deleted due to reaching max views`, {
id: file.id,
views: file.views,
});
}

return app.render404(req, res, parsedUrl);
}

if (file.User?.view.enabled) return res.redirect(`/view/${file.name}`);

Expand Down
22 changes: 21 additions & 1 deletion src/server/routes/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { NextServer } from 'next/dist/server/next';
import express, { Request, Response } from 'express';
import { parse } from 'url';
import { prisma } from '@/lib/db';
import { config } from '@/lib/config';
import { log } from '@/lib/logger';

const logger = log('server').c('urls');

export async function urlsRoute(
this: ReturnType<typeof express>,
Expand All @@ -20,7 +24,23 @@ export async function urlsRoute(
});
if (!url) return app.render404(req, res, parsedUrl);

if (url.maxViews && url.views >= url.maxViews) return app.render404(req, res, parsedUrl);
if (url.maxViews && url.views >= url.maxViews) {
if (config.features.deleteOnMaxViews) {
await prisma.url.delete({
where: {
id: url.id,
},
});

logger.info(`${url.code} deleted due to reaching max views`, {
id: url.id,
views: url.views,
vanity: url.vanity ?? 'none',
});
}

return app.render404(req, res, parsedUrl);
}

await prisma.url.update({
where: {
Expand Down

0 comments on commit 485a1ae

Please sign in to comment.