BREAKING: postgres + saas

This commit is contained in:
Vasily Zubarev
2025-04-03 13:07:54 +02:00
parent 54a892ddb0
commit f523b1f8ba
136 changed files with 3971 additions and 1563 deletions

35
ai/attachments.ts Normal file
View 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")
}