Skip to content

Commit

Permalink
feat : allow user to download their contributions
Browse files Browse the repository at this point in the history
  • Loading branch information
Juknum committed Aug 9, 2024
1 parent 563ea69 commit 78a2764
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 14 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"mcversion",
"modid",
"Mojangstudios",
"nodebuffer",
"prisma",
"serversocket",
"stylelint",
Expand Down
40 changes: 40 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@types/unzipper": "^0.10.9",
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
"jszip": "^3.10.1",
"next": "^14.2.5",
"next-auth": "5.0.0-beta.18",
"next-themes": "^0.3.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { startTransition, useEffect, useMemo, useState } from 'react';
import { startTransition, useEffect, useMemo, useRef, useState } from 'react';

import { Button, Checkbox, Divider, Group, Stack, Text } from '@mantine/core';
import { useDisclosure } from '@mantine/hooks';
Expand Down Expand Up @@ -63,6 +63,21 @@ export function ContributionPanel({ drafts, submitted, onUpdate }: ContributionP
});
};

const linkRef = useRef<HTMLAnchorElement>(null);
const handleContributionsDownload = async () => {
const response = await fetch(`/api/download/contributions/${author.id}`, { method: 'GET' });
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const link = linkRef.current;

if (!link) return;

link.href = url;
link.download = 'contributions.zip';
link.click();
window.URL.revokeObjectURL(url);
};

const getBorderStyles = (c: ContributionWithCoAuthors | ContributionWithCoAuthorsAndPoll) => {
if (contributionToDelete.includes(c.id))
return { boxShadow: '0 0 10px var(--mantine-color-red-filled)' };
Expand Down Expand Up @@ -139,6 +154,22 @@ export function ContributionPanel({ drafts, submitted, onUpdate }: ContributionP
onMouseLeave={() => setHoveringSubmit(false)}
>
Submit {ready.length > 1 ? ready.length : ''} draft{ready.length > 1 ? 's' : ''}
<a ref={linkRef} style={{ display: 'none' }} />
</Button>

{windowWidth > BREAKPOINT_MOBILE_LARGE && (
<Divider orientation="vertical" size="sm" h={20} mt="auto" mb="auto" />
)}

<Button
variant="gradient"
gradient={gradient}
fullWidth={windowWidth <= BREAKPOINT_MOBILE_LARGE}
disabled={drafts.length === 0 && submitted.length === 0}
className={drafts.length === 0 && submitted.length === 0 ? 'button-disabled-with-bg' : undefined}
onClick={() => handleContributionsDownload()}
>
Download all contributions
</Button>

{windowWidth > BREAKPOINT_MOBILE_LARGE && (
Expand Down
43 changes: 43 additions & 0 deletions src/app/api/download/contributions/[ownerId]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

import { createReadStream } from 'fs';

import { UserRole } from '@prisma/client';
import JSZip from 'jszip';

import { canAccess } from '~/lib/auth';
import { FILE_PATH } from '~/lib/constants';
import { db } from '~/lib/db';

interface Params {
params: {
ownerId: string;
};
}

export async function GET(req: Request, { params: { ownerId } }: Params) {
await canAccess(UserRole.ADMIN, ownerId);

const contributions = await db.contribution.findMany({
where: { ownerId },
select: { file: true, filename: true, status: true, hash: true },
});

const zip = new JSZip();

for (const contribution of contributions) {
zip.file<'stream'>(
`${contribution.status}/${contribution.hash}_${contribution.filename}`,
createReadStream(`${FILE_PATH}/${contribution.file.replace('/files', '/')}`)
);
}

const zipFile = await zip.generateAsync({ type: 'nodebuffer' });

return new Response(zipFile, {
status: 200,
headers: {
'Content-Type': 'application/zip',
},
});

}
8 changes: 8 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { join } from 'path';

import { Resolution } from '@prisma/client';

import type { MantineColor } from '@mantine/core';
Expand Down Expand Up @@ -29,3 +31,9 @@ export const RESOLUTIONS_COLORS: Record<Resolution, MantineColor> = {
[Resolution.x64]: 'yellow',
};

export const FILE_DIR = process.env.NODE_ENV === 'production'
? 'https://data.faithfulmods.net'
: '/files';
export const FILE_PATH = process.env.NODE_ENV === 'production'
? '/var/www/html/data.faithfulmods.net'
: join(process.cwd(), './public/files');
9 changes: 1 addition & 8 deletions src/server/actions/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { join } from 'path';
import unzipper from 'unzipper';

import { MODS_LOADERS } from '~/lib/constants';
import { FILE_DIR, FILE_PATH } from '~/lib/constants';
import { db } from '~/lib/db';
import { calculateHash } from '~/lib/hash';
import { socket } from '~/lib/serversocket';
Expand All @@ -21,14 +22,6 @@ import { createTexture, findTexture } from '../data/texture';
import type { ModVersion } from '@prisma/client';
import type { MCModInfo, MCModInfoData, SocketModUpload } from '~/types';

const FILE_DIR = process.env.NODE_ENV === 'production'
? 'https://data.faithfulmods.net'
: '/files';

const FILE_PATH = process.env.NODE_ENV === 'production'
? '/var/www/html/data.faithfulmods.net'
: join(process.cwd(), './public/files');

/**
* Uploads a file to the server
* @param file File to upload
Expand Down
6 changes: 1 addition & 5 deletions src/server/data/contributions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
'use server';
import 'server-only';

import {
Status,

UserRole,
} from '@prisma/client';
import { Status, UserRole } from '@prisma/client';

import { canAccess } from '~/lib/auth';
import { db } from '~/lib/db';
Expand Down

0 comments on commit 78a2764

Please sign in to comment.