mirror of
https://github.com/vas3k/TaxHacker.git
synced 2026-01-06 05:30:08 -06:00
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
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import { FILE_PREVIEWS_PATH } from "@/lib/files"
|
|
import { existsSync } from "fs"
|
|
import fs from "fs/promises"
|
|
import path from "path"
|
|
import sharp from "sharp"
|
|
|
|
const MAX_WIDTH = 1800
|
|
const MAX_HEIGHT = 1800
|
|
const QUALITY = 90
|
|
|
|
export async function resizeImage(
|
|
origFilePath: string,
|
|
maxWidth: number = MAX_WIDTH,
|
|
maxHeight: number = MAX_HEIGHT
|
|
): Promise<{ contentType: string; resizedPath: string }> {
|
|
try {
|
|
await fs.mkdir(FILE_PREVIEWS_PATH, { recursive: true })
|
|
|
|
const basename = path.basename(origFilePath, path.extname(origFilePath))
|
|
const outputPath = path.join(FILE_PREVIEWS_PATH, `${basename}.webp`)
|
|
|
|
if (existsSync(outputPath)) {
|
|
const metadata = await sharp(outputPath).metadata()
|
|
return {
|
|
contentType: `image/${metadata.format}`,
|
|
resizedPath: outputPath,
|
|
}
|
|
}
|
|
|
|
await sharp(origFilePath)
|
|
.rotate()
|
|
.resize(maxWidth, maxHeight, {
|
|
fit: "inside",
|
|
withoutEnlargement: true,
|
|
})
|
|
.webp({ quality: QUALITY })
|
|
.toFile(outputPath)
|
|
|
|
return {
|
|
contentType: "image/webp",
|
|
resizedPath: outputPath,
|
|
}
|
|
} catch (error) {
|
|
console.error("Error resizing image:", error)
|
|
return {
|
|
contentType: "image/unknown",
|
|
resizedPath: origFilePath,
|
|
}
|
|
}
|
|
}
|