Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
xquanluu committed Jul 28, 2023
1 parent 6e7154a commit 8c9f377
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
24 changes: 14 additions & 10 deletions lib/routes/api/recent-calls.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {DbErrorBadRequest} = require('../../utils/errors');
const {getHomerApiKey, getHomerSipTrace, getHomerPcap} = require('../../utils/homer-utils');
const {getJaegerTrace} = require('../../utils/jaeger-utils');
const Account = require('../../models/account');
const { getS3Object } = require('../../utils/storage-utils');
const { getS3Object, getGoogleStorageObject } = require('../../utils/storage-utils');

const parseAccountSid = (url) => {
const arr = /Accounts\/([^\/]*)/.exec(url);
Expand Down Expand Up @@ -124,22 +124,26 @@ router.get('/:call_sid/record/:year/:month/:day/:format', async(req, res) => {
const r = await Account.retrieve(account_sid);
if (r.length === 0 || !r[0].bucket_credential) return res.sendStatus(404);
const {bucket_credential} = r[0];
const getOptions = {
...bucket_credential,
key: `${year}/${month}/${day}/${call_sid}.${format || 'mp3'}`
};
let stream;
switch (bucket_credential.vendor) {
case 'aws_s3':
const getS3Options = {
...bucket_credential,
key: `${year}/${month}/${day}/${call_sid}.${format || 'mp3'}`
};
const stream = await getS3Object(logger, getS3Options);
res.set({
'Content-Type': `audio/${format || 'mp3'}`
});
stream.pipe(res);
stream = await getS3Object(logger, getOptions);
break;
case 'google':
stream = await getGoogleStorageObject(logger, getOptions);
break;
default:
logger.error(`There is no handler for fetching record from ${bucket_credential.vendor}`);
return res.sendStatus(500);
}
res.set({
'Content-Type': `audio/${format || 'mp3'}`
});
stream.pipe(res);
} catch (err) {
logger.error({err}, ` error retrieving recording ${call_sid}`);
res.sendStatus(404);
Expand Down
19 changes: 18 additions & 1 deletion lib/utils/storage-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ function testGoogleStorage(logger, opts) {
});
}

async function getGoogleStorageObject(logger, opts) {
const serviceKey = JSON.parse(opts.service_key);
const storage = new Storage({
projectId: serviceKey.project_id,
credentials: {
client_email: serviceKey.client_email,
private_key: serviceKey.private_key
},
});

const bucket = storage.bucket(opts.name);
const file = bucket.file(opts.key);

return file.createReadStream();
}

async function testAwsS3(logger, opts) {
const s3 = new S3Client({
credentials: {
Expand Down Expand Up @@ -61,5 +77,6 @@ async function getS3Object(logger, opts) {
module.exports = {
testAwsS3,
getS3Object,
testGoogleStorage
testGoogleStorage,
getGoogleStorageObject
};

0 comments on commit 8c9f377

Please sign in to comment.