feat: invoice generator

This commit is contained in:
Vasily Zubarev
2025-05-07 14:53:13 +02:00
parent 287abbb219
commit 8b5a2e8056
59 changed files with 2606 additions and 124 deletions

View File

@@ -1,27 +1,25 @@
"use server"
import { fileExists, getUserPreviewsDirectory } from "@/lib/files"
import { fileExists, getUserPreviewsDirectory, safePathJoin } from "@/lib/files"
import { User } from "@/prisma/client"
import fs from "fs/promises"
import path from "path"
import sharp from "sharp"
const MAX_WIDTH = 1800
const MAX_HEIGHT = 1800
const QUALITY = 90
import config from "../config"
export async function resizeImage(
user: User,
origFilePath: string,
maxWidth: number = MAX_WIDTH,
maxHeight: number = MAX_HEIGHT
maxWidth: number = config.upload.images.maxWidth,
maxHeight: number = config.upload.images.maxHeight,
quality: number = config.upload.images.quality
): Promise<{ contentType: string; resizedPath: string }> {
try {
const userPreviewsDirectory = await getUserPreviewsDirectory(user)
const userPreviewsDirectory = getUserPreviewsDirectory(user)
await fs.mkdir(userPreviewsDirectory, { recursive: true })
const basename = path.basename(origFilePath, path.extname(origFilePath))
const outputPath = path.join(userPreviewsDirectory, `${basename}.webp`)
const outputPath = safePathJoin(userPreviewsDirectory, `${basename}.webp`)
if (await fileExists(outputPath)) {
const metadata = await sharp(outputPath).metadata()
@@ -37,7 +35,7 @@ export async function resizeImage(
fit: "inside",
withoutEnlargement: true,
})
.webp({ quality: QUALITY })
.webp({ quality: quality })
.toFile(outputPath)
return {

View File

@@ -1,26 +1,21 @@
"use server"
import { fileExists, getUserPreviewsDirectory } from "@/lib/files"
import { fileExists, getUserPreviewsDirectory, safePathJoin } from "@/lib/files"
import { User } from "@/prisma/client"
import fs from "fs/promises"
import path from "path"
import { fromPath } from "pdf2pic"
const MAX_PAGES = 10
const DPI = 150
const QUALITY = 90
const MAX_WIDTH = 1500
const MAX_HEIGHT = 1500
import config from "../config"
export async function pdfToImages(user: User, origFilePath: string): Promise<{ contentType: string; pages: string[] }> {
const userPreviewsDirectory = await getUserPreviewsDirectory(user)
const userPreviewsDirectory = getUserPreviewsDirectory(user)
await fs.mkdir(userPreviewsDirectory, { recursive: true })
const basename = path.basename(origFilePath, path.extname(origFilePath))
// Check if converted pages already exist
const existingPages: string[] = []
for (let i = 1; i <= MAX_PAGES; i++) {
const convertedFilePath = path.join(userPreviewsDirectory, `${basename}.${i}.webp`)
for (let i = 1; i <= config.upload.pdfs.maxPages; i++) {
const convertedFilePath = safePathJoin(userPreviewsDirectory, `${basename}.${i}.webp`)
if (await fileExists(convertedFilePath)) {
existingPages.push(convertedFilePath)
} else {
@@ -34,13 +29,13 @@ export async function pdfToImages(user: User, origFilePath: string): Promise<{ c
// If not — convert the file as store in previews folder
const pdf2picOptions = {
density: DPI,
density: config.upload.pdfs.dpi,
saveFilename: basename,
savePath: userPreviewsDirectory,
format: "webp",
quality: QUALITY,
width: MAX_WIDTH,
height: MAX_HEIGHT,
quality: config.upload.pdfs.quality,
width: config.upload.pdfs.maxWidth,
height: config.upload.pdfs.maxHeight,
preserveAspectRatio: true,
}