mirror of
https://github.com/marcogll/TaxHacker_s23.git
synced 2026-01-13 13:25:18 +00:00
feat: invoice generator
This commit is contained in:
@@ -2,7 +2,13 @@
|
||||
|
||||
import { ActionState } from "@/lib/actions"
|
||||
import { getCurrentUser, isSubscriptionExpired } from "@/lib/auth"
|
||||
import { getDirectorySize, getUserUploadsDirectory, isEnoughStorageToUploadFile, unsortedFilePath } from "@/lib/files"
|
||||
import {
|
||||
getDirectorySize,
|
||||
getUserUploadsDirectory,
|
||||
isEnoughStorageToUploadFile,
|
||||
safePathJoin,
|
||||
unsortedFilePath,
|
||||
} from "@/lib/files"
|
||||
import { createFile } from "@/models/files"
|
||||
import { updateUser } from "@/models/users"
|
||||
import { randomUUID } from "crypto"
|
||||
@@ -15,7 +21,7 @@ export async function uploadFilesAction(formData: FormData): Promise<ActionState
|
||||
const files = formData.getAll("files") as File[]
|
||||
|
||||
// Make sure upload dir exists
|
||||
const userUploadsDirectory = await getUserUploadsDirectory(user)
|
||||
const userUploadsDirectory = getUserUploadsDirectory(user)
|
||||
|
||||
// Check limits
|
||||
const totalFileSize = files.reduce((acc, file) => acc + file.size, 0)
|
||||
@@ -39,11 +45,11 @@ export async function uploadFilesAction(formData: FormData): Promise<ActionState
|
||||
|
||||
// Save file to filesystem
|
||||
const fileUuid = randomUUID()
|
||||
const relativeFilePath = await unsortedFilePath(fileUuid, file.name)
|
||||
const relativeFilePath = unsortedFilePath(fileUuid, file.name)
|
||||
const arrayBuffer = await file.arrayBuffer()
|
||||
const buffer = Buffer.from(arrayBuffer)
|
||||
|
||||
const fullFilePath = path.join(userUploadsDirectory, relativeFilePath)
|
||||
const fullFilePath = safePathJoin(userUploadsDirectory, relativeFilePath)
|
||||
await mkdir(path.dirname(fullFilePath), { recursive: true })
|
||||
|
||||
await writeFile(fullFilePath, buffer)
|
||||
@@ -65,7 +71,7 @@ export async function uploadFilesAction(formData: FormData): Promise<ActionState
|
||||
)
|
||||
|
||||
// Update user storage used
|
||||
const storageUsed = await getDirectorySize(await getUserUploadsDirectory(user))
|
||||
const storageUsed = await getDirectorySize(getUserUploadsDirectory(user))
|
||||
await updateUser(user.id, { storageUsed })
|
||||
|
||||
console.log("uploadedFiles", uploadedFiles)
|
||||
|
||||
@@ -21,7 +21,7 @@ export async function GET(request: Request, { params }: { params: Promise<{ file
|
||||
}
|
||||
|
||||
// Check if file exists
|
||||
const fullFilePath = await fullPathForFile(user, file)
|
||||
const fullFilePath = fullPathForFile(user, file)
|
||||
const isFileExists = await fileExists(fullFilePath)
|
||||
if (!isFileExists) {
|
||||
return new NextResponse(`File not found on disk: ${file.path}`, { status: 404 })
|
||||
|
||||
@@ -26,7 +26,7 @@ export async function GET(request: Request, { params }: { params: Promise<{ file
|
||||
}
|
||||
|
||||
// Check if file exists on disk
|
||||
const fullFilePath = await fullPathForFile(user, file)
|
||||
const fullFilePath = fullPathForFile(user, file)
|
||||
const isFileExists = await fileExists(fullFilePath)
|
||||
if (!isFileExists) {
|
||||
return new NextResponse(`File not found on disk: ${file.path}`, { status: 404 })
|
||||
|
||||
35
app/(app)/files/static/[filename]/route.ts
Normal file
35
app/(app)/files/static/[filename]/route.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { getCurrentUser } from "@/lib/auth"
|
||||
import { fileExists, getStaticDirectory, safePathJoin } from "@/lib/files"
|
||||
import fs from "fs/promises"
|
||||
import lookup from "mime-types"
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
export async function GET(request: Request, { params }: { params: Promise<{ filename: string }> }) {
|
||||
const { filename } = await params
|
||||
const user = await getCurrentUser()
|
||||
|
||||
if (!filename) {
|
||||
return new NextResponse("No filename provided", { status: 400 })
|
||||
}
|
||||
|
||||
const staticFilesDirectory = getStaticDirectory(user)
|
||||
|
||||
try {
|
||||
const fullFilePath = safePathJoin(staticFilesDirectory, filename)
|
||||
const isFileExists = await fileExists(fullFilePath)
|
||||
if (!isFileExists) {
|
||||
return new NextResponse(`File not found for user: ${filename}`, { status: 404 })
|
||||
}
|
||||
|
||||
const fileBuffer = await fs.readFile(fullFilePath)
|
||||
|
||||
return new NextResponse(fileBuffer, {
|
||||
headers: {
|
||||
"Content-Type": lookup.lookup(filename) || "application/octet-stream",
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
console.error("Error serving file:", error)
|
||||
return new NextResponse("Internal Server Error", { status: 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user