Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for backing up parent scenes #15

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading