Files
TaxHacker_s23/lib/files.ts
Vasily Zubarev 0b98a2c307 (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
2025-03-16 21:29:20 +01:00

37 lines
1.5 KiB
TypeScript

import { Transaction } from "@prisma/client"
import { randomUUID } from "crypto"
import path from "path"
export const FILE_ACCEPTED_MIMETYPES = "image/*,.pdf,.doc,.docx,.xls,.xlsx"
export const FILE_UPLOAD_PATH = path.resolve(process.env.UPLOAD_PATH || "./uploads")
export const FILE_UNSORTED_UPLOAD_PATH = path.join(FILE_UPLOAD_PATH, "unsorted")
export const FILE_PREVIEWS_PATH = path.join(FILE_UPLOAD_PATH, "previews")
export async function getUnsortedFileUploadPath(filename: string) {
const fileUuid = randomUUID()
const fileExtension = path.extname(filename)
const storedFileName = `${fileUuid}${fileExtension}`
const filePath = path.join(FILE_UNSORTED_UPLOAD_PATH, storedFileName)
return { fileUuid, filePath }
}
export async function getTransactionFileUploadPath(filename: string, transaction: Transaction) {
const fileUuid = randomUUID()
const fileExtension = path.extname(filename)
const storedFileName = `${fileUuid}${fileExtension}`
const formattedPath = formatFilePath(storedFileName, transaction.issuedAt || new Date())
const filePath = path.join(FILE_UPLOAD_PATH, formattedPath)
return { fileUuid, filePath }
}
function formatFilePath(filename: string, date: Date, format = "{YYYY}/{MM}/{name}{ext}") {
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, "0")
const ext = path.extname(filename)
const name = path.basename(filename, ext)
return format.replace("{YYYY}", String(year)).replace("{MM}", month).replace("{name}", name).replace("{ext}", ext)
}