auto-claude: subtask-1-1 - Add blog post image processing to optimize-images.js

Extended the image optimization script to process both project and blog post images:
- Added constants for posts input/output directories
- Refactored processImage to accept output directory parameter
- Updated processDirectory to handle multiple content types
- Main function now processes both projects and posts directories
- Gracefully handles missing directories with informative messages
This commit is contained in:
2026-01-25 06:24:07 +01:00
parent eec1758827
commit 7044e16911
+24 -17
View File
@@ -9,10 +9,12 @@ const __dirname = dirname(__filename);
// Mehr Größen für bessere Mobile-Unterstützung // Mehr Größen für bessere Mobile-Unterstützung
const SIZES = [200, 300, 400, 600, 800, 1200]; const SIZES = [200, 300, 400, 600, 800, 1200];
const INPUT_DIR = join(__dirname, '../source-images/projects'); const PROJECTS_INPUT_DIR = join(__dirname, '../source-images/projects');
const OUTPUT_DIR = join(__dirname, '../public/images/projects'); const PROJECTS_OUTPUT_DIR = join(__dirname, '../public/images/projects');
const POSTS_INPUT_DIR = join(__dirname, '../source-images/posts');
const POSTS_OUTPUT_DIR = join(__dirname, '../public/images/posts');
async function processImage(inputPath, projectSlug) { async function processImage(inputPath, slug, outputBaseDir) {
const filename = basename(inputPath, extname(inputPath)); const filename = basename(inputPath, extname(inputPath));
const ext = extname(inputPath).toLowerCase(); const ext = extname(inputPath).toLowerCase();
@@ -20,10 +22,10 @@ async function processImage(inputPath, projectSlug) {
return; return;
} }
const outputDir = join(OUTPUT_DIR, projectSlug); const outputDir = join(outputBaseDir, slug);
await mkdir(outputDir, { recursive: true }); await mkdir(outputDir, { recursive: true });
console.log(`Processing: ${projectSlug}/${filename}${ext}`); console.log(`Processing: ${slug}/${filename}${ext}`);
// Optimierte Originalversion (cover.jpg) // Optimierte Originalversion (cover.jpg)
const originalOutputPath = join(outputDir, `${filename}.jpg`); const originalOutputPath = join(outputDir, `${filename}.jpg`);
@@ -88,23 +90,21 @@ async function processImage(inputPath, projectSlug) {
} }
} }
async function processDirectory(dirPath) { async function processDirectory(inputDir, outputDir, typeName) {
if (!existsSync(dirPath)) { if (!existsSync(inputDir)) {
console.error(`Error: Source directory not found: ${dirPath}`); console.log(`Skipping ${typeName}: Source directory not found: ${inputDir}`);
console.log('\nPlease ensure original images are placed in:'); return;
console.log(' source-images/projects/<project-slug>/cover.jpg');
process.exit(1);
} }
const entries = await readdir(dirPath, { withFileTypes: true }); const entries = await readdir(inputDir, { withFileTypes: true });
for (const entry of entries) { for (const entry of entries) {
const fullPath = join(dirPath, entry.name); const fullPath = join(inputDir, entry.name);
if (entry.isDirectory()) { if (entry.isDirectory()) {
const coverPath = join(fullPath, 'cover.jpg'); const coverPath = join(fullPath, 'cover.jpg');
if (existsSync(coverPath)) { if (existsSync(coverPath)) {
await processImage(coverPath, entry.name); await processImage(coverPath, entry.name, outputDir);
} else { } else {
console.log(`Skipping ${entry.name}: no cover.jpg found`); console.log(`Skipping ${entry.name}: no cover.jpg found`);
} }
@@ -114,12 +114,19 @@ async function processDirectory(dirPath) {
async function main() { async function main() {
console.log('Starting image optimization...'); console.log('Starting image optimization...');
console.log(`Source: ${INPUT_DIR}`);
console.log(`Output: ${OUTPUT_DIR}`);
console.log(`Sizes: ${SIZES.join(', ')}px\n`); console.log(`Sizes: ${SIZES.join(', ')}px\n`);
try { try {
await processDirectory(INPUT_DIR); console.log('Processing projects...');
console.log(`Source: ${PROJECTS_INPUT_DIR}`);
console.log(`Output: ${PROJECTS_OUTPUT_DIR}\n`);
await processDirectory(PROJECTS_INPUT_DIR, PROJECTS_OUTPUT_DIR, 'projects');
console.log('\nProcessing posts...');
console.log(`Source: ${POSTS_INPUT_DIR}`);
console.log(`Output: ${POSTS_OUTPUT_DIR}\n`);
await processDirectory(POSTS_INPUT_DIR, POSTS_OUTPUT_DIR, 'posts');
console.log('\nImage optimization complete!'); console.log('\nImage optimization complete!');
} catch (error) { } catch (error) {
console.error('Error during optimization:', error); console.error('Error during optimization:', error);