Skip to content

Commit

Permalink
Add promise rejection on error to upload-file (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
lubert authored Feb 28, 2022
1 parent 30f7794 commit c16c7ef
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions utils/upload-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@ const uploadFile = (s3, params, cb = () => {}) => (new Promise((resolve, reject)
const request = s3.putObject(params);

request.on('complete', (response) => {
const { data: { ETag } } = response;
try {
const { data: { ETag } } = response;

const hash = ETag.replace(/^"|"$/g, '');
const hash = ETag.replace(/^"|"$/g, '');

const cidObj = CID.parse(hash);

let cidv0;

const cidv1 = cidObj.toV1().toString();
const cidObj = CID.parse(hash);

try {
cidv0 = cidObj.toV0().toString();
let cidv0;

const cidv1 = cidObj.toV1().toString();

try {
cidv0 = cidObj.toV0().toString();
} catch (e) {
// fallback when cbor is used
cidv0 = cidv1;
}

resolve ({ hash: cidv1, hashV0: cidv0 });
} catch (e) {
// fallback when cbor is used
cidv0 = cidv1;
reject(e);
}

resolve ({ hash: cidv1, hashV0: cidv0 });
});

request.on('error', (error) => {
Expand Down

0 comments on commit c16c7ef

Please sign in to comment.