-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support upload document (#136)
* support upload file * fix * fix accept * fix * fix pdfjs * fix pdfjs * fix pdf loader
- Loading branch information
Showing
11 changed files
with
193 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
declare module 'pdfjs-dist/build/pdf.mjs' { | ||
export * from 'pdfjs-dist' | ||
} | ||
|
||
declare module 'pdfjs-dist/build/pdf.worker.mjs' { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { type NextRequest, NextResponse } from 'next/server'; | ||
import PdfLoader from '../../../../../extensions/pdf-loader/PdfLoader'; | ||
|
||
export async function POST (req: NextRequest) { | ||
const pdfLoader = new PdfLoader({}); | ||
const form = await req.formData(); | ||
const file = form.get('file') as File; | ||
return NextResponse.json(await pdfLoader.load(Buffer.from(await file.arrayBuffer()))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { DefaultDocumentImportService, DocumentImportService } from '@/core/services/importing'; | ||
import { defineHandler } from '@/lib/next/handler'; | ||
import { baseRegistry } from '@/rag-spec/base'; | ||
import { getFlow } from '@/rag-spec/createFlow'; | ||
|
||
export const POST = defineHandler({ | ||
auth: 'admin', | ||
}, async ({ request }) => { | ||
const form = await request.formData(); | ||
const file = form.get('file'); | ||
if (!file || typeof file !== 'object') { | ||
throw new Error('file needed'); | ||
} | ||
|
||
const service = new DefaultDocumentImportService({ flow: await getFlow(baseRegistry) }); | ||
|
||
const flow = await getFlow(baseRegistry); | ||
const storage = flow.getStorage(); | ||
|
||
const uri = await storage.put(`uploads/${file.name}`, Buffer.from(await file.arrayBuffer()), false); | ||
|
||
const taskIds = await DocumentImportService.createTasksByURLs([uri], 'file'); | ||
console.log('Create document import tasks: ', taskIds); | ||
|
||
return await service.runTasks(10, taskIds); | ||
}); | ||
|
||
export const dynamic = 'force-dynamic'; | ||
|
||
export const maxDuration = 150; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.