mirror of
https://github.com/marcogll/TaxHacker_s23.git
synced 2026-01-13 21:35:19 +00:00
BREAKING: postgres + saas
This commit is contained in:
35
ai/attachments.ts
Normal file
35
ai/attachments.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { fileExists, fullPathForFile } from "@/lib/files"
|
||||
import { generateFilePreviews } from "@/lib/previews/generate"
|
||||
import { File, User } from "@prisma/client"
|
||||
import fs from "fs/promises"
|
||||
|
||||
const MAX_PAGES_TO_ANALYZE = 4
|
||||
|
||||
export type AnalyzeAttachment = {
|
||||
filename: string
|
||||
contentType: string
|
||||
base64: string
|
||||
}
|
||||
|
||||
export const loadAttachmentsForAI = async (user: User, file: File): Promise<AnalyzeAttachment[]> => {
|
||||
const fullFilePath = await fullPathForFile(user, file)
|
||||
const isFileExists = await fileExists(fullFilePath)
|
||||
if (!isFileExists) {
|
||||
throw new Error("File not found on disk")
|
||||
}
|
||||
|
||||
const { contentType, previews } = await generateFilePreviews(user, fullFilePath, file.mimetype)
|
||||
|
||||
return Promise.all(
|
||||
previews.slice(0, MAX_PAGES_TO_ANALYZE).map(async (preview) => ({
|
||||
filename: file.filename,
|
||||
contentType: contentType,
|
||||
base64: await loadFileAsBase64(preview),
|
||||
}))
|
||||
)
|
||||
}
|
||||
|
||||
export const loadFileAsBase64 = async (filePath: string): Promise<string> => {
|
||||
const buffer = await fs.readFile(filePath)
|
||||
return Buffer.from(buffer).toString("base64")
|
||||
}
|
||||
Reference in New Issue
Block a user