Skip to content

Commit

Permalink
Added code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
omkarshelar committed Jun 13, 2020
1 parent 532f32f commit ca7d14c
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@ const fs = require('fs');
const mime = require('mime');
const { program } = require('commander');

program.version('1.0.0');
program.version('1.1.0');

program
.requiredOption('-f, --file <path-to-file>', 'Expiry time of the object in minutes')
.option('-e, --expire <expire-minutes>', 'Expiry time of the object in minutes. Default is 5 minutes. Minimum 5 minutes', 5);
.option('-e, --expire <expire-minutes>', 'Expiry time of the object in minutes. Minimum 5 minutes', 5);

program.on('--help', () => {
console.log('');
console.log('Example call:');
console.log('$ fha -f hello.txt');
console.log('$ fha -f hello.txt -e 30');
});


// If no arguments more arguments sent to application, display help.
if (process.argv.length === 2) {
program.outputHelp();
process.exit(0);
}

program.parse(process.argv);

// Check minimum time
if (program.expire < 5) {
console.error("Expiry of objects should be more than 5 minutes");
}
Expand All @@ -44,22 +45,24 @@ if (!process.env.FH_API_KEY) {
console.log("API KEY not in environment variables. Please provide API KEY.");
process.exit(1);
}

// API key needed to authenticate
const baseHeaders = {
"x-api-key": process.env.FH_API_KEY
};

// Make requests to the API and S3
axios.get(`${baseURL}/signed-url-upload/${fileName}`, {
headers: {
...baseHeaders
}
}).then(res => {
const uploadURL = res.data.UploadURL
const objectKey = res.data.Key

// Calculate mime/type of the file
contentType = mime.getType(fileName.slice(fileName.lastIndexOf(".") + 1));
axios.put(uploadURL, binaryFile, {
headers: {
...baseHeaders,
'Content-Type': contentType
}
}).then(res => {
Expand All @@ -83,4 +86,3 @@ axios.get(`${baseURL}/signed-url-upload/${fileName}`, {
}
});
});

0 comments on commit ca7d14c

Please sign in to comment.