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

Fix dump #441

Merged
merged 9 commits into from
Jul 26, 2023
2 changes: 1 addition & 1 deletion src/components/pages/Invites.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
11 changes: 6 additions & 5 deletions src/lib/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 6 additions & 16 deletions src/lib/queries/folders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,17 @@ export const useFolders = (query: { [key: string]: string } = {}) => {
const queryString = queryBuilder.toString();

return useQuery<UserFoldersResponse[]>(['folders', queryString], async () => {
return fetch('/api/user/folders?' + queryString)
.then((res) => res.json() as Promise<UserFoldersResponse[]>)
.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<UserFoldersResponse[]>
);
});
};

export const useFolder = (id: string, withFiles = false) => {
return useQuery<UserFoldersResponse>(['folder', id], async () => {
return fetch('/api/user/folders/' + id + (withFiles ? '?files=true' : ''))
.then((res) => res.json() as Promise<UserFoldersResponse>)
.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<UserFoldersResponse>
);
});
};

Expand Down
21 changes: 11 additions & 10 deletions src/pages/api/user/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
14 changes: 1 addition & 13 deletions src/pages/auth/register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
Expand Down Expand Up @@ -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,
},
};
Expand Down
6 changes: 3 additions & 3 deletions src/server/decorators/rawFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ async function thumbs(this: FastifyInstance) {
workerData: {
videos: chunk,
},
});
}).on('error', (err) => logger.child('thumbnail').error(err));
}
}

Expand Down