diff --git a/scripts/mock-ai.ts b/scripts/mock-ai.ts new file mode 100644 index 0000000..e4bec6e --- /dev/null +++ b/scripts/mock-ai.ts @@ -0,0 +1,8 @@ +// Runs the scripted OpenAI-compatible mock used by the tests, so the AI features can be tried +// locally without a real provider: +// npx tsx scripts/mock-ai.ts +// Then add an "OpenAI-compatible" provider with URL http://localhost:4010/v1 and key "mock-key". +import { startMockAi } from '../tests/mockAi.ts'; + +const mock = await startMockAi(Number(process.env.MOCK_AI_PORT ?? 4010)); +console.log(`Mock AI provider listening at ${mock.url} (API key: mock-key)`); diff --git a/server/services/documents.ts b/server/services/documents.ts index 74df5d3..46ef26e 100644 --- a/server/services/documents.ts +++ b/server/services/documents.ts @@ -108,7 +108,11 @@ export function ingestUpload( throw new HttpError(413, 'too_large', `"${input.filename}" is larger than the ${Math.round(app.config.maxUploadBytes / 1048576)} MB upload limit.`); } const filename = path.basename(input.filename.replace(/\\/g, '/')).slice(0, 200) || 'upload'; - const kind = detectKind(filename, input.mime, input.bytes); + let kind = detectKind(filename, input.mime, input.bytes); + // A file whose name claims a binary format but whose bytes say otherwise (a renamed + // executable, say) is refused here instead of being stored and failing later. + if (kind === 'pdf' && !Buffer.from(input.bytes.subarray(0, 1024)).includes('%PDF')) kind = null; + if ((kind === 'docx' || kind === 'pptx') && !(input.bytes[0] === 0x50 && input.bytes[1] === 0x4b) && !(input.bytes[0] === 0xd0 && input.bytes[1] === 0xcf)) kind = null; if (!kind) { throw new HttpError(415, 'unsupported_type', `"${filename}" is not a supported file type. Upload PDF, Word (.docx), PowerPoint (.pptx), text, Markdown, or an image.`); }