1
0
mirror of https://github.com/stonith404/pingvin-share.git synced 2024-10-02 09:30:10 +02:00

feature: Added "never" expiration date

This commit is contained in:
Steve Tautonico 2022-10-12 16:59:04 -04:00
parent 69ee88aebc
commit 56349c6f4c
No known key found for this signature in database
GPG Key ID: 6422E5D217FC628B
9 changed files with 406 additions and 390 deletions

View File

@ -2,23 +2,28 @@ import { Injectable } from "@nestjs/common";
import { Cron } from "@nestjs/schedule"; import { Cron } from "@nestjs/schedule";
import { FileService } from "src/file/file.service"; import { FileService } from "src/file/file.service";
import { PrismaService } from "src/prisma/prisma.service"; import { PrismaService } from "src/prisma/prisma.service";
import * as moment from "moment";
@Injectable() @Injectable()
export class JobsService { export class JobsService {
constructor( constructor(
private prisma: PrismaService, private prisma: PrismaService,
private fileService: FileService private fileService: FileService
) {} ) {
}
@Cron("0 * * * *") @Cron("0 * * * *")
async deleteExpiredShares() { async deleteExpiredShares() {
const expiredShares = await this.prisma.share.findMany({ const expiredShares = await this.prisma.share.findMany({
where: { expiration: { lt: new Date() } }, where: {
// We want to remove only shares that have an expiration date less than the current date, but not 0
AND: [{expiration: {lt: new Date()}}, {expiration: {not: moment(0).toDate()}}]
},
}); });
for (const expiredShare of expiredShares) { for (const expiredShare of expiredShares) {
await this.prisma.share.delete({ await this.prisma.share.delete({
where: { id: expiredShare.id }, where: {id: expiredShare.id},
}); });
await this.fileService.deleteAllFiles(expiredShare.id); await this.fileService.deleteAllFiles(expiredShare.id);
@ -30,7 +35,7 @@ export class JobsService {
@Cron("0 * * * *") @Cron("0 * * * *")
async deleteExpiredRefreshTokens() { async deleteExpiredRefreshTokens() {
const expiredShares = await this.prisma.refreshToken.deleteMany({ const expiredShares = await this.prisma.refreshToken.deleteMany({
where: { expiresAt: { lt: new Date() } }, where: {expiresAt: {lt: new Date()}},
}); });
console.log(`job: deleted ${expiredShares.count} expired refresh tokens`); console.log(`job: deleted ${expiredShares.count} expired refresh tokens`);
} }

View File

@ -33,7 +33,7 @@ export class ShareSecurityGuard implements CanActivate {
include: { security: true }, include: { security: true },
}); });
if (!share || moment().isAfter(share.expiration)) if (!share || (moment().isAfter(share.expiration) && moment(share.expiration).unix() !== 0))
throw new NotFoundException("Share not found"); throw new NotFoundException("Share not found");
if (!share.security) return true; if (!share.security) return true;

View File

@ -22,7 +22,8 @@ export class ShareService {
private fileService: FileService, private fileService: FileService,
private config: ConfigService, private config: ConfigService,
private jwtService: JwtService private jwtService: JwtService
) {} ) {
}
async create(share: CreateShareDTO, user: User) { async create(share: CreateShareDTO, user: User) {
if (!(await this.isShareIdAvailable(share.id)).isAvailable) if (!(await this.isShareIdAvailable(share.id)).isAvailable)
@ -35,7 +36,10 @@ export class ShareService {
share.security.password = await argon.hash(share.security.password); share.security.password = await argon.hash(share.security.password);
} }
const expirationDate = moment() // We have to add an exception for "never" (since moment won't like that)
let expirationDate;
if (share.expiration !== "never") {
expirationDate = moment()
.add( .add(
share.expiration.split("-")[0], share.expiration.split("-")[0],
share.expiration.split("-")[1] as moment.unitOfTime.DurationConstructor share.expiration.split("-")[1] as moment.unitOfTime.DurationConstructor
@ -45,13 +49,16 @@ export class ShareService {
// Throw error if expiration date is now // Throw error if expiration date is now
if (expirationDate.setMilliseconds(0) == new Date().setMilliseconds(0)) if (expirationDate.setMilliseconds(0) == new Date().setMilliseconds(0))
throw new BadRequestException("Invalid expiration date"); throw new BadRequestException("Invalid expiration date");
} else {
expirationDate = moment(0).toDate();
}
return await this.prisma.share.create({ return await this.prisma.share.create({
data: { data: {
...share, ...share,
expiration: expirationDate, expiration: expirationDate,
creator: { connect: { id: user.id } }, creator: {connect: {id: user.id}},
security: { create: share.security }, security: {create: share.security},
}, },
}); });
} }
@ -59,9 +66,9 @@ export class ShareService {
async createZip(shareId: string) { async createZip(shareId: string) {
const path = `./data/uploads/shares/${shareId}`; const path = `./data/uploads/shares/${shareId}`;
const files = await this.prisma.file.findMany({ where: { shareId } }); const files = await this.prisma.file.findMany({where: {shareId}});
const archive = archiver("zip", { const archive = archiver("zip", {
zlib: { level: 9 }, zlib: {level: 9},
}); });
const writeStream = fs.createWriteStream(`${path}/archive.zip`); const writeStream = fs.createWriteStream(`${path}/archive.zip`);
@ -77,7 +84,7 @@ export class ShareService {
async complete(id: string) { async complete(id: string) {
const moreThanOneFileInShare = const moreThanOneFileInShare =
(await this.prisma.file.findMany({ where: { shareId: id } })).length != 0; (await this.prisma.file.findMany({where: {shareId: id}})).length != 0;
if (!moreThanOneFileInShare) if (!moreThanOneFileInShare)
throw new BadRequestException( throw new BadRequestException(
@ -85,24 +92,28 @@ export class ShareService {
); );
this.createZip(id).then(() => this.createZip(id).then(() =>
this.prisma.share.update({ where: { id }, data: { isZipReady: true } }) this.prisma.share.update({where: {id}, data: {isZipReady: true}})
); );
return await this.prisma.share.update({ return await this.prisma.share.update({
where: { id }, where: {id},
data: { uploadLocked: true }, data: {uploadLocked: true},
}); });
} }
async getSharesByUser(userId: string) { async getSharesByUser(userId: string) {
return await this.prisma.share.findMany({ return await this.prisma.share.findMany({
where: { creator: { id: userId }, expiration: { gt: new Date() } }, where: {
creator: {id: userId},
// We want to grab any shares that are not expired or have their expiration date set to "never" (unix 0)
OR: [{expiration: {gt: new Date()}}, {expiration: {equals: moment(0).toDate()}}]
},
}); });
} }
async get(id: string) { async get(id: string) {
let share: any = await this.prisma.share.findUnique({ let share: any = await this.prisma.share.findUnique({
where: { id }, where: {id},
include: { include: {
files: true, files: true,
creator: true, creator: true,
@ -124,7 +135,7 @@ export class ShareService {
async getMetaData(id: string) { async getMetaData(id: string) {
const share = await this.prisma.share.findUnique({ const share = await this.prisma.share.findUnique({
where: { id }, where: {id},
}); });
if (!share || !share.uploadLocked) if (!share || !share.uploadLocked)
@ -135,35 +146,35 @@ export class ShareService {
async remove(shareId: string) { async remove(shareId: string) {
const share = await this.prisma.share.findUnique({ const share = await this.prisma.share.findUnique({
where: { id: shareId }, where: {id: shareId},
}); });
if (!share) throw new NotFoundException("Share not found"); if (!share) throw new NotFoundException("Share not found");
await this.fileService.deleteAllFiles(shareId); await this.fileService.deleteAllFiles(shareId);
await this.prisma.share.delete({ where: { id: shareId } }); await this.prisma.share.delete({where: {id: shareId}});
} }
async isShareCompleted(id: string) { async isShareCompleted(id: string) {
return (await this.prisma.share.findUnique({ where: { id } })).uploadLocked; return (await this.prisma.share.findUnique({where: {id}})).uploadLocked;
} }
async isShareIdAvailable(id: string) { async isShareIdAvailable(id: string) {
const share = await this.prisma.share.findUnique({ where: { id } }); const share = await this.prisma.share.findUnique({where: {id}});
return { isAvailable: !share }; return {isAvailable: !share};
} }
async increaseViewCount(share: Share) { async increaseViewCount(share: Share) {
await this.prisma.share.update({ await this.prisma.share.update({
where: { id: share.id }, where: {id: share.id},
data: { views: share.views + 1 }, data: {views: share.views + 1},
}); });
} }
async exchangeSharePasswordWithToken(shareId: string, password: string) { async exchangeSharePasswordWithToken(shareId: string, password: string) {
const sharePassword = ( const sharePassword = (
await this.prisma.shareSecurity.findFirst({ await this.prisma.shareSecurity.findFirst({
where: { share: { id: shareId } }, where: {share: {id: shareId}},
}) })
).password; ).password;
@ -171,7 +182,7 @@ export class ShareService {
throw new ForbiddenException("Wrong password"); throw new ForbiddenException("Wrong password");
const token = this.generateShareToken(shareId); const token = this.generateShareToken(shareId);
return { token }; return {token};
} }
generateShareToken(shareId: string) { generateShareToken(shareId: string) {

View File

@ -18,7 +18,7 @@ import { ShareSecurity } from "../../types/share.type";
const CreateUploadModalBody = ({ const CreateUploadModalBody = ({
uploadCallback, uploadCallback,
}: { }: {
uploadCallback: ( uploadCallback: (
id: string, id: string,
expiration: string, expiration: string,
@ -103,18 +103,16 @@ const CreateUploadModalBody = ({
label="Expiration" label="Expiration"
{...form.getInputProps("expiration")} {...form.getInputProps("expiration")}
data={[ data={[
{ {value: "never", label: "Never"},
value: "10-minutes", {value: "10-minutes", label: "10 Minutes"},
label: "10 Minutes", {value: "1-hour", label: "1 Hour"},
}, {value: "1-day", label: "1 Day"},
{ value: "1-hour", label: "1 Hour" }, {value: "1-week", label: "1 Week"},
{ value: "1-day", label: "1 Day" }, {value: "1-month", label: "1 Month"},
{ value: "1-week".toString(), label: "1 Week" },
{ value: "1-month", label: "1 Month" },
]} ]}
/> />
<Accordion> <Accordion>
<Accordion.Item value="security" sx={{ borderBottom: "none" }}> <Accordion.Item value="security" sx={{borderBottom: "none"}}>
<Accordion.Control>Security options</Accordion.Control> <Accordion.Control>Security options</Accordion.Control>
<Accordion.Panel> <Accordion.Panel>
<Stack align="stretch"> <Stack align="stretch">

View File

@ -1,4 +1,4 @@
import { Button, Group, PasswordInput, Stack, Text, Title } from "@mantine/core"; import { Button, PasswordInput, Stack, Text, Title } from "@mantine/core";
import { ModalsContextProps } from "@mantine/modals/lib/context"; import { ModalsContextProps } from "@mantine/modals/lib/context";
import { useState } from "react"; import { useState } from "react";

View File

@ -1,4 +1,4 @@
import { Button, Group, Stack, Text, Title } from "@mantine/core"; import { Button, Stack, Text, Title } from "@mantine/core";
import { useModals } from "@mantine/modals"; import { useModals } from "@mantine/modals";
import { ModalsContextProps } from "@mantine/modals/lib/context"; import { ModalsContextProps } from "@mantine/modals/lib/context";
import { useRouter } from "next/router"; import { useRouter } from "next/router";

View File

@ -4,7 +4,6 @@ import {
createStyles, createStyles,
Group, Group,
Text, Text,
useMantineTheme,
} from "@mantine/core"; } from "@mantine/core";
import { Dropzone as MantineDropzone } from "@mantine/dropzone"; import { Dropzone as MantineDropzone } from "@mantine/dropzone";
import getConfig from "next/config"; import getConfig from "next/config";
@ -46,7 +45,6 @@ const Dropzone = ({
isUploading: boolean; isUploading: boolean;
setFiles: Dispatch<SetStateAction<File[]>>; setFiles: Dispatch<SetStateAction<File[]>>;
}) => { }) => {
const theme = useMantineTheme();
const { classes } = useStyles(); const { classes } = useStyles();
const openRef = useRef<() => void>(); const openRef = useRef<() => void>();
return ( return (

View File

@ -1,7 +1,6 @@
import { import {
ActionIcon, ActionIcon,
Button, Button,
Group,
Stack, Stack,
Text, Text,
TextInput, TextInput,
@ -29,12 +28,12 @@ const showCompletedUploadModal = (
<Title order={4}>Share ready</Title> <Title order={4}>Share ready</Title>
</Stack> </Stack>
), ),
children: <Body share={share} />, children: <Body share={share}/>,
}); });
}; };
const Body = ({ share }: { share: Share }) => { const Body = ({share}: { share: Share }) => {
const clipboard = useClipboard({ timeout: 500 }); const clipboard = useClipboard({timeout: 500});
const modals = useModals(); const modals = useModals();
const router = useRouter(); const router = useRouter();
const link = `${window.location.origin}/share/${share.id}`; const link = `${window.location.origin}/share/${share.id}`;
@ -50,7 +49,7 @@ const Body = ({ share }: { share: Share }) => {
toast.success("Your link was copied to the keyboard."); toast.success("Your link was copied to the keyboard.");
}} }}
> >
<Copy /> <Copy/>
</ActionIcon> </ActionIcon>
} }
/> />
@ -60,7 +59,10 @@ const Body = ({ share }: { share: Share }) => {
color: theme.colors.gray[6], color: theme.colors.gray[6],
})} })}
> >
Your share expires at {moment(share.expiration).format("LLL")} {/* If our share.expiration is timestamp 0, show a different message */}
{moment(share.expiration).unix() === 0
? "This share will never expire."
: `This share will expire on ${moment(share.expiration).format("LLL")}`}
</Text> </Text>
<Button <Button

View File

@ -65,7 +65,9 @@ const MyShares = () => {
<td>{share.id}</td> <td>{share.id}</td>
<td>{share.views}</td> <td>{share.views}</td>
<td> <td>
{moment(share.expiration).format("MMMM DD YYYY, HH:mm")} {moment(share.expiration).unix() === 0
? "Never"
: moment(share.expiration).format("MMMM DD YYYY, HH:mm")}
</td> </td>
<td> <td>
<Group position="right"> <Group position="right">