Skip to content

Commit

Permalink
Merge pull request #15 from MozillaReality/spoke-remixed
Browse files Browse the repository at this point in the history
Add support for backing up parent scenes
  • Loading branch information
keianhzo authored May 6, 2024
2 parents fb8d6e0 + ceceee6 commit 8acf613
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 33 deletions.
81 changes: 49 additions & 32 deletions src/api/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import {
copyFileSync,
} from "fs";
import fetch from "node-fetch";
import { AssetT, AvatarListingT, HubT, ReticulumApi } from "./reticulum-api";
import {
AssetT,
AvatarListingT,
HubT,
ReticulumApi,
SceneT,
} from "./reticulum-api";
import path from "path";
import {
IAuthCredentials,
Expand Down Expand Up @@ -162,6 +168,45 @@ async function backupBlenderProjects(
}
}

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 url = new URL(scene.model_url);
const fileName = url.pathname.split("/").pop();
if (fileName) {
await downloadFile({
url: scene.model_url,
outPath: path.join(projectDir, fileName),
override,
});
const dotIdx = fileName.lastIndexOf(".");
const fileNameWithoutExt = fileName.substring(0, dotIdx);
copyFileSync(
path.join(projectDir, fileName),
path.join(projectDir, `${fileNameWithoutExt}.glb`)
);
}
if (scene.screenshot_url) {
const url = new URL(scene.screenshot_url);
const fileName = url.pathname.split("/").pop();
if (fileName) {
await downloadFile({
url: scene.screenshot_url,
outPath: path.join(projectDir, fileName),
override,
});
}
}
}

async function backupSpokeProjects(
api: ReticulumApi,
directory: string,
Expand Down Expand Up @@ -222,37 +267,9 @@ async function backupSpokeProjects(
}
}
if (projectScene.scene) {
writeFileSync(
path.join(projectDir, `${projectScene.scene.scene_id}.json`),
JSON.stringify(projectScene, null, 2),
{ encoding: "utf-8" }
);
const url = new URL(projectScene.scene.model_url);
const fileName = url.pathname.split("/").pop();
if (fileName) {
await downloadFile({
url: projectScene.scene.model_url,
outPath: path.join(projectDir, fileName),
override,
});
const dotIdx = fileName.lastIndexOf(".");
const fileNameWithoutExt = fileName.substring(0, dotIdx);
copyFileSync(
path.join(projectDir, fileName),
path.join(projectDir, `${fileNameWithoutExt}.glb`)
);
}
if (projectScene.scene.screenshot_url) {
const url = new URL(projectScene.scene.screenshot_url);
const fileName = url.pathname.split("/").pop();
if (fileName) {
await downloadFile({
url: projectScene.scene.screenshot_url,
outPath: path.join(projectDir, fileName),
override,
});
}
}
backupScene(api, projectScene.scene, projectDir, override);
} else if (projectScene.parent_scene) {
backupScene(api, projectScene.parent_scene, projectDir, override);
} else {
log.warn(`Project ${project.project_id} doesn't have a scene`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/reticulum-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface ProjectT {

export interface ProjectSceneT {
name: string;
parent_scene: string | null;
parent_scene: SceneT | null;
project_id: string;
project_url: string;
scene: SceneT | null;
Expand Down

0 comments on commit 8acf613

Please sign in to comment.