mirror of
https://github.com/marcogll/TaxHacker_s23.git
synced 2026-01-13 21:35:19 +00:00
(squash) init
feat: filters, settings, backups fix: ts compile errors feat: new dashboard, webp previews and settings feat: use webp for pdfs feat: use webp fix: analyze resets old data fix: switch to corsproxy fix: switch to free cors fix: max upload limit fix: currency conversion feat: transaction export fix: currency conversion feat: refactor settings actions feat: new loader feat: README + LICENSE doc: update readme doc: update readme doc: update readme doc: update screenshots ci: bump prisma
This commit is contained in:
66
app/files/preview/[fileId]/route.ts
Normal file
66
app/files/preview/[fileId]/route.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { getFileById } from "@/data/files"
|
||||
import { resizeImage } from "@/lib/images"
|
||||
import { pdfToImages } from "@/lib/pdf"
|
||||
import fs from "fs/promises"
|
||||
import { NextResponse } from "next/server"
|
||||
import path from "path"
|
||||
|
||||
export async function GET(request: Request, { params }: { params: Promise<{ fileId: string }> }) {
|
||||
const { fileId } = await params
|
||||
|
||||
if (!fileId) {
|
||||
return new NextResponse("No fileId provided", { status: 400 })
|
||||
}
|
||||
|
||||
const url = new URL(request.url)
|
||||
const page = parseInt(url.searchParams.get("page") || "1", 10)
|
||||
|
||||
try {
|
||||
// Find file in database
|
||||
const file = await getFileById(fileId)
|
||||
|
||||
if (!file) {
|
||||
return new NextResponse("File not found", { status: 404 })
|
||||
}
|
||||
|
||||
// Check if file exists
|
||||
try {
|
||||
await fs.access(file.path)
|
||||
} catch {
|
||||
return new NextResponse("File not found on disk", { status: 404 })
|
||||
}
|
||||
|
||||
let previewPath = file.path
|
||||
let previewType = file.mimetype
|
||||
|
||||
if (file.mimetype === "application/pdf") {
|
||||
const { contentType, pages } = await pdfToImages(file.path)
|
||||
if (page > pages.length) {
|
||||
return new NextResponse("Page not found", { status: 404 })
|
||||
}
|
||||
previewPath = pages[page - 1] || file.path
|
||||
previewType = contentType
|
||||
} else if (file.mimetype.startsWith("image/")) {
|
||||
const { contentType, resizedPath } = await resizeImage(file.path)
|
||||
previewPath = resizedPath
|
||||
previewType = contentType
|
||||
} else {
|
||||
previewPath = file.path
|
||||
previewType = file.mimetype
|
||||
}
|
||||
|
||||
// Read filex
|
||||
const fileBuffer = await fs.readFile(previewPath)
|
||||
|
||||
// Return file with proper content type
|
||||
return new NextResponse(fileBuffer, {
|
||||
headers: {
|
||||
"Content-Type": previewType,
|
||||
"Content-Disposition": `inline; filename="${path.basename(previewPath)}"`,
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
console.error("Error serving file:", error)
|
||||
return new NextResponse("Internal Server Error", { status: 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user