feature: Added "never" expiration date

This commit is contained in:
Steve Tautonico
2022-10-12 16:59:04 -04:00
parent 69ee88aebc
commit 56349c6f4c
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 { FileService } from "src/file/file.service";
import { PrismaService } from "src/prisma/prisma.service";
import * as moment from "moment";
@Injectable()
export class JobsService {
constructor(
private prisma: PrismaService,
private fileService: FileService
) {}
) {
}
@Cron("0 * * * *")
async deleteExpiredShares() {
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) {
await this.prisma.share.delete({
where: { id: expiredShare.id },
where: {id: expiredShare.id},
});
await this.fileService.deleteAllFiles(expiredShare.id);
@@ -30,7 +35,7 @@ export class JobsService {
@Cron("0 * * * *")
async deleteExpiredRefreshTokens() {
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`);
}

View File

@@ -33,7 +33,7 @@ export class ShareSecurityGuard implements CanActivate {
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");
if (!share.security) return true;

View File

@@ -22,7 +22,8 @@ export class ShareService {
private fileService: FileService,
private config: ConfigService,
private jwtService: JwtService
) {}
) {
}
async create(share: CreateShareDTO, user: User) {
if (!(await this.isShareIdAvailable(share.id)).isAvailable)
@@ -35,7 +36,10 @@ export class ShareService {
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(
share.expiration.split("-")[0],
share.expiration.split("-")[1] as moment.unitOfTime.DurationConstructor
@@ -45,13 +49,16 @@ export class ShareService {
// Throw error if expiration date is now
if (expirationDate.setMilliseconds(0) == new Date().setMilliseconds(0))
throw new BadRequestException("Invalid expiration date");
} else {
expirationDate = moment(0).toDate();
}
return await this.prisma.share.create({
data: {
...share,
expiration: expirationDate,
creator: { connect: { id: user.id } },
security: { create: share.security },
creator: {connect: {id: user.id}},
security: {create: share.security},
},
});
}
@@ -59,9 +66,9 @@ export class ShareService {
async createZip(shareId: string) {
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", {
zlib: { level: 9 },
zlib: {level: 9},
});
const writeStream = fs.createWriteStream(`${path}/archive.zip`);
@@ -77,7 +84,7 @@ export class ShareService {
async complete(id: string) {
const moreThanOneFileInShare =
(await this.prisma.file.findMany({ where: { shareId: id } })).length != 0;
(await this.prisma.file.findMany({where: {shareId: id}})).length != 0;
if (!moreThanOneFileInShare)
throw new BadRequestException(
@@ -85,24 +92,28 @@ export class ShareService {
);
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({
where: { id },
data: { uploadLocked: true },
where: {id},
data: {uploadLocked: true},
});
}
async getSharesByUser(userId: string) {
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) {
let share: any = await this.prisma.share.findUnique({
where: { id },
where: {id},
include: {
files: true,
creator: true,
@@ -124,7 +135,7 @@ export class ShareService {
async getMetaData(id: string) {
const share = await this.prisma.share.findUnique({
where: { id },
where: {id},
});
if (!share || !share.uploadLocked)
@@ -135,35 +146,35 @@ export class ShareService {
async remove(shareId: string) {
const share = await this.prisma.share.findUnique({
where: { id: shareId },
where: {id: shareId},
});
if (!share) throw new NotFoundException("Share not found");
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) {
return (await this.prisma.share.findUnique({ where: { id } })).uploadLocked;
return (await this.prisma.share.findUnique({where: {id}})).uploadLocked;
}
async isShareIdAvailable(id: string) {
const share = await this.prisma.share.findUnique({ where: { id } });
return { isAvailable: !share };
const share = await this.prisma.share.findUnique({where: {id}});
return {isAvailable: !share};
}
async increaseViewCount(share: Share) {
await this.prisma.share.update({
where: { id: share.id },
data: { views: share.views + 1 },
where: {id: share.id},
data: {views: share.views + 1},
});
}
async exchangeSharePasswordWithToken(shareId: string, password: string) {
const sharePassword = (
await this.prisma.shareSecurity.findFirst({
where: { share: { id: shareId } },
where: {share: {id: shareId}},
})
).password;
@@ -171,7 +182,7 @@ export class ShareService {
throw new ForbiddenException("Wrong password");
const token = this.generateShareToken(shareId);
return { token };
return {token};
}
generateShareToken(shareId: string) {

View File

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

View File

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

View File

@@ -1,7 +1,6 @@
import {
ActionIcon,
Button,
Group,
Stack,
Text,
TextInput,
@@ -29,12 +28,12 @@ const showCompletedUploadModal = (
<Title order={4}>Share ready</Title>
</Stack>
),
children: <Body share={share} />,
children: <Body share={share}/>,
});
};
const Body = ({ share }: { share: Share }) => {
const clipboard = useClipboard({ timeout: 500 });
const Body = ({share}: { share: Share }) => {
const clipboard = useClipboard({timeout: 500});
const modals = useModals();
const router = useRouter();
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.");
}}
>
<Copy />
<Copy/>
</ActionIcon>
}
/>
@@ -60,7 +59,10 @@ const Body = ({ share }: { share: Share }) => {
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>
<Button

View File

@@ -65,7 +65,9 @@ const MyShares = () => {
<td>{share.id}</td>
<td>{share.views}</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>
<Group position="right">