e636cf5d56
Mirror to GitHub / mirror (push) Successful in 20s
Test and Lint / backend-test (push) Successful in 1m9s
continuous-integration/drone/push Build is passing
Test and Lint / frontend-test (push) Successful in 2m9s
Version and Release / version-bump (push) Successful in 34s
Version and Release / trigger-drone (push) Successful in 3s
- Remove async functions from multer callbacks (primary corruption cause) - Implement temp directory upload approach with proper cleanup - Add comprehensive file integrity validation before processing - Fix batch upload category assignment and photo naming - Add automatic cleanup service for orphaned temp uploads - Enhance error handling with better corruption detection - Add fix-temp-photos script to repair existing temporary files - Update file watcher to ignore temp upload files Fixes issues with: - Corrupted photos showing only partial images - Photos retaining temp_ names after upload - Category assignments lost during batch uploads - Incomplete file uploads causing "Premature end of input" errors 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
111 lines
3.1 KiB
JavaScript
111 lines
3.1 KiB
JavaScript
const fs = require('fs').promises;
|
|
const path = require('path');
|
|
const sharp = require('sharp');
|
|
const logger = require('../utils/logger');
|
|
|
|
/**
|
|
* Validate uploaded file is complete and not corrupted
|
|
*/
|
|
async function validateUploadedFile(filePath) {
|
|
try {
|
|
// Check file exists and has size
|
|
const stats = await fs.stat(filePath);
|
|
if (stats.size === 0) {
|
|
throw new Error('File is empty');
|
|
}
|
|
|
|
// For image files, verify they can be read by Sharp
|
|
const ext = path.extname(filePath).toLowerCase();
|
|
const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp'];
|
|
|
|
if (imageExtensions.includes(ext)) {
|
|
// Try to read metadata - this will fail if image is corrupted
|
|
let metadata;
|
|
try {
|
|
metadata = await sharp(filePath, {
|
|
failOnError: false, // Don't fail on recoverable errors
|
|
limitInputPixels: 268402689 // ~16k x 16k max
|
|
}).metadata();
|
|
} catch (metadataError) {
|
|
// If metadata reading fails, the file is likely incomplete
|
|
throw new Error(`Invalid image file: ${metadataError.message}`);
|
|
}
|
|
|
|
if (!metadata || !metadata.width || !metadata.height) {
|
|
throw new Error('Invalid image dimensions - file may be incomplete');
|
|
}
|
|
|
|
// Check for reasonable dimensions
|
|
if (metadata.width < 10 || metadata.height < 10) {
|
|
throw new Error('Image dimensions too small');
|
|
}
|
|
|
|
// Additional check: verify we can actually decode a small portion of the image
|
|
try {
|
|
await sharp(filePath, {
|
|
failOnError: false,
|
|
limitInputPixels: 268402689
|
|
})
|
|
.resize(10, 10) // Try to resize to very small size
|
|
.toBuffer();
|
|
} catch (decodeError) {
|
|
throw new Error(`Image decode failed - file may be corrupted: ${decodeError.message}`);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
return true;
|
|
} catch (error) {
|
|
logger.error(`File validation failed for ${filePath}:`, error.message);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Middleware to validate uploaded files after multer processing
|
|
*/
|
|
async function validateUploadedFiles(req, res, next) {
|
|
if (!req.files || req.files.length === 0) {
|
|
return next();
|
|
}
|
|
|
|
const validFiles = [];
|
|
const invalidFiles = [];
|
|
|
|
// Validate each file
|
|
for (const file of req.files) {
|
|
try {
|
|
await validateUploadedFile(file.path);
|
|
validFiles.push(file);
|
|
} catch (error) {
|
|
logger.warn(`Removing invalid upload ${file.originalname}: ${error.message}`);
|
|
invalidFiles.push({
|
|
filename: file.originalname,
|
|
error: error.message
|
|
});
|
|
|
|
// Delete the invalid file
|
|
try {
|
|
await fs.unlink(file.path);
|
|
} catch (unlinkErr) {
|
|
logger.error(`Failed to delete invalid file ${file.path}:`, unlinkErr.message);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Update req.files to only include valid files
|
|
req.files = validFiles;
|
|
|
|
// Store invalid files info for response
|
|
if (invalidFiles.length > 0) {
|
|
req.invalidFiles = invalidFiles;
|
|
}
|
|
|
|
next();
|
|
}
|
|
|
|
module.exports = {
|
|
validateUploadedFile,
|
|
validateUploadedFiles
|
|
}; |