File size: 8,109 Bytes
bbfde3f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | const Busboy = require('busboy');
const JSZip = require('jszip');
const fs = require('fs').promises;
const path = require('path');
const sessionManager = require('../lib/session-manager');
const { applyCorsHeaders, handleCorsPreflight } = require('../lib/cors-middleware');
// Helper function to send JSON with proper headers
function sendJson(res, status, data) {
res.setHeader('Content-Type', 'application/json');
res.status(status).end(JSON.stringify(data));
}
module.exports = async (req, res) => {
if (handleCorsPreflight(req, res, { allowedMethods: 'POST, OPTIONS' })) {
return;
}
applyCorsHeaders(req, res, { allowedMethods: 'POST, OPTIONS' });
if (req.method !== 'POST') {
sendJson(res, 405, { error: 'Method not allowed' });
return;
}
try {
const busboy = Busboy({ headers: req.headers });
const uploadedFiles = []; // Store multiple files
const MAX_FILES = 10; // Allow up to 10 files per batch
let fileCount = 0;
busboy.on('file', (fieldname, file, info) => {
fileCount++;
if (fileCount > MAX_FILES) {
file.resume(); // Drain the file stream
return;
}
const filename = info.filename;
const chunks = [];
file.on('data', (chunk) => {
chunks.push(chunk);
});
file.on('end', () => {
const fileData = Buffer.concat(chunks);
uploadedFiles.push({
filename: filename,
data: fileData,
size: fileData.length
});
});
});
busboy.on('finish', async () => {
if (uploadedFiles.length === 0) {
res.status(400).json({ error: 'No valid files uploaded' });
return;
}
if (fileCount > MAX_FILES) {
res.status(400).json({
error: `Too many files. Maximum ${MAX_FILES} files allowed per batch.`,
received: fileCount
});
return;
}
// Get or create session
const sessionId = req.headers['x-session-id'] || req.query.sessionId;
const session = sessionManager.getOrCreateSession(sessionId);
// Process each file and generate individual reports
const batchResults = {
batchId: Date.now(),
sessionId: session.sessionId,
timestamp: new Date().toISOString(),
totalFiles: uploadedFiles.length,
results: []
};
for (let i = 0; i < uploadedFiles.length; i++) {
const fileInfo = uploadedFiles[i];
try {
console.log(`Processing file ${i + 1}/${uploadedFiles.length}: ${fileInfo.filename}`);
// Process individual file (reuse existing logic)
const fileResult = await processSingleFile(fileInfo, session.directory);
// Add file to session
sessionManager.addFileToSession(session.sessionId, {
filename: fileInfo.filename,
reportId: fileResult.reportId,
originalPath: fileResult.originalFilePath,
reportPath: fileResult.reportPath,
processedAt: new Date().toISOString()
});
batchResults.results.push({
fileIndex: i + 1,
filename: fileInfo.filename,
fileSize: fileInfo.size,
success: true,
reportId: fileResult.reportId,
...fileResult.report
});
} catch (error) {
console.error(`Error processing ${fileInfo.filename}:`, error);
batchResults.results.push({
fileIndex: i + 1,
filename: fileInfo.filename,
fileSize: fileInfo.size,
success: false,
error: error.message
});
}
}
// Save batch summary to session directory
const batchReportPath = `${session.directory}/batch-${batchResults.batchId}-summary.json`;
await fs.writeFile(batchReportPath, JSON.stringify(batchResults, null, 2));
// Add batch to session
sessionManager.addBatchToSession(session.sessionId, {
batchId: batchResults.batchId,
timestamp: batchResults.timestamp,
totalFiles: batchResults.totalFiles,
successful: batchResults.results.filter(r => r.success).length,
failed: batchResults.results.filter(r => !r.success).length,
reportPath: batchReportPath
});
// Return batch summary with session info
res.json({
message: `Successfully processed batch of ${uploadedFiles.length} files`,
sessionId: session.sessionId,
batchId: batchResults.batchId,
summary: {
totalFiles: batchResults.totalFiles,
successful: batchResults.results.filter(r => r.success).length,
failed: batchResults.results.filter(r => !r.success).length
},
results: batchResults.results,
expiresIn: '1 hour'
});
});
req.pipe(busboy);
} catch (error) {
console.error('Batch upload error:', error);
res.status(500).json({ error: 'Internal server error during batch processing' });
}
};
// Extract single file processing logic (from existing upload-document.js)
async function processSingleFile(fileInfo, sessionDirectory) {
const { filename, data } = fileInfo;
// Validate DOCX file
if (!filename.toLowerCase().endsWith('.docx')) {
throw new Error(`Invalid file type: ${filename}. Only .docx files are supported.`);
}
let zip;
try {
zip = await JSZip.loadAsync(data);
} catch (error) {
throw new Error(`Invalid DOCX file: ${filename}. Unable to read as ZIP archive.`);
}
// Generate unique report ID for this file
const reportId = `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
// Initialize report structure
const report = {
filename: filename,
reportId: reportId,
timestamp: new Date().toISOString(),
summary: {
flagged: 0,
fixed: 0
},
details: {
hasProtection: false,
removedProtection: false,
languageDefaultFixed: null,
titleNeedsFixing: false,
textShadowsRemoved: false,
fontsNormalized: false,
fontSizesNormalized: false
}
};
// Run all analysis functions (copied from existing logic)
await analyzeDocumentStructure(zip, report);
await analyzeProtection(zip, report);
const shadowFontResults = await analyzeShadowsAndFonts(zip);
// Update report with shadow/font analysis
if (shadowFontResults.hasShadows) {
report.details.textShadowsRemoved = false; // Will be true after remediation
report.summary.flagged++;
}
if (shadowFontResults.hasSerifFonts) {
report.details.fontsNormalized = false; // Will be true after remediation
report.summary.flagged++;
}
if (shadowFontResults.hasSmallFonts) {
report.details.fontSizesNormalized = false; // Will be true after remediation
report.summary.flagged++;
}
// Save original file and report to session directory (not permanent storage)
const originalFilePath = `${sessionDirectory}/original-${reportId}.docx`;
const reportPath = `${sessionDirectory}/${reportId}-accessibility-report.json`;
await fs.writeFile(originalFilePath, data);
await fs.writeFile(reportPath, JSON.stringify(report, null, 2));
return {
reportId: reportId,
report: report,
reportPath: reportPath,
originalFilePath: originalFilePath
};
}
// Copy existing analysis functions (you'll need to import these)
async function analyzeDocumentStructure(zip, report) {
// Implementation from existing upload-document.js
// ... existing logic ...
}
async function analyzeProtection(zip, report) {
// Implementation from existing upload-document.js
// ... existing logic ...
}
async function analyzeShadowsAndFonts(zip) {
// Implementation from existing upload-document.js
// ... existing logic ...
} |