update ai buttons
This commit is contained in:
@@ -3,14 +3,14 @@ import { requireLibraryAccess } from '@/lib/auth'
|
||||
import { enqueueJob } from '@/lib/ai-jobs'
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
let body: { itemKey?: string; ocrLanguages?: string }
|
||||
let body: { itemKey?: string; ocrLanguages?: string; ocrMode?: string }
|
||||
try {
|
||||
body = await request.json()
|
||||
} catch {
|
||||
return NextResponse.json({ error: 'Invalid JSON body' }, { status: 400 })
|
||||
}
|
||||
|
||||
const { itemKey, ocrLanguages } = body
|
||||
const { itemKey, ocrLanguages, ocrMode } = body
|
||||
if (!itemKey || typeof itemKey !== 'string') {
|
||||
return NextResponse.json({ error: 'itemKey is required' }, { status: 400 })
|
||||
}
|
||||
@@ -19,12 +19,15 @@ export async function POST(request: NextRequest) {
|
||||
const auth = await requireLibraryAccess(request, libraryId)
|
||||
if (auth instanceof NextResponse) return auth
|
||||
|
||||
const payload: Record<string, string> = {}
|
||||
if (ocrLanguages) payload.ocrLanguages = ocrLanguages
|
||||
if (ocrMode) payload.ocrMode = ocrMode
|
||||
const jobId = enqueueJob(
|
||||
itemKey,
|
||||
'extract',
|
||||
libraryId,
|
||||
undefined,
|
||||
ocrLanguages ? { ocrLanguages } : undefined,
|
||||
Object.keys(payload).length ? payload : undefined,
|
||||
)
|
||||
return NextResponse.json({ jobId }, { status: 202 })
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { requireLibraryAccess } from '@/lib/auth'
|
||||
import { getAiFields, updateExtractedText } from '@/lib/ai-tagger'
|
||||
import { getAiFields, updateExtractedText, updateAiDescription } from '@/lib/ai-tagger'
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const { searchParams } = request.nextUrl
|
||||
@@ -19,25 +19,37 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
|
||||
export async function PATCH(request: NextRequest) {
|
||||
let body: { itemKey?: string; extractedText?: string }
|
||||
let body: { itemKey?: string; extractedText?: string; aiDescription?: string }
|
||||
try {
|
||||
body = await request.json()
|
||||
} catch {
|
||||
return NextResponse.json({ error: 'Invalid JSON body' }, { status: 400 })
|
||||
}
|
||||
|
||||
const { itemKey, extractedText } = body
|
||||
const { itemKey, extractedText, aiDescription } = body
|
||||
if (!itemKey || typeof itemKey !== 'string') {
|
||||
return NextResponse.json({ error: 'itemKey is required' }, { status: 400 })
|
||||
}
|
||||
if (typeof extractedText !== 'string') {
|
||||
return NextResponse.json({ error: 'extractedText is required' }, { status: 400 })
|
||||
if (extractedText === undefined && aiDescription === undefined) {
|
||||
return NextResponse.json({ error: 'extractedText or aiDescription is required' }, { status: 400 })
|
||||
}
|
||||
|
||||
const libraryId = itemKey.split(':')[0]
|
||||
const auth = await requireLibraryAccess(request, libraryId)
|
||||
if (auth instanceof NextResponse) return auth
|
||||
|
||||
updateExtractedText(itemKey, extractedText)
|
||||
if (extractedText !== undefined) {
|
||||
if (typeof extractedText !== 'string') {
|
||||
return NextResponse.json({ error: 'extractedText must be a string' }, { status: 400 })
|
||||
}
|
||||
updateExtractedText(itemKey, extractedText)
|
||||
}
|
||||
if (aiDescription !== undefined) {
|
||||
if (typeof aiDescription !== 'string') {
|
||||
return NextResponse.json({ error: 'aiDescription must be a string' }, { status: 400 })
|
||||
}
|
||||
updateAiDescription(itemKey, aiDescription)
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user