fix(export): include excalidraw source/version metadata
This commit is contained in:
+62
-59
@@ -1373,73 +1373,76 @@ app.get("/export/json", requireAuth, asyncHandler(async (req, res, next) => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
res.setHeader("Content-Type", "application/zip");
|
res.setHeader("Content-Type", "application/zip");
|
||||||
res.setHeader(
|
res.setHeader(
|
||||||
"Content-Disposition",
|
"Content-Disposition",
|
||||||
`attachment; filename="excalidraw-drawings-${new Date().toISOString().split("T")[0]
|
`attachment; filename="excalidraw-drawings-${new Date().toISOString().split("T")[0]}.zip"`,
|
||||||
}.zip"`
|
);
|
||||||
);
|
|
||||||
|
|
||||||
const archive = archiver("zip", { zlib: { level: 9 } });
|
const archive = archiver("zip", { zlib: { level: 9 } });
|
||||||
|
|
||||||
archive.on("error", (err) => {
|
archive.on("error", (err) => {
|
||||||
console.error("Archive error:", err);
|
console.error("Archive error:", err);
|
||||||
res.status(500).json({ error: "Failed to create archive" });
|
res.status(500).json({ error: "Failed to create archive" });
|
||||||
});
|
});
|
||||||
|
|
||||||
archive.pipe(res);
|
archive.pipe(res);
|
||||||
|
|
||||||
type DrawingWithCollection = Prisma.DrawingGetPayload<{
|
type DrawingWithCollection = Prisma.DrawingGetPayload<{
|
||||||
include: { collection: true };
|
include: { collection: true };
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
type DrawingExportItem = {
|
type DrawingExportItem = {
|
||||||
name: string;
|
name: string;
|
||||||
data: {
|
data: {
|
||||||
elements: unknown[];
|
type: "excalidraw";
|
||||||
appState: Record<string, unknown>;
|
version: 2;
|
||||||
files: Record<string, unknown>;
|
source: string;
|
||||||
};
|
elements: unknown[];
|
||||||
|
appState: Record<string, unknown>;
|
||||||
|
files: Record<string, unknown>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const drawingsByCollection: Record<string, DrawingExportItem[]> = {};
|
||||||
|
const exportSource = `${req.protocol}://${req.get("host")}`;
|
||||||
|
|
||||||
|
drawings.forEach((drawing: DrawingWithCollection) => {
|
||||||
|
const collectionName = drawing.collection?.name || "Unorganized";
|
||||||
|
if (!drawingsByCollection[collectionName]) {
|
||||||
|
drawingsByCollection[collectionName] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const drawingData = {
|
||||||
|
type: "excalidraw" as const,
|
||||||
|
version: 2 as const,
|
||||||
|
source: exportSource,
|
||||||
|
elements: JSON.parse(drawing.elements) as unknown[],
|
||||||
|
appState: JSON.parse(drawing.appState) as Record<string, unknown>,
|
||||||
|
files: JSON.parse(drawing.files || "{}") as Record<string, unknown>,
|
||||||
};
|
};
|
||||||
|
|
||||||
const drawingsByCollection: Record<string, DrawingExportItem[]> = {};
|
drawingsByCollection[collectionName].push({
|
||||||
|
name: drawing.name,
|
||||||
drawings.forEach((drawing: DrawingWithCollection) => {
|
data: drawingData,
|
||||||
const collectionName = drawing.collection?.name || "Unorganized";
|
|
||||||
if (!drawingsByCollection[collectionName]) {
|
|
||||||
drawingsByCollection[collectionName] = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
const drawingData = {
|
|
||||||
elements: JSON.parse(drawing.elements) as unknown[],
|
|
||||||
appState: JSON.parse(drawing.appState) as Record<string, unknown>,
|
|
||||||
files: JSON.parse(drawing.files || "{}") as Record<string, unknown>,
|
|
||||||
};
|
|
||||||
|
|
||||||
drawingsByCollection[collectionName].push({
|
|
||||||
name: drawing.name,
|
|
||||||
data: drawingData,
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
Object.entries(drawingsByCollection).forEach(
|
Object.entries(drawingsByCollection).forEach(
|
||||||
([collectionName, collectionDrawings]) => {
|
([collectionName, collectionDrawings]) => {
|
||||||
const folderName = collectionName.replace(/[<>:"/\\|?*]/g, "_");
|
const folderName = collectionName.replace(/[<>:"/\\|?*]/g, "_");
|
||||||
collectionDrawings.forEach((drawing, index) => {
|
collectionDrawings.forEach((drawing) => {
|
||||||
const fileName = `${drawing.name.replace(
|
const fileName = `${drawing.name.replace(/[<>:"/\\|?*]/g, "_")}.excalidraw`;
|
||||||
/[<>:"/\\|?*]/g,
|
const filePath = `${folderName}/${fileName}`;
|
||||||
"_"
|
|
||||||
)}.excalidraw`;
|
|
||||||
const filePath = `${folderName}/${fileName}`;
|
|
||||||
|
|
||||||
archive.append(JSON.stringify(drawing.data, null, 2), {
|
archive.append(JSON.stringify(drawing.data, null, 2), {
|
||||||
name: filePath,
|
name: filePath,
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
);
|
},
|
||||||
|
);
|
||||||
|
|
||||||
const readmeContent = `ExcaliDash Export
|
const readmeContent = `ExcaliDash Export
|
||||||
|
|
||||||
This archive contains your ExcaliDash drawings organized by collection folders.
|
This archive contains your ExcaliDash drawings organized by collection folders.
|
||||||
|
|
||||||
@@ -1454,13 +1457,13 @@ Total Drawings: ${drawings.length}
|
|||||||
|
|
||||||
Collections:
|
Collections:
|
||||||
${Object.entries(drawingsByCollection)
|
${Object.entries(drawingsByCollection)
|
||||||
.map(([name, drawings]) => `- ${name}: ${drawings.length} drawings`)
|
.map(([name, drawings]) => `- ${name}: ${drawings.length} drawings`)
|
||||||
.join("\n")}
|
.join("\n")}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
archive.append(readmeContent, { name: "README.txt" });
|
archive.append(readmeContent, { name: "README.txt" });
|
||||||
|
|
||||||
await archive.finalize();
|
await archive.finalize();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// Database import endpoints should be admin-only or disabled in production
|
// Database import endpoints should be admin-only or disabled in production
|
||||||
|
|||||||
Reference in New Issue
Block a user