-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshowbuilder.js
More file actions
455 lines (404 loc) · 15.8 KB
/
showbuilder.js
File metadata and controls
455 lines (404 loc) · 15.8 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// fileUpload.js
const express = require("express");
const multer = require("multer");
const path = require("path");
const fs = require("fs"); // Regular fs for createWriteStream
const fsp = require("fs").promises; // Promise-based fs for other operations
const archiver = require("archiver");
const extract = require("extract-zip");
// node backend.js --directories '[\"showbuilder\",\"C:/Users/megaf/Documents/OpenSpace/sync/url/showbuilder\",\"showbuilder/uploads\",\"C:/Users/megaf/Documents/OpenSpace/user/showbuilder/uploads\",\"showbuilder/projects\",\"C:/Users/megaf/Documents/OpenSpace/user/showbuilder/projects\"]' -p 5860
const setupShowbuilderRoutes = async (app, endpoints) => {
if (!endpoints.uploads || !endpoints.projects) {
console.log(
"Required showbuilder endpoints are missing. Skipping showbuilder setup."
);
return;
}
const uploadDir = endpoints.uploads;
const projectsDir = endpoints.projects;
// Ensure upload directory exists
await fsp.mkdir(uploadDir, { recursive: true }).catch(console.error);
// Ensure projects directory exists
await fsp.mkdir(projectsDir, { recursive: true }).catch(console.error);
// Configure multer for image uploads
const imageStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, uploadDir);
},
filename: (req, file, cb) => {
const uniqueSuffix = Date.now() + "-" + Math.round(Math.random() * 1e9);
cb(null, uniqueSuffix + path.extname(file.originalname));
},
});
// Separate multer instances for different purposes
const imageUpload = multer({
storage: imageStorage,
limits: {
fileSize: 5 * 1024 * 1024, // 5MB limit
},
fileFilter: (req, file, cb) => {
if (file.mimetype.startsWith("image/")) {
cb(null, true);
} else {
cb(new Error("Only image files are allowed!"));
}
},
});
// Configure multer for zip uploads
const zipStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, uploadDir);
},
filename: (req, file, cb) => {
const uniqueSuffix = Date.now() + "-" + Math.round(Math.random() * 1e9);
cb(null, uniqueSuffix + path.extname(file.originalname));
},
});
const zipUpload = multer({
storage: zipStorage,
limits: {
fileSize: 50 * 1024 * 1024, // 50MB limit for zip files
},
fileFilter: (req, file, cb) => {
if (
file.mimetype === "application/zip" ||
file.mimetype === "application/x-zip-compressed"
) {
cb(null, true);
} else {
cb(new Error("Only zip files are allowed!"));
}
},
});
// Upload endpoint
app.post(
"/showcomposer/api/upload",
imageUpload.single("image"),
(req, res) => {
if (!req.file) {
return res.status(400).json({ error: "No file uploaded" });
}
// console.log("req.file", req.file);
res.json({
filePath: `/uploads/${req.file.filename}`,
fileName: req.file.filename,
});
}
);
// node backend.js -d '["showcomposer","C:\\Users\\megaf\\Documents\\OpenSpace\\sync\\url\\showbuilder","showcomposer/uploads","C:\\Users\\megaf\\Documents\\OpenSpace\\user\\showbuilder\\uploads","showcomposer/projects","C:\\Users\\megaf\\Documents\\OpenSpace\\user\\showbuilder\\projects"]'
// List all images endpoint
app.get("/showcomposer/api/images", async (req, res) => {
try {
const files = await fsp.readdir(uploadDir);
const images = files
.filter((file) => /\.(jpg|jpeg|png|gif)$/i.test(file))
.map((file) => `/uploads/${file}`);
res.json({ images });
} catch (error) {
console.error("Error reading upload directory:", error);
res.status(500).json({ error: "Failed to list images" });
}
});
// // Serve uploaded files statically
// app.use("/showbuilder/uploads", express.static(uploadDir));
// Modified package endpoint to return zip file directly
app.post("/showcomposer/api/package", express.json(), async (req, res) => {
try {
const jsonData = req.body;
console.log("JSON Data: ", jsonData);
const zipFileName = `${
jsonData.settingsStore.projectName.replace(/ /g, "_") || "project"
}-${Date.now()}.zip`;
// Set up response headers for download
res.attachment(zipFileName);
const archive = archiver("zip", {
zlib: { level: 9 }, // Maximum compression
});
// Pipe archive to the response
archive.pipe(res);
// Add the JSON file to the zip
archive.append(JSON.stringify(jsonData, null, 2), { name: "data.json" });
// Function to extract image URLs from JSON
function extractImageUrls(obj) {
const urls = new Set();
JSON.stringify(obj, (key, value) => {
if (typeof value === "string" && value.includes("/uploads/")) {
urls.add(value);
}
return value;
});
return Array.from(urls);
}
// Get all image URLs from the JSON
const imageUrls = extractImageUrls(jsonData);
// Add each referenced image to the zip
for (const imageUrl of imageUrls) {
const fileName = imageUrl.split("/").pop();
const filePath = path.join(uploadDir, fileName);
try {
// Use fsp for access
await fsp.access(filePath);
archive.file(filePath, { name: `uploads/${fileName}` });
} catch (error) {
console.warn(`Warning: Referenced image not found: ${fileName}`);
}
}
// Finalize the zip file
await archive.finalize();
} catch (error) {
console.error("Error creating package:", error);
// If headers haven't been sent yet, send error response
if (!res.headersSent) {
res.status(500).json({ error: "Failed to create package" });
}
}
});
app.post(
"/showcomposer/api/projects/save",
express.json(),
async (req, res) => {
try {
const projectData = req.body;
const projectName =
projectData.settingsStore.projectName.replace(/ /g, "_") || "project";
if (!projectName || !projectData) {
return res
.status(400)
.json({ error: "Project name and data are required." });
}
const projectFilePath = path.join(projectsDir, `${projectName}.json`);
await fsp.writeFile(
projectFilePath,
JSON.stringify(projectData, null, 2)
);
res.status(201).json({ message: "Project saved successfully." });
} catch (error) {
console.error("Error saving project:", error);
res.status(500).json({ error: "Failed to save project." });
}
}
);
app.get("/showcomposer/api/projects", async (req, res) => {
try {
const files = await fsp.readdir(projectsDir);
const projectList = await Promise.all(
files
.filter((file) => file.endsWith(".json"))
.map(async (file) => {
const filePath = path.join(projectsDir, file);
const stats = await fsp.stat(filePath); // Get file statistics
return {
filePath: `./projects/${file}`, //this needs to be relative url
projectName: path.basename(file, ".json"),
lastModified: stats.mtime, // Last modified date
created: stats.birthtime, // Created date
};
})
);
res.json(projectList);
} catch (error) {
console.error("Error reading projects directory:", error);
res.status(500).json({ error: "Failed to list projects." });
}
});
app.post(
"/showcomposer/api/projects/load",
zipUpload.single("file"),
async (req, res) => {
const tempId = Date.now() + "_" + Math.round(Math.random() * 1e9);
try {
if (!req.file) {
return res.status(400).json({ error: "No zip file uploaded" });
}
// Create a temporary directory with unique ID for extraction
const tempDir = path.join(uploadDir, "temp_" + tempId);
await fsp.mkdir(tempDir, { recursive: true });
// Extract zip file
try {
await extract(req.file.path, { dir: tempDir });
console.log("Zip file extracted successfully");
} catch (extractError) {
console.error("Error extracting zip file:", extractError);
return res
.status(500)
.json({ error: "Failed to extract zip file" });
}
// Read and parse the data.json file
const dataJsonPath = path.join(tempDir, "data.json");
let projectData;
try {
const dataJson = await fsp.readFile(dataJsonPath, "utf8");
projectData = JSON.parse(dataJson);
// Function to fix image URLs
function fixImageUrls(obj) {
if (typeof obj !== 'object' || obj === null) return;
Object.keys(obj).forEach(key => {
if (typeof obj[key] === 'string') {
// Check if it's an image URL
if (obj[key].startsWith('/uploads/')) {
// Fix URL by adding showcomposer prefix
obj[key] = '/showcomposer' + obj[key];
}
} else if (typeof obj[key] === 'object') {
// Recursively check nested objects
fixImageUrls(obj[key]);
}
});
}
// Fix image URLs in project data
fixImageUrls(projectData);
console.log("Fixed image URLs in project data");
} catch (error) {
console.error("Error parsing data.json:", error);
return res
.status(500)
.json({ error: "Failed to parse data.json" });
}
// Add temp directory ID to project data for reference
projectData._tempImportId = tempId;
// Clean up the uploaded zip file
await fsp.unlink(req.file.path);
// Send back the project data with temp ID
res.json(projectData);
} catch (error) {
console.error("Error processing uploaded project:", error);
// Clean up temp directory if it exists
const tempDir = path.join(uploadDir, "temp_" + tempId);
if (tempDir) {
await fsp
.rm(tempDir, { recursive: true, force: true })
.catch(console.error);
}
// Clean up uploaded file if it exists
if (req.file && req.file.path) {
await fsp.unlink(req.file.path).catch(console.error);
}
res.status(500).json({ error: "Failed to process uploaded project" });
}
}
);
// New endpoint to confirm or reject an import
app.post(
"/showcomposer/api/projects/confirm-import",
express.json(),
async (req, res) => {
try {
const { tempId, confirm } = req.body;
if (!tempId) {
return res.status(400).json({ error: "Missing temporary import ID" });
}
const tempDir = path.join(uploadDir, "temp_" + tempId);
// Check if the temp directory exists
try {
await fsp.access(tempDir);
} catch (error) {
return res
.status(404)
.json({ error: "Import session not found or expired" });
}
if (confirm) {
// User confirmed import - process the files
// Get list of existing images
const existingImages = await fsp.readdir(uploadDir);
const imageRenames = new Map(); // Track renamed images
// Read the data.json again
const dataJsonPath = path.join(tempDir, "data.json");
let projectData = JSON.parse(
await fsp.readFile(dataJsonPath, "utf8")
);
// Check for uploads directory and handle different possible structures
const uploadedImagesDir = path.join(tempDir, "uploads");
let uploadedImages = [];
try {
// Try to read the uploads directory
uploadedImages = await fsp.readdir(uploadedImagesDir);
} catch (err) {
if (err.code === "ENOENT") {
// If "uploads" directory doesn't exist, check if images are in root of zip
const allFiles = await fsp.readdir(tempDir);
uploadedImages = allFiles.filter(
(file) =>
/\.(jpg|jpeg|png|gif)$/i.test(file) && file !== "data.json"
);
console.log("uploadedImages", uploadedImages);
uploadedImages = uploadedImages.map((image) =>
image.replace(/\\/g, "/")
);
// If we found images in root, treat tempDir as the images directory
if (uploadedImages.length > 0) {
console.log("Found images in root of zip");
// Note: we're assigning to a variable that was const
// We'll use a different approach
let imagesSourceDir = tempDir;
// Process images if we found any
for (const imageName of uploadedImages) {
let newImageName = imageName;
// If image already exists, create new unique name
if (existingImages.includes(imageName)) {
const ext = path.extname(imageName);
const baseName = path.basename(imageName, ext);
newImageName = `${baseName}_${Date.now()}${ext}`;
imageRenames.set(imageName, newImageName);
}
// Copy image to upload directory
await fsp.copyFile(
path.join(imagesSourceDir, imageName),
path.join(uploadDir, newImageName)
);
}
}
} else {
throw err; // Re-throw if it's a different error
}
}
// Process images if we found them in the uploads directory
if (uploadedImagesDir !== tempDir && uploadedImages.length > 0) {
for (const imageName of uploadedImages) {
let newImageName = imageName;
// If image already exists, create new unique name
if (existingImages.includes(imageName)) {
const ext = path.extname(imageName);
const baseName = path.basename(imageName, ext);
newImageName = `${baseName}_${Date.now()}${ext}`;
imageRenames.set(imageName, newImageName);
}
// Copy image to upload directory
await fsp.copyFile(
path.join(uploadedImagesDir, imageName),
path.join(uploadDir, newImageName)
);
}
}
// Update image references in data.json if any images were renamed
if (imageRenames.size > 0) {
const jsonString = JSON.stringify(projectData);
let updatedJsonString = jsonString;
imageRenames.forEach((newName, oldName) => {
updatedJsonString = updatedJsonString.replace(
new RegExp(oldName, "g"),
newName
);
});
projectData = JSON.parse(updatedJsonString);
}
// Remove the temp ID from the project data
delete projectData._tempImportId;
// Clean up the temporary directory
await fsp.rm(tempDir, { recursive: true, force: true });
// Send the updated project data back
res.json({ success: true, projectData });
} else {
// User rejected import - just delete the temp directory
await fsp.rm(tempDir, { recursive: true, force: true });
res.json({ success: true, message: "Import cancelled" });
}
} catch (error) {
console.error("Error confirming/rejecting import:", error);
res
.status(500)
.json({ error: "Failed to process import confirmation" });
}
}
);
};
module.exports = setupShowbuilderRoutes;