apps: make uploadFile async

This commit is contained in:
Girish Ramakrishnan
2021-10-21 15:15:39 -07:00
parent fbaee89c7b
commit 2e3070a5c6
2 changed files with 15 additions and 20 deletions

View File

@@ -2519,33 +2519,28 @@ function downloadFile(app, filePath, callback) {
});
}
function uploadFile(app, sourceFilePath, destFilePath, callback) {
async function uploadFile(app, sourceFilePath, destFilePath) {
assert.strictEqual(typeof app, 'object');
assert.strictEqual(typeof sourceFilePath, 'string');
assert.strictEqual(typeof destFilePath, 'string');
assert.strictEqual(typeof callback, 'function');
const done = once(function (error) {
safe.fs.unlinkSync(sourceFilePath); // remove file in /tmp
if (error) return callback(new BoxError(BoxError.FS_ERROR, error.message)); // blame it on filesystem for now
callback(null);
});
// the built-in bash printf understands "%q" but not /usr/bin/printf.
// ' gets replaced with '\'' . the first closes the quote and last one starts a new one
const escapedDestFilePath = safe.child_process.execSync(`printf %q '${destFilePath.replace(/'/g, '\'\\\'\'')}'`, { shell: '/bin/bash', encoding: 'utf8' });
debug(`uploadFile: ${sourceFilePath} -> ${escapedDestFilePath}`);
exec(app, { cmd: [ 'bash', '-c', `cat - > ${escapedDestFilePath}` ], tty: false }, function (error, stream) {
if (error) return done(error);
const destStream = await exec(app, { cmd: [ 'bash', '-c', `cat - > ${escapedDestFilePath}` ], tty: false });
var readFile = fs.createReadStream(sourceFilePath);
readFile.on('error', done);
return new Promise((resolve, reject) => {
const done = once(error => reject(new BoxError(BoxError.FS_ERROR, error.message)));
stream.on('error', done);
stream.on('finish', done);
const sourceStream = fs.createReadStream(sourceFilePath);
sourceStream.on('error', done);
destStream.on('error', done);
readFile.pipe(stream);
destStream.on('finish', resolve);
sourceStream.pipe(destStream);
});
}