Skip to content

Commit

Permalink
Allow templates to influence videos and configs
Browse files Browse the repository at this point in the history
  • Loading branch information
uellenberg committed Sep 13, 2022
1 parent 745863a commit 9801ac6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
"@protobuf-ts/runtime": "^2.7.0",
"axios": "^0.27.2",
"form-data": "^4.0.0",
"lodash": "^4.17.21",
"walkdir": "^0.4.1",
"ws": "^8.8.0"
},
"devDependencies": {
"@protobuf-ts/plugin": "^2.6.0",
"@types/lodash": "^4.14.185",
"@types/node": "^18.0.0",
"@types/ws": "^8.5.3",
"@typescript-eslint/eslint-plugin": "^5.28.0",
Expand Down
51 changes: 43 additions & 8 deletions src/upload/lesson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as ProxyInDeleteFile from "../proto_proxy_in/delete_file";
import * as ProxyInSetFile from "../proto_proxy_in/set_file";
import { TextEncoder } from "util";
import FormData from "form-data";
import defaultsDeep from "lodash/defaultsDeep";

export const websockets: WebSocket[] = [];

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

// Now, we need to upload the config file, if it exists.
let configData: object | null = null;

// Let's first try the main config.
if (fs.existsSync(Path.join(dir, "config.json"))) {
const configForm = new FormData();
configForm.append(
"config",
fs.createReadStream(Path.join(dir, "config.json")),
configData = JSON.parse(
fs.readFileSync(Path.join(dir, "config.json"), "utf-8"),
);
}

// Now, we can try to load the template config object.
// We'll use it as a default for the main config object,
// so anything added to it is overridable.
if (templateDir && fs.existsSync(Path.join(templateDir, "config.json"))) {
const templateConfig = JSON.parse(
fs.readFileSync(Path.join(templateDir, "config.json"), "utf-8"),
);
// This will handle configData being null.
configData = defaultsDeep(configData, templateConfig);
}

// Now, let's upload the config if it's valid.
if (configData) {
const configBuffer = Buffer.from(JSON.stringify(configData), "utf-8");

const configForm = new FormData();
configForm.append("config", configBuffer, {
filename: "config.json",
contentType: "application/json",
knownLength: configBuffer.length,
});

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

// Next, we need to upload the video, if it exists.
let videoPath: string | null = null;

// We'll use the template video as a default.
if (templateDir && fs.existsSync(Path.join(templateDir, "video.cv"))) {
videoPath = Path.join(templateDir, "video.cv");
}

// And override it with the main one.
if (fs.existsSync(Path.join(dir, "video.cv"))) {
videoPath = Path.join(dir, "video.cv");
}

// Now, we'll upload the video if it exists.
if (videoPath) {
const videoForm = new FormData();
videoForm.append(
"video",
fs.createReadStream(Path.join(dir, "video.cv")),
);
videoForm.append("video", fs.createReadStream(videoPath));

await axios.put(
"https://cratecode.com/internal/api/video/upload/" + lessonID,
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.11.tgz#d421b6c527a3037f7c84433fd2c4229e016863d3"
integrity sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==

"@types/lodash@^4.14.185":
version "4.14.185"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.185.tgz#c9843f5a40703a8f5edfd53358a58ae729816908"
integrity sha512-evMDG1bC4rgQg4ku9tKpuMh5iBNEwNa3tf9zRHdP1qlv+1WUg44xat4IxCE14gIpZRGUUWAx2VhItCZc25NfMA==

"@types/node@*", "@types/node@^18.0.0":
version "18.0.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.0.0.tgz#67c7b724e1bcdd7a8821ce0d5ee184d3b4dd525a"
Expand Down Expand Up @@ -678,6 +683,11 @@ lodash.merge@^4.6.2:
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==

lodash@^4.17.21:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==

lru-cache@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94"
Expand Down

0 comments on commit 9801ac6

Please sign in to comment.