@@ -11,6 +11,7 @@ import * as ProxyInDeleteFile from "../proto_proxy_in/delete_file";
1111import * as ProxyInSetFile from "../proto_proxy_in/set_file" ;
1212import { TextEncoder } from "util" ;
1313import FormData from "form-data" ;
14+ import defaultsDeep from "lodash/defaultsDeep" ;
1415
1516export 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 ,
0 commit comments