From cc0fad0b51fff1167ac5655697c02027c03dee95 Mon Sep 17 00:00:00 2001 From: Florian Trayon <26360935+FlorianLeChat@users.noreply.github.com> Date: Tue, 2 Jan 2024 18:04:39 +0100 Subject: [PATCH] Added user file dynamic fetching from server file system --- .../dashboard/components/data-table.tsx | 2 +- app/[locale]/dashboard/page.tsx | 77 +++++++++++-------- package-lock.json | 15 ++++ package.json | 1 + 4 files changed, 61 insertions(+), 34 deletions(-) diff --git a/app/[locale]/dashboard/components/data-table.tsx b/app/[locale]/dashboard/components/data-table.tsx index 748661a..353837d 100644 --- a/app/[locale]/dashboard/components/data-table.tsx +++ b/app/[locale]/dashboard/components/data-table.tsx @@ -151,7 +151,7 @@ export default function DataTable( { colSpan={columns.length} className="h-24 text-center" > - Aucun résultat. + Aucun fichier trouvé. )} diff --git a/app/[locale]/dashboard/page.tsx b/app/[locale]/dashboard/page.tsx index 1f423f3..0be40dc 100644 --- a/app/[locale]/dashboard/page.tsx +++ b/app/[locale]/dashboard/page.tsx @@ -3,11 +3,13 @@ // // Importation des dépendances. +import mime from "mime"; import { lazy } from "react"; -import { readdir } from "fs/promises"; import { redirect } from "next/navigation"; -import { join } from "path"; +import { join, parse } from "path"; import type { Metadata } from "next"; +import { mkdir, readdir } from "fs/promises"; +import { existsSync, statSync } from "fs"; import { unstable_setRequestLocale } from "next-intl/server"; // Importation des fonctions utilitaires. @@ -27,35 +29,47 @@ export const metadata: Metadata = { title: "Tableau de bord – Simple File Storage" }; -// Récupération des données depuis l'API. -function getData(): File[] +// Récupération des fichiers depuis le système de fichiers. +async function getFiles(): Promise { - return [ - { - id: "728ed52f", - name: "Document", - type: "document/pdf", - size: 11450005.2, - date: "2021-09-01", - status: "public" - }, - { - id: "728ed53f", - name: "Secret", - type: "image/gif", - size: 404150.6, - date: "2023-02-01", + // On vérifie d'abord si la session utilisateur est valide. + const session = await auth(); + + if ( !session ) + { + return []; + } + + // On créé ensuite le dossier de stockage si celui-ci n'existe pas. + const folderPath = join( process.cwd(), "public/storage" ); + + mkdir( folderPath, { recursive: true } ); + + // On vérifie après l'existance du dossier de l'utilisateur. + const userFolder = join( folderPath, session.user.id ); + + if ( !existsSync( userFolder ) ) + { + return []; + } + + // On récupère alors tous les fichiers de l'utilisateur. + const userFiles = await readdir( userFolder ); + + return userFiles.map( ( file, index ) => + { + // On retourne enfin les propriétés de chaque fichier. + const fileStats = statSync( join( userFolder, file ) ); + + return { + id: index.toString(), + name: parse( file ).name, + type: mime.getType( file ) ?? "application/octet-stream", + size: fileStats.size, + date: fileStats.birthtime.toISOString(), status: "public" - }, - { - id: "728ed54f", - name: "Application", - type: "application/exe", - size: 334156500.4, - date: "2023-04-06", - status: "private" - } - ]; + } as File; + } ); } // Affichage de la page. @@ -87,9 +101,6 @@ export default async function Page( { } } - // Déclaration des constantes. - const data = getData(); - // Affichage du rendu HTML de la page. return ( <> @@ -123,7 +134,7 @@ export default async function Page( { {/* Tableau des fichiers téléversés */} - + ); diff --git a/package-lock.json b/package-lock.json index ee5f41b..70c6f9d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,6 +21,7 @@ "bcrypt": "~5.1", "cmdk": "~0.2", "deepmerge": "~4.3", + "mime": "~4.0", "next": "~14.0", "next-auth": "5.0.0-beta.4", "next-intl": "~3.4", @@ -7474,6 +7475,20 @@ "resolved": "https://registry.npmjs.org/microseconds/-/microseconds-0.2.0.tgz", "integrity": "sha512-n7DHHMjR1avBbSpsTBj6fmMGh2AGrifVV4e+WYc3Q9lO+xnSZ3NyhcBND3vzzatt05LFhoKFRxrIyklmLlUtyA==" }, + "node_modules/mime": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/mime/-/mime-4.0.1.tgz", + "integrity": "sha512-5lZ5tyrIfliMXzFtkYyekWbtRXObT9OWa8IwQ5uxTBDHucNNwniRqo0yInflj+iYi5CBa6qxadGzGarDfuEOxA==", + "funding": [ + "https://github.com/sponsors/broofa" + ], + "bin": { + "mime": "bin/cli.js" + }, + "engines": { + "node": ">=16" + } + }, "node_modules/mime-db": { "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", diff --git a/package.json b/package.json index f202935..17ab4d9 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ }, "dependencies": { "zod": "~3.22", + "mime": "~4.0", "cmdk": "~0.2", "next": "~14.0", "sharp": "~0.33",