diff --git a/src/components/pages/Invites.tsx b/src/components/pages/Invites.tsx index f3f66b9b0..3baad6b2d 100644 --- a/src/components/pages/Invites.tsx +++ b/src/components/pages/Invites.tsx @@ -50,7 +50,7 @@ function CreateInviteModal({ open, setOpen, updateInvites }) { if (!expires.includes(values.expires)) return form.setFieldError('expires', 'Invalid expiration'); if (values.count < 1 || values.count > 100) return form.setFieldError('count', 'Must be between 1 and 100'); - const expiresAt = values.expires === 'never' ? null : expireReadToDate(values.expires); + const expiresAt = expireReadToDate(values.expires); setOpen(false); diff --git a/src/lib/discord.ts b/src/lib/discord.ts index a73db8f51..a37fc758f 100644 --- a/src/lib/discord.ts +++ b/src/lib/discord.ts @@ -28,10 +28,10 @@ export function parseContent( } export async function sendUpload(user: User, file: File, raw_link: string, link: string) { - if (!config.discord.upload) return; - if (!config.discord.url && !config.discord.upload.url) return; + if (!config.discord.upload) return logger.debug('no discord upload config, no webhook sent'); + if (!config.discord.url && !config.discord.upload.url) + return logger.debug('no discord url, no webhook sent'); - logger.debug(`discord config:\n${JSON.stringify(config.discord)}`); const parsed = parseContent(config.discord.upload, { file, user, @@ -97,8 +97,9 @@ export async function sendUpload(user: User, file: File, raw_link: string, link: } export async function sendShorten(user: User, url: Url, link: string) { - if (!config.discord.shorten) return; - if (!config.discord.url && !config.discord.shorten.url) return; + if (!config.discord.shorten) return logger.debug('no discord shorten config, no webhook sent'); + if (!config.discord.url && !config.discord.shorten.url) + return logger.debug('no discord url, no webhook sent'); const parsed = parseContent(config.discord.shorten, { url, diff --git a/src/lib/queries/folders.ts b/src/lib/queries/folders.ts index f73b342dc..c076a8b6b 100644 --- a/src/lib/queries/folders.ts +++ b/src/lib/queries/folders.ts @@ -17,27 +17,17 @@ export const useFolders = (query: { [key: string]: string } = {}) => { const queryString = queryBuilder.toString(); return useQuery(['folders', queryString], async () => { - return fetch('/api/user/folders?' + queryString) - .then((res) => res.json() as Promise) - .then((data) => - data.map((x) => ({ - ...x, - createdAt: new Date(x.createdAt).toLocaleString(), - updatedAt: new Date(x.updatedAt).toLocaleString(), - })) - ); + return fetch('/api/user/folders?' + queryString).then( + (res) => res.json() as Promise + ); }); }; export const useFolder = (id: string, withFiles = false) => { return useQuery(['folder', id], async () => { - return fetch('/api/user/folders/' + id + (withFiles ? '?files=true' : '')) - .then((res) => res.json() as Promise) - .then((data) => ({ - ...data, - createdAt: new Date(data.createdAt).toLocaleString(), - updatedAt: new Date(data.updatedAt).toLocaleString(), - })); + return fetch('/api/user/folders/' + id + (withFiles ? '?files=true' : '')).then( + (res) => res.json() as Promise + ); }); }; diff --git a/src/pages/api/user/check.ts b/src/pages/api/user/check.ts index 257d3c433..d8925612b 100644 --- a/src/pages/api/user/check.ts +++ b/src/pages/api/user/check.ts @@ -3,18 +3,19 @@ import prisma from 'lib/prisma'; import { NextApiReq, NextApiRes, withZipline } from 'middleware/withZipline'; async function handler(req: NextApiReq, res: NextApiRes) { - if (!config.features.user_registration && !req.body.code) - return res.badRequest('user registration is disabled'); - else if (!config.features.invites && req.body.code) return res.forbidden('user/invites are disabled'); + const { code, username } = req.body as { code?: string; username?: string }; - if (!req.body?.code) return res.badRequest('no code'); - if (!req.body?.username) return res.badRequest('no username'); + if (!config.features.user_registration && !code) return res.badRequest('user registration is disabled'); + else if (!config.features.invites && code) return res.forbidden('user invites are disabled'); - const { code, username } = req.body as { code: string; username: string }; - const invite = await prisma.invite.findUnique({ - where: { code }, - }); - if (!invite) return res.badRequest('invalid invite code'); + if (config.features.invites && !code) return res.badRequest('no code'); + else if (config.features.invites && code) { + const invite = await prisma.invite.findUnique({ + where: { code }, + }); + if (!invite) return res.badRequest('invalid invite code'); + } + if (!username) return res.badRequest('no username'); const user = await prisma.user.findFirst({ where: { username }, diff --git a/src/pages/auth/register.tsx b/src/pages/auth/register.tsx index 8a75a5157..c62dc3f9f 100644 --- a/src/pages/auth/register.tsx +++ b/src/pages/auth/register.tsx @@ -6,14 +6,13 @@ import useFetch from 'hooks/useFetch'; import config from 'lib/config'; import prisma from 'lib/prisma'; import { userSelector } from 'lib/recoil/user'; -import { randomChars } from 'lib/util'; import { GetServerSideProps } from 'next'; import Head from 'next/head'; import { useRouter } from 'next/router'; import { useState } from 'react'; import { useSetRecoilState } from 'recoil'; -export default function Register({ code, title, user_registration }) { +export default function Register({ code = undefined, title, user_registration }) { const [active, setActive] = useState(0); const [username, setUsername] = useState(''); const [usernameError, setUsernameError] = useState(''); @@ -196,20 +195,9 @@ export const getServerSideProps: GetServerSideProps = async (context) => { notFound: true, }; - const code = randomChars(4); - const temp = await prisma.invite.create({ - data: { - code, - createdById: 1, - }, - }); - - logger.debug(`request to access user registration, creating temporary invite ${JSON.stringify(temp)}`); - return { props: { title: config.website.title, - code, user_registration: true, }, }; diff --git a/src/server/decorators/rawFile.ts b/src/server/decorators/rawFile.ts index 80783451c..b3c4a194a 100644 --- a/src/server/decorators/rawFile.ts +++ b/src/server/decorators/rawFile.ts @@ -11,7 +11,7 @@ function rawFileDecorator(fastify: FastifyInstance, _, done) { done(); async function rawFile(this: FastifyReply, id: string) { - const { download, compress } = this.request.query as { download?: string; compress?: boolean }; + const { download, compress = 'false' } = this.request.query as { download?: string; compress?: string }; const data = await this.server.datasource.get(id); if (!data) return this.notFound(); @@ -22,11 +22,11 @@ function rawFileDecorator(fastify: FastifyInstance, _, done) { if ( this.server.config.core.compression.enabled && - compress && + compress?.match(/^true$/i) && !this.request.headers['X-Zipline-NoCompress'] && !!this.request.headers['accept-encoding'] ) - if (size > this.server.config.core.compression.threshold) + if (size > this.server.config.core.compression.threshold && mimetype.match(/^(image|video|text)/)) return this.send(useCompress.call(this, data)); this.header('Content-Length', size); return this.send(data); diff --git a/src/server/index.ts b/src/server/index.ts index 73cd082ae..9f268dcc6 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -260,7 +260,7 @@ async function thumbs(this: FastifyInstance) { workerData: { videos: chunk, }, - }); + }).on('error', (err) => logger.child('thumbnail').error(err)); } }