Skip to content

Commit 9801ac6

Browse files
committed
Allow templates to influence videos and configs
1 parent 745863a commit 9801ac6

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
"@protobuf-ts/runtime": "^2.7.0",
2121
"axios": "^0.27.2",
2222
"form-data": "^4.0.0",
23+
"lodash": "^4.17.21",
2324
"walkdir": "^0.4.1",
2425
"ws": "^8.8.0"
2526
},
2627
"devDependencies": {
2728
"@protobuf-ts/plugin": "^2.6.0",
29+
"@types/lodash": "^4.14.185",
2830
"@types/node": "^18.0.0",
2931
"@types/ws": "^8.5.3",
3032
"@typescript-eslint/eslint-plugin": "^5.28.0",

src/upload/lesson.ts

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as ProxyInDeleteFile from "../proto_proxy_in/delete_file";
1111
import * as ProxyInSetFile from "../proto_proxy_in/set_file";
1212
import { TextEncoder } from "util";
1313
import FormData from "form-data";
14+
import defaultsDeep from "lodash/defaultsDeep";
1415

1516
export const websockets: WebSocket[] = [];
1617

@@ -102,12 +103,36 @@ export async function handleLesson(
102103
if (!lessonID) throw new Error("Could not create or find a lesson!");
103104

104105
// Now, we need to upload the config file, if it exists.
106+
let configData: object | null = null;
107+
108+
// Let's first try the main config.
105109
if (fs.existsSync(Path.join(dir, "config.json"))) {
106-
const configForm = new FormData();
107-
configForm.append(
108-
"config",
109-
fs.createReadStream(Path.join(dir, "config.json")),
110+
configData = JSON.parse(
111+
fs.readFileSync(Path.join(dir, "config.json"), "utf-8"),
112+
);
113+
}
114+
115+
// Now, we can try to load the template config object.
116+
// We'll use it as a default for the main config object,
117+
// so anything added to it is overridable.
118+
if (templateDir && fs.existsSync(Path.join(templateDir, "config.json"))) {
119+
const templateConfig = JSON.parse(
120+
fs.readFileSync(Path.join(templateDir, "config.json"), "utf-8"),
110121
);
122+
// This will handle configData being null.
123+
configData = defaultsDeep(configData, templateConfig);
124+
}
125+
126+
// Now, let's upload the config if it's valid.
127+
if (configData) {
128+
const configBuffer = Buffer.from(JSON.stringify(configData), "utf-8");
129+
130+
const configForm = new FormData();
131+
configForm.append("config", configBuffer, {
132+
filename: "config.json",
133+
contentType: "application/json",
134+
knownLength: configBuffer.length,
135+
});
111136

112137
await axios.put(
113138
"https://cratecode.com/internal/api/config/upload/" + lessonID,
@@ -123,12 +148,22 @@ export async function handleLesson(
123148
}
124149

125150
// Next, we need to upload the video, if it exists.
151+
let videoPath: string | null = null;
152+
153+
// We'll use the template video as a default.
154+
if (templateDir && fs.existsSync(Path.join(templateDir, "video.cv"))) {
155+
videoPath = Path.join(templateDir, "video.cv");
156+
}
157+
158+
// And override it with the main one.
126159
if (fs.existsSync(Path.join(dir, "video.cv"))) {
160+
videoPath = Path.join(dir, "video.cv");
161+
}
162+
163+
// Now, we'll upload the video if it exists.
164+
if (videoPath) {
127165
const videoForm = new FormData();
128-
videoForm.append(
129-
"video",
130-
fs.createReadStream(Path.join(dir, "video.cv")),
131-
);
166+
videoForm.append("video", fs.createReadStream(videoPath));
132167

133168
await axios.put(
134169
"https://cratecode.com/internal/api/video/upload/" + lessonID,

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@
9393
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.11.tgz#d421b6c527a3037f7c84433fd2c4229e016863d3"
9494
integrity sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==
9595

96+
"@types/lodash@^4.14.185":
97+
version "4.14.185"
98+
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.185.tgz#c9843f5a40703a8f5edfd53358a58ae729816908"
99+
integrity sha512-evMDG1bC4rgQg4ku9tKpuMh5iBNEwNa3tf9zRHdP1qlv+1WUg44xat4IxCE14gIpZRGUUWAx2VhItCZc25NfMA==
100+
96101
"@types/node@*", "@types/node@^18.0.0":
97102
version "18.0.0"
98103
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.0.0.tgz#67c7b724e1bcdd7a8821ce0d5ee184d3b4dd525a"
@@ -678,6 +683,11 @@ lodash.merge@^4.6.2:
678683
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
679684
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==
680685

686+
lodash@^4.17.21:
687+
version "4.17.21"
688+
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
689+
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
690+
681691
lru-cache@^6.0.0:
682692
version "6.0.0"
683693
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94"

0 commit comments

Comments
 (0)