Enhance image processing in generateSite.js by adding detailed logging for original and processed images, including metadata and buffer size. Improved error handling for image processing and ensured proper serving of original images when parameters are not provided. This update aids in debugging and provides better insights into image handling performance.
This commit is contained in:
parent
bbcb932147
commit
f8883c1fe8
@ -148,12 +148,26 @@ async function serveStaticSite(port = 3000) {
|
|||||||
if (url.pathname.startsWith('/images/')) {
|
if (url.pathname.startsWith('/images/')) {
|
||||||
// Serve directly from images folder
|
// Serve directly from images folder
|
||||||
filePath = url.pathname.slice(1); // Remove leading slash
|
filePath = url.pathname.slice(1); // Remove leading slash
|
||||||
|
console.log(`Request for image: ${filePath}`);
|
||||||
|
console.log(`Parameters: quality=${quality}, width=${width}`);
|
||||||
|
|
||||||
// If quality parameter is present, serve a resized version
|
// If quality parameter is present, serve a resized version
|
||||||
if (quality && width && (filePath.endsWith('.jpg') || filePath.endsWith('.jpeg') || filePath.endsWith('.png'))) {
|
if (quality && width && (filePath.endsWith('.jpg') || filePath.endsWith('.jpeg') || filePath.endsWith('.png'))) {
|
||||||
try {
|
try {
|
||||||
const Sharp = (await import('sharp')).default;
|
const Sharp = (await import('sharp')).default;
|
||||||
const image = await Sharp(filePath)
|
|
||||||
|
// Get original image info
|
||||||
|
const originalImage = await Sharp(filePath);
|
||||||
|
const originalMetadata = await originalImage.metadata();
|
||||||
|
console.log('Original image metadata:', {
|
||||||
|
width: originalMetadata.width,
|
||||||
|
height: originalMetadata.height,
|
||||||
|
size: originalMetadata.size,
|
||||||
|
format: originalMetadata.format
|
||||||
|
});
|
||||||
|
|
||||||
|
// Process the image
|
||||||
|
const processedImage = await Sharp(filePath)
|
||||||
.resize({
|
.resize({
|
||||||
width: parseInt(width),
|
width: parseInt(width),
|
||||||
height: parseInt(width),
|
height: parseInt(width),
|
||||||
@ -162,19 +176,36 @@ async function serveStaticSite(port = 3000) {
|
|||||||
.jpeg({
|
.jpeg({
|
||||||
quality: parseInt(quality),
|
quality: parseInt(quality),
|
||||||
progressive: true
|
progressive: true
|
||||||
})
|
});
|
||||||
.toBuffer();
|
|
||||||
|
// Get processed image info
|
||||||
|
const processedMetadata = await processedImage.metadata();
|
||||||
|
console.log('Processed image metadata:', {
|
||||||
|
width: processedMetadata.width,
|
||||||
|
height: processedMetadata.height,
|
||||||
|
format: processedMetadata.format
|
||||||
|
});
|
||||||
|
|
||||||
|
// Convert to buffer and get size
|
||||||
|
const buffer = await processedImage.toBuffer();
|
||||||
|
console.log('Processed image buffer size:', buffer.length);
|
||||||
|
|
||||||
res.writeHead(200, {
|
res.writeHead(200, {
|
||||||
'Content-Type': 'image/jpeg',
|
'Content-Type': 'image/jpeg',
|
||||||
'Cache-Control': 'public, max-age=31536000'
|
'Cache-Control': 'public, max-age=31536000',
|
||||||
|
'Content-Length': buffer.length
|
||||||
});
|
});
|
||||||
res.end(image);
|
res.end(buffer);
|
||||||
return;
|
return;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error processing image:', error);
|
console.error('Error processing image:', error);
|
||||||
// Fall through to serve original image
|
// Fall through to serve original image
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Log info for original image request
|
||||||
|
console.log('Serving original image');
|
||||||
|
const stats = await fs.stat(filePath);
|
||||||
|
console.log('Original file size:', stats.size);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Serve from public folder
|
// Serve from public folder
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user