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,18 +2,23 @@ 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) {

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,6 +49,9 @@ 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: {
@@ -96,7 +103,11 @@ export class ShareService {
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()}}]
},
});
}

View File

@@ -103,13 +103,11 @@ const CreateUploadModalBody = ({
label="Expiration"
{...form.getInputProps("expiration")}
data={[
{
value: "10-minutes",
label: "10 Minutes",
},
{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".toString(), label: "1 Week" },
{value: "1-week", label: "1 Week"},
{value: "1-month", label: "1 Month"},
]}
/>

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,
@@ -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">