fix: gallery thumbnails not loading (404 errors) #96

The gallery thumbnail endpoint was returning 404 when thumbnail_path
was null or the file didn't exist, unlike the admin endpoint which
generates thumbnails on demand using ensureThumbnail().

- Import ensureThumbnail from imageProcessor
- Use ensureThumbnail() in gallery thumbnail route to generate
  thumbnails on demand if they don't exist
- This matches the admin endpoint behavior

Fixes #96
This commit is contained in:
Paul Nothaft
2026-01-15 11:22:13 +01:00
parent 0e3b50d1b6
commit e3c3c4c951
+10 -9
View File
@@ -12,6 +12,7 @@ const { resolvePhotoFilePath } = require('../services/photoResolver');
const { getEventShareToken, resolveShareIdentifier, buildShareLinkVariants } = require('../services/shareLinkService'); const { getEventShareToken, resolveShareIdentifier, buildShareLinkVariants } = require('../services/shareLinkService');
const { handleAsync } = require('../utils/routeHelpers'); const { handleAsync } = require('../utils/routeHelpers');
const { NotFoundError } = require('../utils/errors'); const { NotFoundError } = require('../utils/errors');
const { ensureThumbnail } = require('../services/imageProcessor');
// Get storage path from environment or default // Get storage path from environment or default
const getStoragePath = () => process.env.STORAGE_PATH || path.join(__dirname, '../../storage'); const getStoragePath = () => process.env.STORAGE_PATH || path.join(__dirname, '../../storage');
@@ -797,20 +798,20 @@ router.get('/:slug/thumbnail/:photoId',
.where({ id: photoId, event_id: req.event.id }) .where({ id: photoId, event_id: req.event.id })
.first(); .first();
if (!photo || !photo.thumbnail_path) { if (!photo) {
return res.status(404).json({ error: 'Thumbnail not found' }); return res.status(404).json({ error: 'Photo not found' });
} }
const thumbPath = path.join(getStoragePath(), photo.thumbnail_path); // Ensure thumbnail exists and is valid, regenerate if needed
const thumbnailPath = await ensureThumbnail(photo);
// Check if file exists if (!thumbnailPath) {
const fs = require('fs').promises; logger.error(`Failed to generate thumbnail for photo ${photoId}`);
try { return res.status(404).json({ error: 'Thumbnail generation failed' });
await fs.access(thumbPath);
} catch (error) {
return res.status(404).json({ error: 'Thumbnail file not found' });
} }
const thumbPath = path.join(getStoragePath(), thumbnailPath);
// Log thumbnail access // Log thumbnail access
await secureImageService.logImageAccess( await secureImageService.logImageAccess(
photoId, photoId,