Files
picpeak/scripts/backfill-dimensions.js
T
Paul Nothaft 46ed1bc276 feat: add quilted layout, fix mosaic, and backfill photo dimensions (#146)
- Add migration to backfill width/height for existing photos without dimensions
- Replace justified masonry mode with quilted layout (mixed sizes based on aspect ratio)
- Rewrite mosaic layout to use proper CSS Grid with span rules
- Fix theme not being applied after gallery login
- Improve columns mode distribution using shortest-column algorithm
- Apply gallery theme regardless of authentication status
2026-01-29 23:09:12 +01:00

71 lines
1.8 KiB
JavaScript

/**
* Script to backfill photo dimensions for photos that are missing them
*/
const path = require('path');
const fs = require('fs');
const sharp = require('sharp');
// Dynamic require for knex to use the app's config
const config = require('../backend/knexfile');
const knex = require('knex')(config);
const storagePath = process.env.STORAGE_PATH || path.join(__dirname, '../storage');
async function backfillDimensions() {
console.log('Storage path:', storagePath);
const photos = await knex('photos')
.whereNull('width')
.orWhereNull('height')
.select('id', 'path', 'filename', 'media_type');
console.log(`Found ${photos.length} photos without dimensions`);
let updated = 0;
let failed = 0;
for (const photo of photos) {
if (photo.media_type === 'video') continue;
if (!photo.path) {
console.log(`Photo ${photo.id} has no path`);
failed++;
continue;
}
const fullPath = path.join(storagePath, 'events/active', photo.path);
if (!fs.existsSync(fullPath)) {
console.log(`Not found: ${fullPath}`);
failed++;
continue;
}
try {
const metadata = await sharp(fullPath).metadata();
if (metadata.width && metadata.height) {
await knex('photos')
.where('id', photo.id)
.update({ width: metadata.width, height: metadata.height });
updated++;
if (updated % 20 === 0) {
console.log(`Updated ${updated} photos...`);
}
}
} catch (err) {
console.log(`Error processing photo ${photo.id}:`, err.message);
failed++;
}
}
console.log(`\nCompleted: ${updated} updated, ${failed} failed`);
await knex.destroy();
process.exit(0);
}
backfillDimensions().catch(err => {
console.error(err);
process.exit(1);
});