Skip to content

Commit

Permalink
Backup Spoke project assets
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo committed May 15, 2024
1 parent 8acf613 commit 3cbaef9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hubs-backup-tool",
"productName": "Hubs Backup Tool",
"version": "1.0.4",
"version": "1.0.5",
"description": "Tool for backing up your assets from a Hubs instance",
"main": ".webpack/main",
"scripts": {
Expand Down
58 changes: 53 additions & 5 deletions src/api/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,64 @@ async function backupBlenderProjects(
}
}

async function processSpokeScene(
sceneUrl: string,
projectDir: string,
override: boolean
) {
const url = new URL(sceneUrl);
const fileName = url.pathname.split("/").pop();
if (fileName) {
await downloadFile({
url: sceneUrl,
outPath: path.join(projectDir, fileName),
override,
});
try {
const fileText = readFileSync(path.join(projectDir, fileName), {
encoding: "utf-8",
}) as never;
const parsed = JSON.parse(fileText);
if ("entities" in parsed) {
for (const entity in parsed["entities"]) {
const thisEntity = parsed["entities"][entity];
if ("components" in thisEntity) {
for (const component of thisEntity["components"]) {
if ("src" in component["props"]) {
const src = component["props"]["src"];
const assetUrl = new URL(src);
const assetFileName = assetUrl.pathname.split("/").pop();
await downloadFile({
url: component["props"]["src"],
outPath: path.join(projectDir, assetFileName),
override,
});
}
}
}
}
}
} catch (e) {
log.error(e);
}
}
}

async function backupScene(
api: ReticulumApi,
scene: SceneT,
projectDir: string,
override: boolean
) {
writeFileSync(
path.join(projectDir, `${scene.scene_id}.json`),
JSON.stringify(scene, null, 2),
{ encoding: "utf-8" }
);
const spokeFilePath = path.join(projectDir, `${scene.scene_id}.json`);
writeFileSync(spokeFilePath, JSON.stringify(scene, null, 2), {
encoding: "utf-8",
});

if (scene.scene_project_url) {
await processSpokeScene(scene.scene_project_url, projectDir, override);
}

const url = new URL(scene.model_url);
const fileName = url.pathname.split("/").pop();
if (fileName) {
Expand Down Expand Up @@ -253,6 +300,7 @@ async function backupSpokeProjects(
path.join(projectDir, fileName),
path.join(projectDir, `${project.name}.spoke`)
);
await processSpokeScene(project.project_url, projectDir, override);
}
}
if (project.thumbnail_url) {
Expand Down
1 change: 1 addition & 0 deletions src/api/reticulum-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface SceneT {
screenshot_url: string;
type: ReticulumAssetType;
url: string;
scene_project_url?: string;
}

export interface ProjectT {
Expand Down

0 comments on commit 3cbaef9

Please sign in to comment.