-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate_images_geojson.js
More file actions
190 lines (154 loc) · 7.44 KB
/
update_images_geojson.js
File metadata and controls
190 lines (154 loc) · 7.44 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env node
const fs = require('fs').promises;
const fsSync = require('fs');
const path = require('path');
// Configuration
const CONFIG = {
PROGRESS_FILE: './upload_progress.json',
IMAGES_GEOJSON: './images.geojson',
IMAGES_GEOJSON_BACKUP: './images.geojson.backup',
CUSTOM_IMAGES_DOMAIN: 'img.code4history.dev'
};
class GeojsonUpdater {
constructor() {
this.uploadedFiles = {};
this.updatedCount = 0;
this.skippedCount = 0;
}
async loadUploadProgress() {
try {
if (!fsSync.existsSync(CONFIG.PROGRESS_FILE)) {
throw new Error('Progress file not found. Please run upload script first.');
}
const data = await fs.readFile(CONFIG.PROGRESS_FILE, 'utf8');
const progress = JSON.parse(data);
this.uploadedFiles = progress.uploadedFiles || {};
console.log(`📁 Loaded upload progress: ${Object.keys(this.uploadedFiles).length} uploaded files`);
if (Object.keys(this.uploadedFiles).length === 0) {
throw new Error('No uploaded files found in progress. Please complete uploads first.');
}
} catch (error) {
console.error('❌ Error loading upload progress:', error.message);
process.exit(1);
}
}
async createBackup() {
try {
await fs.copyFile(CONFIG.IMAGES_GEOJSON, CONFIG.IMAGES_GEOJSON_BACKUP);
console.log(`💾 Created backup: ${CONFIG.IMAGES_GEOJSON_BACKUP}`);
} catch (error) {
console.error('❌ Failed to create backup:', error.message);
process.exit(1);
}
}
async updateGeojson() {
try {
// Load images.geojson
const geojsonData = JSON.parse(await fs.readFile(CONFIG.IMAGES_GEOJSON, 'utf8'));
console.log(`🔄 Updating ${geojsonData.features.length} features in images.geojson`);
// Update each feature
for (const feature of geojsonData.features) {
const fid = feature.properties.fid;
if (this.uploadedFiles[fid]) {
const uploadInfo = this.uploadedFiles[fid];
// Update the URLs to point to Cloudflare Images
feature.properties.path = uploadInfo.urls.public;
feature.properties.mid_thumbs = uploadInfo.urls.mid;
feature.properties.small_thumbs = uploadInfo.urls.small;
// Add metadata about the upload
feature.properties.cloudflare_id = uploadInfo.cloudflareId;
feature.properties.upload_time = uploadInfo.uploadTime;
feature.properties.original_path = uploadInfo.originalPath;
this.updatedCount++;
console.log(`✅ Updated feature ${fid}: ${uploadInfo.originalPath} -> ${uploadInfo.cloudflareId}`);
} else {
this.skippedCount++;
console.log(`⏭️ Skipped feature ${fid}: not uploaded yet`);
}
}
// Save updated geojson
await fs.writeFile(CONFIG.IMAGES_GEOJSON, JSON.stringify(geojsonData, null, 2));
console.log(`\n🎉 Updated images.geojson successfully!`);
console.log(`✅ Updated features: ${this.updatedCount}`);
console.log(`⏭️ Skipped features: ${this.skippedCount}`);
console.log(`📊 Total features: ${geojsonData.features.length}`);
} catch (error) {
console.error('❌ Error updating geojson:', error.message);
// Try to restore backup
if (fsSync.existsSync(CONFIG.IMAGES_GEOJSON_BACKUP)) {
try {
await fs.copyFile(CONFIG.IMAGES_GEOJSON_BACKUP, CONFIG.IMAGES_GEOJSON);
console.log('🔄 Restored backup due to error');
} catch (restoreError) {
console.error('❌ Failed to restore backup:', restoreError.message);
}
}
process.exit(1);
}
}
async showPreview() {
try {
const geojsonData = JSON.parse(await fs.readFile(CONFIG.IMAGES_GEOJSON, 'utf8'));
console.log('\n🔍 Preview of updates that will be made:');
console.log('=====================================');
let previewCount = 0;
const maxPreview = 5;
for (const feature of geojsonData.features) {
const fid = feature.properties.fid;
if (this.uploadedFiles[fid] && previewCount < maxPreview) {
const uploadInfo = this.uploadedFiles[fid];
console.log(`\nFeature ID: ${fid}`);
console.log(` Current path: ${feature.properties.path}`);
console.log(` New path: ${uploadInfo.urls.public}`);
console.log(` Current mid_thumbs: ${feature.properties.mid_thumbs}`);
console.log(` New mid_thumbs: ${uploadInfo.urls.mid}`);
console.log(` Current small_thumbs: ${feature.properties.small_thumbs}`);
console.log(` New small_thumbs: ${uploadInfo.urls.small}`);
previewCount++;
}
}
if (previewCount === maxPreview) {
console.log(`\n... and ${Object.keys(this.uploadedFiles).length - maxPreview} more`);
}
console.log(`\nSummary:`);
console.log(`📤 Will update: ${Object.keys(this.uploadedFiles).length} features`);
console.log(`⏭️ Will skip: ${geojsonData.features.length - Object.keys(this.uploadedFiles).length} features`);
} catch (error) {
console.error('❌ Error showing preview:', error.message);
}
}
}
// Main execution
async function main() {
const command = process.argv[2];
const updater = new GeojsonUpdater();
await updater.loadUploadProgress();
switch (command) {
case 'preview':
await updater.showPreview();
break;
case 'update':
await updater.createBackup();
await updater.updateGeojson();
break;
case 'restore':
if (fsSync.existsSync(CONFIG.IMAGES_GEOJSON_BACKUP)) {
await fs.copyFile(CONFIG.IMAGES_GEOJSON_BACKUP, CONFIG.IMAGES_GEOJSON);
console.log('🔄 Restored images.geojson from backup');
} else {
console.log('❌ No backup file found');
}
break;
default:
console.log('Usage:');
console.log(' node update_images_geojson.js preview - Show what changes will be made');
console.log(' node update_images_geojson.js update - Update images.geojson with new URLs');
console.log(' node update_images_geojson.js restore - Restore from backup');
console.log('');
console.log('Note: Run this after completing uploads with upload_to_cloudflare.js');
break;
}
}
if (require.main === module) {
main().catch(console.error);
}