-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a proxy using NextJS middleware to download user files
- Loading branch information
1 parent
a554e53
commit b7c4066
Showing
5 changed files
with
91 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// Route pour la récupération des fichiers utilisateurs. | ||
// Source : https://github.com/prisma/prisma/discussions/12602 | ||
// | ||
import prisma from "@/utilities/prisma"; | ||
import { parse } from "path"; | ||
import { NextResponse } from "next/server"; | ||
|
||
export async function GET( | ||
request: Request, | ||
{ params }: { params: { id: string } } | ||
) | ||
{ | ||
// On récupère d'abord les informations du fichier | ||
// à partir de son identifiant dans la base de données. | ||
const file = await prisma.file.findUnique( { | ||
where: { | ||
fileId: params.id | ||
} | ||
} ); | ||
|
||
if ( !file ) | ||
{ | ||
// Si le fichier n'existe pas, on retourne une erreur 404. | ||
return new NextResponse( null, { status: 404 } ); | ||
} | ||
|
||
// Dans le cas contraire, on vérifie ensuite si le fichier est | ||
// public ou non. | ||
if ( file.status === "public" ) | ||
{ | ||
// Si le fichier est public, on retourne une redirection | ||
// vers le fichier. | ||
return new NextResponse( | ||
new URL( | ||
`/storage/${ file.userId }/${ file.fileId + parse( file.name ).ext }`, | ||
request.url | ||
).href | ||
); | ||
} | ||
|
||
// Sinon, on retourne enfin une erreur d'authentification. | ||
return new NextResponse( null, { status: 403 } ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters