feat: add POST /api/jobs and GET /api/formats endpoints

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-30 09:15:13 +02:00
co-authored by Claude Sonnet 5
parent 5cf2ae4d59
commit 280b249b05
2 changed files with 232 additions and 0 deletions
+116
View File
@@ -0,0 +1,116 @@
import path from 'node:path';
import express from 'express';
import multer from 'multer';
import { rateLimit } from 'express-rate-limit';
import { v4 as uuidv4 } from 'uuid';
import { resolve as resolveConverter, listTargetFormats } from './converters/registry.js';
import { registerImageConverters } from './converters/image.js';
import { registerImageToPdfConverter } from './converters/imageToPdf.js';
import { registerDocumentConverters } from './converters/document.js';
import { resolveInputFormat } from './mime.js';
import { deleteIfExists } from './storage.js';
import { createJob, getJobById } from './jobs/jobRepository.js';
let convertersRegistered = false;
function registerAllConverters() {
if (convertersRegistered) return;
registerImageConverters();
registerImageToPdfConverter();
registerDocumentConverters();
convertersRegistered = true;
}
export function createApp(config, pool) {
registerAllConverters();
const app = express();
const storage = multer.diskStorage({
destination: (req, file, cb) => cb(null, path.join(config.storageDir, 'uploads')),
filename: (req, file, cb) => {
const ext = path.extname(file.originalname).slice(1).toLowerCase();
cb(null, `${uuidv4()}.${ext}`);
},
});
const upload = multer({
storage,
limits: {
fileSize: config.maxFileSizeMb * 1024 * 1024,
files: 10,
},
});
const jobsRateLimiter = rateLimit({
windowMs: config.rateLimitWindowMinutes * 60 * 1000,
limit: config.rateLimitMaxJobs,
standardHeaders: 'draft-8',
legacyHeaders: false,
});
app.get('/api/formats', (req, res) => {
const source = String(req.query.source ?? '').toLowerCase();
res.json({ targets: listTargetFormats(source) });
});
app.post('/api/jobs', jobsRateLimiter, upload.array('files', 10), async (req, res) => {
if (!req.files || req.files.length === 0) {
return res.status(400).json({ error: 'No files uploaded' });
}
let targetFormats;
try {
targetFormats = JSON.parse(req.body.targetFormats ?? '[]');
} catch {
return res.status(400).json({ error: 'targetFormats must be a JSON array' });
}
if (!Array.isArray(targetFormats) || targetFormats.length !== req.files.length) {
return res.status(400).json({ error: 'targetFormats must have one entry per uploaded file' });
}
const results = [];
for (let i = 0; i < req.files.length; i += 1) {
const file = req.files[i];
const targetFormat = targetFormats[i];
const id = path.basename(file.filename, path.extname(file.filename));
const sourceFormat = path.extname(file.filename).slice(1).toLowerCase();
const { mime, valid } = await resolveInputFormat(file.path, sourceFormat);
if (!valid) {
await deleteIfExists(file.path);
results.push({ file: file.originalname, error: 'File content does not match its extension' });
continue;
}
const registryEntry = resolveConverter(sourceFormat, targetFormat);
if (!registryEntry) {
await deleteIfExists(file.path);
results.push({
file: file.originalname,
error: `Unsupported conversion: ${sourceFormat} to ${targetFormat}`,
});
continue;
}
const expiresAt = new Date(Date.now() + config.retentionHours * 3600 * 1000);
await createJob(pool, {
id,
family: registryEntry.family,
sourceFormat,
targetFormat,
originalFilename: file.originalname,
inputPath: file.filename,
inputMimeType: mime,
expiresAt,
});
results.push({ file: file.originalname, id, status: 'pending' });
}
res.status(201).json({ jobs: results });
});
return app;
}