fix: show warning when password protect

This commit is contained in:
dicedtomato 2023-03-04 04:15:24 +00:00 committed by GitHub
parent 4ef0c6021a
commit 11bca28ef5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 66 additions and 16 deletions

View file

@ -1,4 +1,15 @@
import { Alert, Box, Button, Card, Center, Group, Image, LoadingOverlay, Text } from '@mantine/core'; import {
Alert,
Box,
Button,
Card,
Center,
Group,
Image,
LoadingOverlay,
Text,
UnstyledButton,
} from '@mantine/core';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { AudioIcon, FileIcon, ImageIcon, PlayIcon } from './icons'; import { AudioIcon, FileIcon, ImageIcon, PlayIcon } from './icons';
import KaTeX from './render/KaTeX'; import KaTeX from './render/KaTeX';
@ -15,6 +26,15 @@ function PlaceholderContent({ text, Icon }) {
} }
function Placeholder({ text, Icon, ...props }) { function Placeholder({ text, Icon, ...props }) {
if (props.onClick)
return (
<UnstyledButton sx={{ height: 200 }} {...props}>
<Center sx={{ height: 200 }}>
<PlaceholderContent text={text} Icon={Icon} />
</Center>
</UnstyledButton>
);
return ( return (
<Box sx={{ height: 200 }} {...props}> <Box sx={{ height: 200 }} {...props}>
<Center sx={{ height: 200 }}> <Center sx={{ height: 200 }}>
@ -81,6 +101,17 @@ export default function Type({ file, popup = false, disableMediaPreview, ...prop
return <Placeholder Icon={FileIcon} text={`Click to view file (${file.name})`} {...props} />; return <Placeholder Icon={FileIcon} text={`Click to view file (${file.name})`} {...props} />;
} }
if (file.password) {
return (
<Placeholder
Icon={FileIcon}
text={`This file is password protected. Click to view file (${file.name})`}
onClick={() => window.open(file.url)}
{...props}
/>
);
}
return popup ? ( return popup ? (
media ? ( media ? (
{ {

View file

@ -41,7 +41,9 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) {
logger.info(`User ${user.username} (${user.id}) deleted an image ${file.name} (${file.id})`); logger.info(`User ${user.username} (${user.id}) deleted an image ${file.name} (${file.id})`);
delete file.password; // @ts-ignore
if (file.password) file.password = true;
return res.json(file); return res.json(file);
} }
} else if (req.method === 'PATCH') { } else if (req.method === 'PATCH') {
@ -57,7 +59,8 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) {
}, },
}); });
delete image.password; // @ts-ignore
if (file.password) file.password = true;
return res.json(image); return res.json(image);
} else { } else {
if (req.query.count) { if (req.query.count) {

View file

@ -89,7 +89,8 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) {
if (req.query.files) { if (req.query.files) {
for (let i = 0; i !== folder.files.length; ++i) { for (let i = 0; i !== folder.files.length; ++i) {
const file = folder.files[i]; const file = folder.files[i];
delete file.password; // @ts-ignore
if (file.password) file.password = true;
(folder.files[i] as unknown as { url: string }).url = formatRootUrl( (folder.files[i] as unknown as { url: string }).url = formatRootUrl(
config.uploader.route, config.uploader.route,
@ -123,7 +124,8 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) {
if (req.query.files) { if (req.query.files) {
for (let i = 0; i !== folder.files.length; ++i) { for (let i = 0; i !== folder.files.length; ++i) {
const file = folder.files[i]; const file = folder.files[i];
delete file.password; // @ts-ignore
if (file.password) file.password = true;
(folder.files[i] as unknown as { url: string }).url = formatRootUrl( (folder.files[i] as unknown as { url: string }).url = formatRootUrl(
config.uploader.route, config.uploader.route,
@ -217,7 +219,8 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) {
if (req.query.files) { if (req.query.files) {
for (let i = 0; i !== folder.files.length; ++i) { for (let i = 0; i !== folder.files.length; ++i) {
const file = folder.files[i]; const file = folder.files[i];
delete file.password; // @ts-ignore
if (file.password) file.password = true;
(folder.files[i] as unknown as { url: string }).url = formatRootUrl( (folder.files[i] as unknown as { url: string }).url = formatRootUrl(
config.uploader.route, config.uploader.route,
@ -232,7 +235,8 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) {
if (req.query.files) { if (req.query.files) {
for (let i = 0; i !== folder.files.length; ++i) { for (let i = 0; i !== folder.files.length; ++i) {
const file = folder.files[i]; const file = folder.files[i];
delete file.password; // @ts-ignore
if (file.password) file.password = true;
(folder.files[i] as unknown as { url: string }).url = formatRootUrl( (folder.files[i] as unknown as { url: string }).url = formatRootUrl(
config.uploader.route, config.uploader.route,

View file

@ -81,7 +81,8 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) {
const folder = folders[i]; const folder = folders[i];
for (let j = 0; j !== folders[i].files.length; ++j) { for (let j = 0; j !== folders[i].files.length; ++j) {
const file = folder.files[j]; const file = folder.files[j];
delete file.password; // @ts-ignore
if (file.password) file.password = true;
(folder.files[j] as unknown as { url: string }).url = formatRootUrl( (folder.files[j] as unknown as { url: string }).url = formatRootUrl(
config.uploader.route, config.uploader.route,

View file

@ -58,6 +58,7 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) {
maxViews: number; maxViews: number;
views: number; views: number;
folderId: number; folderId: number;
password: string | boolean;
}[] = await prisma.file.findMany({ }[] = await prisma.file.findMany({
where, where,
orderBy: { orderBy: {
@ -73,13 +74,17 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) {
views: true, views: true,
maxViews: true, maxViews: true,
folderId: true, folderId: true,
password: true,
}, },
skip: page ? (Number(page) - 1) * pageCount : undefined, skip: page ? (Number(page) - 1) * pageCount : undefined,
take: page ? pageCount : undefined, take: page ? pageCount : undefined,
}); });
for (let i = 0; i !== files.length; ++i) { for (let i = 0; i !== files.length; ++i) {
(files[i] as unknown as { url: string }).url = formatRootUrl(config.uploader.route, files[i].name); const file = files[i];
if (file.password) file.password = true;
(file as unknown as { url: string }).url = formatRootUrl(config.uploader.route, file.name);
} }
return res.json(files); return res.json(files);

View file

@ -35,8 +35,8 @@ export default function Folder({ title, folder }: Props) {
<title>{full_title}</title> <title>{full_title}</title>
</Head> </Head>
<Container size='lg'> <Container size='lg'>
<Title align='center' my='lg'> <Title size={50} align='center' my='lg'>
Viewing folder: {folder.name} {folder.name}
</Title> </Title>
<SimpleGrid <SimpleGrid
my='md' my='md'
@ -80,6 +80,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
id: true, id: true,
views: true, views: true,
createdAt: true, createdAt: true,
password: true,
}, },
}, },
user: { user: {
@ -101,6 +102,9 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
folder.files[j].name folder.files[j].name
); );
// @ts-ignore
if (folder.files[j].password) folder.files[j].password = true;
(folder.files[j].createdAt as unknown) = folder.files[j].createdAt.toString(); (folder.files[j].createdAt as unknown) = folder.files[j].createdAt.toString();
} }

View file

@ -197,7 +197,8 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
}; };
else if (prismRender && file.password) { else if (prismRender && file.password) {
const pass = file.password ? true : false; const pass = file.password ? true : false;
delete file.password; // @ts-ignore
if (file.password) file.password = true;
return { return {
props: { props: {
image: file, image: file,
@ -214,7 +215,8 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
const data = await datasource.get(file.name); const data = await datasource.get(file.name);
if (!data) return { notFound: true }; if (!data) return { notFound: true };
delete file.password; // @ts-ignore
if (file.password) file.password = true;
return { return {
props: { props: {
@ -223,14 +225,14 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
}, },
}; };
} }
// @ts-ignore
if (file.password) file.password = true;
const pass = file.password ? true : false;
delete file.password;
return { return {
props: { props: {
file, file,
user, user,
pass, pass: file.password ? true : false,
}, },
}; };
}; };