Skip to content

Commit

Permalink
Added user quota check bypass for uploads by administrators
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianLeChat committed Jan 10, 2024
1 parent 66c8c5a commit 1379720
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
12 changes: 8 additions & 4 deletions app/[locale]/dashboard/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,16 @@ export async function uploadFiles(

// On filtre la liste des fichiers à téléverser pour ne garder que
// ceux qui ne dépassent pas le quota de l'utilisateur.
result.data.upload = result.data.upload.filter( ( file ) =>
// Note : cela ne concerne pas les administrateurs.
if ( session.user.role !== "admin" )
{
currentQuota += file.size;
result.data.upload = result.data.upload.filter( ( file ) =>
{
currentQuota += file.size;

return currentQuota <= maxQuota;
} );
return currentQuota <= maxQuota;
} );
}

// On téléverse chaque fichier dans le système de fichiers.
const data = result.data.upload.map( async ( file, index ) =>
Expand Down
7 changes: 5 additions & 2 deletions app/[locale]/dashboard/components/data-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { X } from "lucide-react";
import { useState } from "react";
import { SessionProvider } from "next-auth/react";
import { flexRender,
SortingState,
useReactTable,
Expand Down Expand Up @@ -72,7 +73,9 @@ export default function DataTable( { data }: { data: FileAttributes[] } )

// Affichage du rendu HTML du composant.
return (
<>
<SessionProvider
basePath={`${ process.env.__NEXT_ROUTER_BASEPATH }/api/auth`}
>
{/* Filtrage et tri des données */}
<div className="flex items-center gap-2 py-4">
{/* Filtrage par nom */}
Expand Down Expand Up @@ -164,6 +167,6 @@ export default function DataTable( { data }: { data: FileAttributes[] } )
<aside className="flex items-center justify-end gap-2 py-4 max-sm:flex-col sm:gap-4 lg:gap-8">
<Pagination table={table} />
</aside>
</>
</SessionProvider>
);
}
23 changes: 16 additions & 7 deletions app/[locale]/dashboard/components/file-upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { merge } from "@/utilities/tailwind";
import { useForm } from "react-hook-form";
import serverAction from "@/utilities/recaptcha";
import { useSession } from "next-auth/react";
import type { Table } from "@tanstack/react-table";
import { formatSize } from "@/utilities/react-table";
import { useFormState } from "react-dom";
Expand Down Expand Up @@ -42,6 +43,7 @@ export default function FileUpload( {
{
// Déclaration des constantes.
const files = table.options.meta?.files ?? [];
const session = useSession();
const maxQuota = Number( process.env.NEXT_PUBLIC_MAX_QUOTA ?? 0 );
const setFiles = useMemo(
() => table.options.meta?.setFiles ?? ( () =>
Expand Down Expand Up @@ -214,14 +216,21 @@ export default function FileUpload( {
/>
</FormControl>

<Progress value={percent} className="h-1" />
{session.data?.user?.role !== "admin" && (
<>
<Progress
value={percent}
className="h-1"
/>

<FormDescription className="!mt-1 text-sm text-muted-foreground">
{percent.toLocaleString()}% du quota
actuellement utilisés (
{formatSize( quota )} /{" "}
{formatSize( maxQuota )})
</FormDescription>
<FormDescription className="!mt-1 text-sm text-muted-foreground">
{percent.toLocaleString()}% du
quota actuellement utilisés (
{formatSize( quota )} /{" "}
{formatSize( maxQuota )})
</FormDescription>
</>
)}

<FormMessage />
</FormItem>
Expand Down
7 changes: 1 addition & 6 deletions schemas/file-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
//
import { z } from "zod";

// Taille maximale d'un fichier.
const MAX_FILE_SIZE = Number( process.env.NEXT_PUBLIC_MAX_QUOTA ?? "0" );

// Types de fichiers acceptés.
const ACCEPTED_FILE_TYPES =
process.env.NEXT_PUBLIC_ACCEPTED_FILE_TYPES?.split( "," ) ?? [];
Expand All @@ -20,9 +17,7 @@ const schema = z.object( {
"wrong_file_object"
)
.refine(
( files ) => files.every(
( file ) => file.size > 0 && file.size <= MAX_FILE_SIZE
),
( files ) => files.every( ( file ) => file.size > 0 ),
"wrong_file_size"
)
.refine(
Expand Down

0 comments on commit 1379720

Please sign in to comment.