From 0a2dae82e88b32f4c603b4141b3187e81125b728 Mon Sep 17 00:00:00 2001 From: Brikaa Date: Thu, 14 Oct 2021 21:14:10 +0200 Subject: [PATCH] Continue implementation for uploading binary files --- api/src/api/v2.js | 8 ++++++++ api/src/job.js | 16 ++++++---------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/api/src/api/v2.js b/api/src/api/v2.js index f9a3f66..1257071 100644 --- a/api/src/api/v2.js +++ b/api/src/api/v2.js @@ -86,6 +86,14 @@ function get_job(body) { }); } } + if ( + files.filter(file => !file.encoding || file.encoding === 'utf8') + .length === 0 + ) { + return reject({ + message: 'files must include at least one utf8 encoded file', + }); + } const rt = runtime.get_latest_runtime_matching_language_version( language, diff --git a/api/src/job.js b/api/src/job.js index 7acff22..4c14431 100644 --- a/api/src/job.js +++ b/api/src/job.js @@ -37,6 +37,7 @@ class Job { ? file.encoding : 'utf8', })); + this.code_files = this.files.filter(file => file.encoding === 'utf8'); this.args = args; this.stdin = stdin; @@ -81,7 +82,6 @@ class Job { for (const file of this.files) { const file_path = path.join(this.dir, file.name); const rel = path.relative(this.dir, file_path); - const file_content = Buffer.from(file.content, file.encoding); if (rel.startsWith('..')) throw Error( @@ -94,7 +94,7 @@ class Job { }); await fs.chown(path.dirname(file_path), this.uid, this.gid); - await fs.write_file(file_path, file_content); + await fs.write_file(file_path, file.content, file.encoding); await fs.chown(file_path, this.uid, this.gid); } @@ -222,8 +222,6 @@ class Job { } runtime=${this.runtime.toString()}` ); - const code_files = this.files.filter(file => file.encoding == 'utf8'); - logger.debug('Compiling'); let compile; @@ -231,7 +229,7 @@ class Job { if (this.runtime.compiled) { compile = await this.safe_call( path.join(this.runtime.pkgdir, 'compile'), - code_files.map(x => x.name), + this.code_files.map(x => x.name), this.timeouts.compile, this.memory_limits.compile ); @@ -241,7 +239,7 @@ class Job { const run = await this.safe_call( path.join(this.runtime.pkgdir, 'run'), - [code_files[0].name, ...this.args], + [this.code_files[0].name, ...this.args], this.timeouts.run, this.memory_limits.run ); @@ -270,13 +268,11 @@ class Job { } gid=${this.gid} runtime=${this.runtime.toString()}` ); - const code_files = this.files.filter(file => file.encoding == 'utf8'); - if (this.runtime.compiled) { eventBus.emit('stage', 'compile'); const { error, code, signal } = await this.safe_call( path.join(this.runtime.pkgdir, 'compile'), - code_files.map(x => x.name), + this.code_files.map(x => x.name), this.timeouts.compile, this.memory_limits.compile, eventBus @@ -289,7 +285,7 @@ class Job { eventBus.emit('stage', 'run'); const { error, code, signal } = await this.safe_call( path.join(this.runtime.pkgdir, 'run'), - [code_files[0].name, ...this.args], + [this.code_files[0].name, ...this.args], this.timeouts.run, this.memory_limits.run, eventBus