| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import { BadRequestError } from "../shared/errors/index.js";
- import { extractDocumentsFromUpload, ingestDocuments, fetchUrlText } from "../services/ingestService.js";
- export const IngestController = {
- Ingerir: async function (req, res, next) {
- try {
- const result = await ingestDocuments(req.body.documents);
- res.json(result);
- } catch (err) {
- next(err);
- }
- },
- IngerirArquivo: async function (req, res, next) {
- try {
- const f = req.file;
- if (!f?.buffer) throw new BadRequestError("file_required");
- const source = typeof req.body?.source === "string" && req.body.source.trim() ? req.body.source.trim() : undefined;
- const docs = await extractDocumentsFromUpload({
- buffer: f.buffer,
- filename: f.originalname,
- mimeType: f.mimetype,
- source
- });
- const extractedChars = docs.reduce((acc, d) => acc + (d?.text?.length ?? 0), 0);
- if (!extractedChars) throw new BadRequestError("empty_extracted_text");
- const result = await ingestDocuments(docs);
- res.json({ ...result, documents: docs.length, extractedChars });
- } catch (err) {
- next(err);
- }
- },
- IngerirUrl: async function (req, res, next) {
- try {
- const body = req.body;
- const text = await fetchUrlText(body.url);
- if (!text) throw new BadRequestError("empty_extracted_text");
- const source = body.source ?? body.url;
- const result = await ingestDocuments([{ text, source, title: source }]);
- res.json({ ...result, source, extractedChars: text.length });
- } catch (err) {
- next(err);
- }
- }
- };
- export default IngestController;
|