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

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

View File

@ -103,13 +103,11 @@ 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-hour", label: "1 Hour"},
{value: "1-day", label: "1 Day"}, {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"}, {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 { 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,
@ -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">