-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Image thumbnail preview in grid view (#2)
- Loading branch information
1 parent
fe9734c
commit 72a2cfd
Showing
11 changed files
with
101 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import fs from "node:fs"; | ||
|
||
import { NextRequest, NextResponse } from "next/server"; | ||
import mime from "mime"; | ||
|
||
import { tokenStorageKey } from "@/lib/global"; | ||
import { validateToken } from "@/lib/token"; | ||
import { error } from "@/lib/packet"; | ||
import { getExtname, getFileType } from "@/lib/utils"; | ||
import { streamFile } from "@/lib/stream"; | ||
|
||
export async function GET(req: NextRequest) { | ||
const token = req.cookies.get(tokenStorageKey)?.value; | ||
|
||
if(!token) return error(401); | ||
if(!validateToken(token)) return error(403); | ||
|
||
const { searchParams } = new URL(req.url); | ||
const targetPath = searchParams.get("path") ?? "/"; | ||
|
||
try { | ||
if(!targetPath || !fs.existsSync(targetPath)) return error(404); | ||
|
||
const stat = fs.statSync(targetPath); | ||
|
||
if(!stat.isFile() || getFileType(getExtname(targetPath))?.id !== "image") return error(400); | ||
|
||
const stream = fs.createReadStream(targetPath); | ||
|
||
return new NextResponse(streamFile(stream), { | ||
status: 200, | ||
headers: { | ||
"Content-Disposition": `attachment; filename=thumbnail`, | ||
"Content-Type": mime.getType(targetPath) ?? "application/octet-stream", | ||
"Content-Length": stat.size.toString() | ||
} | ||
}); | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.log("[Server: /api/fs/thumbnail] "+ err); | ||
|
||
return error(500); | ||
} | ||
} |
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
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
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