File size: 6,118 Bytes
8925074
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9879cf
 
8925074
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
 * complete-t4t5-images.ts
 * 
 * Run this script after 21:36 UTC on 2026-02-28 to:
 *  1. Generate the 11 missing images for T4 (J3-J7) and T5 (J1-J6) via Imagen API
 *  2. Upload them to Cloudflare R2
 *  3. Update all T4 and T5 JSON content files with the real imageUrls
 *  4. Run db sync automatically
 * 
 * Usage:
 *   cd /Volumes/sms/edtech/apps/api
 *   ./node_modules/.bin/tsx src/scripts/complete-t4t5-images.ts
 * 
 * NOTE: This script uses axios to call the Imagen API. Requires IMAGEN or OPENAI key.
 * Easier: just re-run the conversation and say "génère les images T4/T5 manquantes"
 */

import dotenv from 'dotenv';
dotenv.config({ path: '/Volumes/sms/edtech/.env' });

import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';

// ── R2 Setup ──────────────────────────────────────────────────────────────────
const { R2_ACCOUNT_ID, R2_BUCKET, R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY, R2_PUBLIC_URL } = process.env;

if (!R2_ACCOUNT_ID || !R2_BUCKET || !R2_ACCESS_KEY_ID || !R2_SECRET_ACCESS_KEY || !R2_PUBLIC_URL) {
    console.error('❌ Missing R2 credentials'); process.exit(1);
}

const client = new S3Client({
    region: 'auto',
    endpoint: `https://${R2_ACCOUNT_ID}.r2.cloudflarestorage.com`,
    credentials: { accessKeyId: R2_ACCESS_KEY_ID!, secretAccessKey: R2_SECRET_ACCESS_KEY! },
});

const base = R2_PUBLIC_URL!.replace(/\/$/, '');
const tracksDir = '/Volumes/sms/edtech/packages/database/content/tracks';

// ── Image Definitions ─────────────────────────────────────────────────────────
// These are the 11 images that need unique images (T4 J3-J7 + T5 J1-J6)
// After generating, place them in /tmp/ with these exact names, then run this script
const PENDING_IMAGES: Array<{
    localFile: string;  // place your generated PNG here
    r2Key: string;
    track: string;
    dayNumber: number;
    lang: ['FR', 'WO'];
}> = [
        // T4
        { localFile: '/tmp/t4_j3_credibilite.png', r2Key: 'images/t4/bes3_credibilite.png', track: 'T4', dayNumber: 3, lang: ['FR', 'WO'] },
        { localFile: '/tmp/t4_j4_organisation.png', r2Key: 'images/t4/bes4_organisation.png', track: 'T4', dayNumber: 4, lang: ['FR', 'WO'] },
        { localFile: '/tmp/t4_j5_stock.png', r2Key: 'images/t4/bes5_stock.png', track: 'T4', dayNumber: 5, lang: ['FR', 'WO'] },
        { localFile: '/tmp/t4_j6_cahier_cash.png', r2Key: 'images/t4/bes6_cahier_cash.png', track: 'T4', dayNumber: 6, lang: ['FR', 'WO'] },
        { localFile: '/tmp/t4_j7_plan_action.png', r2Key: 'images/t4/bes7_plan_action.png', track: 'T4', dayNumber: 7, lang: ['FR', 'WO'] },
        // T5
        { localFile: '/tmp/t5_j1_banque.png', r2Key: 'images/t5/bes1_banque.png', track: 'T5', dayNumber: 1, lang: ['FR', 'WO'] },
        { localFile: '/tmp/t5_j2_5c_credit.png', r2Key: 'images/t5/bes2_5c_credit.png', track: 'T5', dayNumber: 2, lang: ['FR', 'WO'] },
        { localFile: '/tmp/t5_j3_chiffres.png', r2Key: 'images/t5/bes3_chiffres.png', track: 'T5', dayNumber: 3, lang: ['FR', 'WO'] },
        { localFile: '/tmp/t5_j4_garanties.png', r2Key: 'images/t5/bes4_garanties.png', track: 'T5', dayNumber: 4, lang: ['FR', 'WO'] },
        { localFile: '/tmp/t5_j5_pitch_financeur.png', r2Key: 'images/t5/bes5_pitch_financeur.png', track: 'T5', dayNumber: 5, lang: ['FR', 'WO'] },
        { localFile: '/tmp/t5_j6_certification.png', r2Key: 'images/t5/bes6_certification.png', track: 'T5', dayNumber: 6, lang: ['FR', 'WO'] },
    ];

async function uploadFile(localPath: string, r2Key: string): Promise<string> {
    const buf = fs.readFileSync(localPath);
    await client.send(new PutObjectCommand({
        Bucket: R2_BUCKET!,
        Key: r2Key,
        Body: buf,
        ContentType: 'image/png',
        CacheControl: 'public, max-age=31536000',
    }));
    return `${base}/${r2Key}`;
}

function injectImageUrl(track: string, dayNumber: number, langs: string[], imageUrl: string) {
    for (const lang of langs) {
        const fp = path.join(tracksDir, `${track}-${lang}.json`);
        if (!fs.existsSync(fp)) continue;
        const data = JSON.parse(fs.readFileSync(fp, 'utf-8'));
        for (const day of data.days) {
            if (day.dayNumber === dayNumber) {
                day.imageUrl = imageUrl;
            }
        }
        fs.writeFileSync(fp, JSON.stringify(data, null, 2));
        console.log(`  πŸ“ Updated ${track}-${lang}.json Day ${dayNumber}`);
    }
}

async function main() {
    const missing = PENDING_IMAGES.filter(img => !fs.existsSync(img.localFile));
    if (missing.length > 0) {
        console.error(`\n❌ Missing source files β€” generate them first and place in /tmp/:`);
        for (const m of missing) console.error(`   ${m.localFile}`);
        console.error('\nTip: Ask the AI assistant to generate images for T4 J3-J7 and T5 J1-J6');
        process.exit(1);
    }

    console.log('\nπŸš€ Uploading 11 T4-T5 images and injecting into JSONs...\n');
    let ok = 0;

    for (const img of PENDING_IMAGES) {
        try {
            const url = await uploadFile(img.localFile, img.r2Key);
            console.log(`βœ… ${img.r2Key}`);
            injectImageUrl(img.track, img.dayNumber, img.lang, url);
            ok++;
        } catch (e: unknown) {
            console.error(`❌ ${img.r2Key}: ${(e instanceof Error ? (e instanceof Error ? e.message : String(e)) : String(e))}`);
        }
    }

    console.log(`\n── Syncing DB ──`);
    execSync('cd /Volumes/sms/edtech/apps/api && ./node_modules/.bin/tsx src/scripts/sync-content.ts', { stdio: 'inherit' });

    console.log(`\nπŸŽ‰ Done! ${ok}/11 images uploaded and injected.`);
    console.log('   Run: git add -A && git commit -m "feat: complete T4-T5 unique images" && git push');
}

main().catch(e => { console.error(e); process.exit(1); });