Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
binary (#45)
Browse files Browse the repository at this point in the history
* init

* update

* retrieve Buffer type response body for binary type response

* update
  • Loading branch information
yingwang-us authored Feb 18, 2020
1 parent 10a9a5f commit e85e63e
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/RemoteResourceS3DecryptController.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,24 @@ module.exports = class RemoteResourceS3DecryptController extends RemoteResourceS
}

async download(reqOpt) {

// disable response encoding, so that res.body is Buffer type
reqOpt.encoding = null;
// get full response payload to avoid http chunked data
reqOpt.resolveWithFullResponse = true;
let res = await super.download(reqOpt);
if (res.statusCode != 200) {
return res;
}

let source = reqOpt.uri || reqOpt.url;

let isBinary = false;
if (res.headers['content-type'] === 'binary/octet-stream') {
isBinary = true;
} else {
// if response is not binary, reset body to utf-8 string
res.body = res.body.toString('utf8');
}
let alpha1Keys = objectPath.get(this.data, ['object', 'spec', 'keys'], []);
let objKeys = objectPath.get(this.data, ['object', 'spec', 'gpg', 'privateKeyRefs'], []);
let strKeys = objectPath.get(this.data, ['object', 'spec', 'gpg', 'privateKeys'], []);
Expand Down Expand Up @@ -97,15 +107,21 @@ module.exports = class RemoteResourceS3DecryptController extends RemoteResourceS
this.log.debug('All Keys found:', options.privateKeys.map(k => k.getUserIds()));

try {
this.log.debug(`Downloaded from ${source}`);
this.log.info(`Downloaded from ${source} type: ${Buffer.isBuffer(res.body) ? 'Buffer' : typeof res.body} length: ${res.body.length}`);
const isCompressed = source.includes('.tar') || source.includes('.tgz');
if (source.includes('.gpg')) {
this.log.debug(`Decrypting ${reqOpt.uri || reqOpt.url}`);
objectPath.set(options, 'message', await openpgp.message.readArmored(res.body));
this.log.debug(`Decrypting ${reqOpt.uri || reqOpt.url} isBinary: ${isBinary} isCompressed: ${isCompressed}`);
if (isBinary) {
objectPath.set(options, 'message', await openpgp.message.read(res.body));
objectPath.set(options, 'format', 'binary');
} else {
objectPath.set(options, 'message', await openpgp.message.readArmored(res.body));
}
let plaintext = await openpgp.decrypt(options);
res.body = plaintext.data;
this.log.debug(`Decrypting Succeeded ${reqOpt.uri || reqOpt.url}`);
}
if (source.includes('.tar') || source.includes('.tgz')) {
if (isCompressed) {
res.body = await this.uncompress(res.body);
}
return res;
Expand Down

0 comments on commit e85e63e

Please sign in to comment.