feat: analyze all button, support for UTF8 filenames

This commit is contained in:
vas3k
2025-05-22 19:22:30 +02:00
parent feb56fa3ac
commit 347cf2a0e8
7 changed files with 40 additions and 10 deletions

View File

@@ -1,5 +1,6 @@
import { getCurrentUser } from "@/lib/auth" import { getCurrentUser } from "@/lib/auth"
import { fileExists, fullPathForFile } from "@/lib/files" import { fileExists, fullPathForFile } from "@/lib/files"
import { encodeFilename } from "@/lib/utils"
import { getFileById } from "@/models/files" import { getFileById } from "@/models/files"
import fs from "fs/promises" import fs from "fs/promises"
import { NextResponse } from "next/server" import { NextResponse } from "next/server"
@@ -30,12 +31,12 @@ export async function GET(request: Request, { params }: { params: Promise<{ file
// Read file // Read file
const fileBuffer = await fs.readFile(fullFilePath) const fileBuffer = await fs.readFile(fullFilePath)
// Return file with proper content type // Return file with proper content type and encoded filename
return new NextResponse(fileBuffer, { return new NextResponse(fileBuffer, {
headers: { headers: {
"Content-Type": file.mimetype, "Content-Type": file.mimetype,
"Content-Disposition": `attachment; filename="${file.filename}"`, "Content-Disposition": `attachment; filename*=${encodeFilename(file.filename)}`,
}, },
}) })
} catch (error) { } catch (error) {
console.error("Error serving file:", error) console.error("Error serving file:", error)

View File

@@ -5,6 +5,7 @@ import { getFileById } from "@/models/files"
import fs from "fs/promises" import fs from "fs/promises"
import { NextResponse } from "next/server" import { NextResponse } from "next/server"
import path from "path" import path from "path"
import { encodeFilename } from "@/lib/utils"
export async function GET(request: Request, { params }: { params: Promise<{ fileId: string }> }) { export async function GET(request: Request, { params }: { params: Promise<{ fileId: string }> }) {
const { fileId } = await params const { fileId } = await params
@@ -46,7 +47,7 @@ export async function GET(request: Request, { params }: { params: Promise<{ file
return new NextResponse(fileBuffer, { return new NextResponse(fileBuffer, {
headers: { headers: {
"Content-Type": contentType, "Content-Type": contentType,
"Content-Disposition": `inline; filename="${path.basename(previewPath)}"`, "Content-Disposition": `inline; filename*=${encodeFilename(path.basename(previewPath))}`,
}, },
}) })
} catch (error) { } catch (error) {

View File

@@ -3,6 +3,7 @@ import { UploadButton } from "@/components/files/upload-button"
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert" import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
import { Button } from "@/components/ui/button" import { Button } from "@/components/ui/button"
import { Card } from "@/components/ui/card" import { Card } from "@/components/ui/card"
import { AnalyzeAllButton } from "@/components/unsorted/analyze-all-button"
import AnalyzeForm from "@/components/unsorted/analyze-form" import AnalyzeForm from "@/components/unsorted/analyze-form"
import { getCurrentUser } from "@/lib/auth" import { getCurrentUser } from "@/lib/auth"
import config from "@/lib/config" import config from "@/lib/config"
@@ -12,7 +13,7 @@ import { getFields } from "@/models/fields"
import { getUnsortedFiles } from "@/models/files" import { getUnsortedFiles } from "@/models/files"
import { getProjects } from "@/models/projects" import { getProjects } from "@/models/projects"
import { getSettings } from "@/models/settings" import { getSettings } from "@/models/settings"
import { FileText, PartyPopper, Settings, Upload } from "lucide-react" import { FileText, PartyPopper, Settings, Upload, Brain } from "lucide-react"
import { Metadata } from "next" import { Metadata } from "next"
import Link from "next/link" import Link from "next/link"
@@ -34,6 +35,7 @@ export default async function UnsortedPage() {
<div className="flex flex-col gap-6 p-4 w-full max-w-6xl"> <div className="flex flex-col gap-6 p-4 w-full max-w-6xl">
<header className="flex items-center justify-between"> <header className="flex items-center justify-between">
<h2 className="text-3xl font-bold tracking-tight">You have {files.length} unsorted files</h2> <h2 className="text-3xl font-bold tracking-tight">You have {files.length} unsorted files</h2>
{files.length > 1 && <AnalyzeAllButton />}
</header> </header>
{config.selfHosted.isEnabled && !settings.openai_api_key && ( {config.selfHosted.isEnabled && !settings.openai_api_key && (

View File

@@ -0,0 +1,19 @@
"use client"
import { Button } from "@/components/ui/button"
import { Swords } from "lucide-react"
export function AnalyzeAllButton() {
const handleAnalyzeAll = () => {
document.querySelectorAll("button[data-analyze-button]").forEach((button) => {
;(button as HTMLButtonElement).click()
})
}
return (
<Button variant="outline" className="flex items-center gap-2" onClick={handleAnalyzeAll}>
<Swords className="h-4 w-4" />
Analyze all
</Button>
)
}

View File

@@ -141,7 +141,7 @@ export default function AnalyzeForm({
return ( return (
<> <>
<Button className="w-full mb-6 py-6 text-lg" onClick={startAnalyze} disabled={isAnalyzing}> <Button className="w-full mb-6 py-6 text-lg" onClick={startAnalyze} disabled={isAnalyzing} data-analyze-button>
{isAnalyzing ? ( {isAnalyzing ? (
<> <>
<Loader2 className="mr-1 h-4 w-4 animate-spin" /> <Loader2 className="mr-1 h-4 w-4 animate-spin" />

View File

@@ -75,3 +75,8 @@ export async function fetchAsBase64(url: string): Promise<string | null> {
return null return null
} }
} }
export function encodeFilename(filename: string): string {
const encoded = encodeURIComponent(filename)
return `UTF-8''${encoded}`
}

View File

@@ -1,7 +1,6 @@
import { prisma } from "@/lib/db" import { prisma } from "@/lib/db"
export const DEFAULT_PROMPT_ANALYSE_NEW_FILE = `You are an accountant and invoice analysis assistant. export const DEFAULT_PROMPT_ANALYSE_NEW_FILE = `You are an accountant and invoice analysis assistant. Extract following information from the given invoice:
Extract the following information from the given invoice:
{fields} {fields}
@@ -13,7 +12,10 @@ And projects are:
{projects} {projects}
If you can't find something leave it blank. Return only one object. Do not include any other text in your response!` IMPORTANT RULES:
- Do not include any other text in your response!
- If you can't find something leave it blank, NEVER make up information
- Return only one object`
export const DEFAULT_SETTINGS = [ export const DEFAULT_SETTINGS = [
{ {
@@ -284,7 +286,7 @@ export const DEFAULT_FIELDS = [
code: "name", code: "name",
name: "Name", name: "Name",
type: "string", type: "string",
llm_prompt: "human readable name, summarize what is bought in the invoice", llm_prompt: "human readable name, summarize what is bought or paid for in the invoice",
isVisibleInList: true, isVisibleInList: true,
isVisibleInAnalysis: true, isVisibleInAnalysis: true,
isRequired: true, isRequired: true,