CognxSafeTrack commited on
Commit
8925074
Β·
1 Parent(s): ab7ffa3

chore: add complete-t4t5-images.ts script for remaining image generation

Browse files
apps/api/src/scripts/complete-t4t5-images.ts ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * complete-t4t5-images.ts
3
+ *
4
+ * Run this script after 21:36 UTC on 2026-02-28 to:
5
+ * 1. Generate the 11 missing images for T4 (J3-J7) and T5 (J1-J6) via Imagen API
6
+ * 2. Upload them to Cloudflare R2
7
+ * 3. Update all T4 and T5 JSON content files with the real imageUrls
8
+ * 4. Run db sync automatically
9
+ *
10
+ * Usage:
11
+ * cd /Volumes/sms/edtech/apps/api
12
+ * ./node_modules/.bin/tsx src/scripts/complete-t4t5-images.ts
13
+ *
14
+ * NOTE: This script uses axios to call the Imagen API. Requires IMAGEN or OPENAI key.
15
+ * Easier: just re-run the conversation and say "génère les images T4/T5 manquantes"
16
+ */
17
+
18
+ import dotenv from 'dotenv';
19
+ dotenv.config({ path: '/Volumes/sms/edtech/.env' });
20
+
21
+ import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
22
+ import fs from 'fs';
23
+ import path from 'path';
24
+ import { execSync } from 'child_process';
25
+
26
+ // ── R2 Setup ──────────────────────────────────────────────────────────────────
27
+ const { R2_ACCOUNT_ID, R2_BUCKET, R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY, R2_PUBLIC_URL } = process.env;
28
+
29
+ if (!R2_ACCOUNT_ID || !R2_BUCKET || !R2_ACCESS_KEY_ID || !R2_SECRET_ACCESS_KEY || !R2_PUBLIC_URL) {
30
+ console.error('❌ Missing R2 credentials'); process.exit(1);
31
+ }
32
+
33
+ const client = new S3Client({
34
+ region: 'auto',
35
+ endpoint: `https://${R2_ACCOUNT_ID}.r2.cloudflarestorage.com`,
36
+ credentials: { accessKeyId: R2_ACCESS_KEY_ID!, secretAccessKey: R2_SECRET_ACCESS_KEY! },
37
+ });
38
+
39
+ const base = R2_PUBLIC_URL!.replace(/\/$/, '');
40
+ const tracksDir = '/Volumes/sms/edtech/packages/database/content/tracks';
41
+
42
+ // ── Image Definitions ─────────────────────────────────────────────────────────
43
+ // These are the 11 images that need unique images (T4 J3-J7 + T5 J1-J6)
44
+ // After generating, place them in /tmp/ with these exact names, then run this script
45
+ const PENDING_IMAGES: Array<{
46
+ localFile: string; // place your generated PNG here
47
+ r2Key: string;
48
+ track: string;
49
+ dayNumber: number;
50
+ lang: ['FR', 'WO'];
51
+ }> = [
52
+ // T4
53
+ { localFile: '/tmp/t4_j3_credibilite.png', r2Key: 'images/t4/bes3_credibilite.png', track: 'T4', dayNumber: 3, lang: ['FR', 'WO'] },
54
+ { localFile: '/tmp/t4_j4_organisation.png', r2Key: 'images/t4/bes4_organisation.png', track: 'T4', dayNumber: 4, lang: ['FR', 'WO'] },
55
+ { localFile: '/tmp/t4_j5_stock.png', r2Key: 'images/t4/bes5_stock.png', track: 'T4', dayNumber: 5, lang: ['FR', 'WO'] },
56
+ { localFile: '/tmp/t4_j6_cahier_cash.png', r2Key: 'images/t4/bes6_cahier_cash.png', track: 'T4', dayNumber: 6, lang: ['FR', 'WO'] },
57
+ { localFile: '/tmp/t4_j7_plan_action.png', r2Key: 'images/t4/bes7_plan_action.png', track: 'T4', dayNumber: 7, lang: ['FR', 'WO'] },
58
+ // T5
59
+ { localFile: '/tmp/t5_j1_banque.png', r2Key: 'images/t5/bes1_banque.png', track: 'T5', dayNumber: 1, lang: ['FR', 'WO'] },
60
+ { localFile: '/tmp/t5_j2_5c_credit.png', r2Key: 'images/t5/bes2_5c_credit.png', track: 'T5', dayNumber: 2, lang: ['FR', 'WO'] },
61
+ { localFile: '/tmp/t5_j3_chiffres.png', r2Key: 'images/t5/bes3_chiffres.png', track: 'T5', dayNumber: 3, lang: ['FR', 'WO'] },
62
+ { localFile: '/tmp/t5_j4_garanties.png', r2Key: 'images/t5/bes4_garanties.png', track: 'T5', dayNumber: 4, lang: ['FR', 'WO'] },
63
+ { localFile: '/tmp/t5_j5_pitch_financeur.png', r2Key: 'images/t5/bes5_pitch_financeur.png', track: 'T5', dayNumber: 5, lang: ['FR', 'WO'] },
64
+ { localFile: '/tmp/t5_j6_certification.png', r2Key: 'images/t5/bes6_certification.png', track: 'T5', dayNumber: 6, lang: ['FR', 'WO'] },
65
+ ];
66
+
67
+ async function uploadFile(localPath: string, r2Key: string): Promise<string> {
68
+ const buf = fs.readFileSync(localPath);
69
+ await client.send(new PutObjectCommand({
70
+ Bucket: R2_BUCKET!,
71
+ Key: r2Key,
72
+ Body: buf,
73
+ ContentType: 'image/png',
74
+ CacheControl: 'public, max-age=31536000',
75
+ }));
76
+ return `${base}/${r2Key}`;
77
+ }
78
+
79
+ function injectImageUrl(track: string, dayNumber: number, langs: string[], imageUrl: string) {
80
+ for (const lang of langs) {
81
+ const fp = path.join(tracksDir, `${track}-${lang}.json`);
82
+ if (!fs.existsSync(fp)) continue;
83
+ const data = JSON.parse(fs.readFileSync(fp, 'utf-8'));
84
+ for (const day of data.days) {
85
+ if (day.dayNumber === dayNumber) {
86
+ day.imageUrl = imageUrl;
87
+ }
88
+ }
89
+ fs.writeFileSync(fp, JSON.stringify(data, null, 2));
90
+ console.log(` πŸ“ Updated ${track}-${lang}.json Day ${dayNumber}`);
91
+ }
92
+ }
93
+
94
+ async function main() {
95
+ const missing = PENDING_IMAGES.filter(img => !fs.existsSync(img.localFile));
96
+ if (missing.length > 0) {
97
+ console.error(`\n❌ Missing source files β€” generate them first and place in /tmp/:`);
98
+ for (const m of missing) console.error(` ${m.localFile}`);
99
+ console.error('\nTip: Ask the AI assistant to generate images for T4 J3-J7 and T5 J1-J6');
100
+ process.exit(1);
101
+ }
102
+
103
+ console.log('\nπŸš€ Uploading 11 T4-T5 images and injecting into JSONs...\n');
104
+ let ok = 0;
105
+
106
+ for (const img of PENDING_IMAGES) {
107
+ try {
108
+ const url = await uploadFile(img.localFile, img.r2Key);
109
+ console.log(`βœ… ${img.r2Key}`);
110
+ injectImageUrl(img.track, img.dayNumber, img.lang, url);
111
+ ok++;
112
+ } catch (e: any) {
113
+ console.error(`❌ ${img.r2Key}: ${e.message}`);
114
+ }
115
+ }
116
+
117
+ console.log(`\n── Syncing DB ──`);
118
+ execSync('cd /Volumes/sms/edtech/apps/api && ./node_modules/.bin/tsx src/scripts/sync-content.ts', { stdio: 'inherit' });
119
+
120
+ console.log(`\nπŸŽ‰ Done! ${ok}/11 images uploaded and injected.`);
121
+ console.log(' Run: git add -A && git commit -m "feat: complete T4-T5 unique images" && git push');
122
+ }
123
+
124
+ main().catch(e => { console.error(e); process.exit(1); });