mirror of
https://github.com/marcogll/TaxHacker_s23.git
synced 2026-01-13 13:25:18 +00:00
BREAKING: postgres + saas
This commit is contained in:
57
app/(app)/files/actions.ts
Normal file
57
app/(app)/files/actions.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
"use server"
|
||||
|
||||
import { getCurrentUser } from "@/lib/auth"
|
||||
import { getUserUploadsDirectory, unsortedFilePath } from "@/lib/files"
|
||||
import { createFile } from "@/models/files"
|
||||
import { randomUUID } from "crypto"
|
||||
import { mkdir, writeFile } from "fs/promises"
|
||||
import { revalidatePath } from "next/cache"
|
||||
import path from "path"
|
||||
|
||||
export async function uploadFilesAction(prevState: any, formData: FormData) {
|
||||
const user = await getCurrentUser()
|
||||
const files = formData.getAll("files")
|
||||
|
||||
// Make sure upload dir exists
|
||||
const userUploadsDirectory = await getUserUploadsDirectory(user)
|
||||
|
||||
// Process each file
|
||||
const uploadedFiles = await Promise.all(
|
||||
files.map(async (file) => {
|
||||
if (!(file instanceof File)) {
|
||||
return { success: false, error: "Invalid file" }
|
||||
}
|
||||
|
||||
// Save file to filesystem
|
||||
const fileUuid = randomUUID()
|
||||
const relativeFilePath = await unsortedFilePath(fileUuid, file.name)
|
||||
const arrayBuffer = await file.arrayBuffer()
|
||||
const buffer = Buffer.from(arrayBuffer)
|
||||
|
||||
const fullFilePath = path.join(userUploadsDirectory, relativeFilePath)
|
||||
await mkdir(path.dirname(fullFilePath), { recursive: true })
|
||||
|
||||
await writeFile(fullFilePath, buffer)
|
||||
|
||||
// Create file record in database
|
||||
const fileRecord = await createFile(user.id, {
|
||||
id: fileUuid,
|
||||
filename: file.name,
|
||||
path: relativeFilePath,
|
||||
mimetype: file.type,
|
||||
metadata: {
|
||||
size: file.size,
|
||||
lastModified: file.lastModified,
|
||||
},
|
||||
})
|
||||
|
||||
return fileRecord
|
||||
})
|
||||
)
|
||||
|
||||
console.log("uploadedFiles", uploadedFiles)
|
||||
|
||||
revalidatePath("/unsorted")
|
||||
|
||||
return { success: true, error: null }
|
||||
}
|
||||
44
app/(app)/files/download/[fileId]/route.ts
Normal file
44
app/(app)/files/download/[fileId]/route.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { getCurrentUser } from "@/lib/auth"
|
||||
import { fileExists, fullPathForFile } from "@/lib/files"
|
||||
import { getFileById } from "@/models/files"
|
||||
import fs from "fs/promises"
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
export async function GET(request: Request, { params }: { params: Promise<{ fileId: string }> }) {
|
||||
const { fileId } = await params
|
||||
const user = await getCurrentUser()
|
||||
|
||||
if (!fileId) {
|
||||
return new NextResponse("No fileId provided", { status: 400 })
|
||||
}
|
||||
|
||||
try {
|
||||
// Find file in database
|
||||
const file = await getFileById(fileId, user.id)
|
||||
|
||||
if (!file || file.userId !== user.id) {
|
||||
return new NextResponse("File not found or does not belong to the user", { status: 404 })
|
||||
}
|
||||
|
||||
// Check if file exists
|
||||
const fullFilePath = await fullPathForFile(user, file)
|
||||
const isFileExists = await fileExists(fullFilePath)
|
||||
if (!isFileExists) {
|
||||
return new NextResponse(`File not found on disk: ${file.path}`, { status: 404 })
|
||||
}
|
||||
|
||||
// Read file
|
||||
const fileBuffer = await fs.readFile(fullFilePath)
|
||||
|
||||
// Return file with proper content type
|
||||
return new NextResponse(fileBuffer, {
|
||||
headers: {
|
||||
"Content-Type": file.mimetype,
|
||||
"Content-Disposition": `attachment; filename="${file.filename}"`,
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
console.error("Error serving file:", error)
|
||||
return new NextResponse("Internal Server Error", { status: 500 })
|
||||
}
|
||||
}
|
||||
10
app/(app)/files/page.tsx
Normal file
10
app/(app)/files/page.tsx
Normal file
@@ -0,0 +1,10 @@
|
||||
import { Metadata } from "next"
|
||||
import { notFound } from "next/navigation"
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: "Uploading...",
|
||||
}
|
||||
|
||||
export default function UploadStatusPage() {
|
||||
notFound()
|
||||
}
|
||||
56
app/(app)/files/preview/[fileId]/route.ts
Normal file
56
app/(app)/files/preview/[fileId]/route.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { getCurrentUser } from "@/lib/auth"
|
||||
import { fileExists, fullPathForFile } from "@/lib/files"
|
||||
import { generateFilePreviews } from "@/lib/previews/generate"
|
||||
import { getFileById } from "@/models/files"
|
||||
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
|
||||
const user = await getCurrentUser()
|
||||
|
||||
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, user.id)
|
||||
|
||||
if (!file || file.userId !== user.id) {
|
||||
return new NextResponse("File not found or does not belong to the user", { status: 404 })
|
||||
}
|
||||
|
||||
// Check if file exists on disk
|
||||
const fullFilePath = await fullPathForFile(user, file)
|
||||
const isFileExists = await fileExists(fullFilePath)
|
||||
if (!isFileExists) {
|
||||
return new NextResponse(`File not found on disk: ${file.path}`, { status: 404 })
|
||||
}
|
||||
|
||||
// Generate previews
|
||||
const { contentType, previews } = await generateFilePreviews(user, fullFilePath, file.mimetype)
|
||||
if (page > previews.length) {
|
||||
return new NextResponse("Page not found", { status: 404 })
|
||||
}
|
||||
const previewPath = previews[page - 1] || fullFilePath
|
||||
|
||||
// Read file
|
||||
const fileBuffer = await fs.readFile(previewPath)
|
||||
|
||||
// Return file with proper content type
|
||||
return new NextResponse(fileBuffer, {
|
||||
headers: {
|
||||
"Content-Type": contentType,
|
||||
"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