Skip to content

Commit

Permalink
Now files can be password protected
Browse files Browse the repository at this point in the history
  • Loading branch information
omkarshelar committed Jun 20, 2020
1 parent ca7d14c commit bb3d638
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 60 deletions.
170 changes: 110 additions & 60 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
#!/usr/bin/env node
const utils = require("./utils");
const fs = require("fs");
const mime = require("mime");
const { program } = require("commander");

const fs = require('fs');
const mime = require('mime');
const { program } = require('commander');
program.version("1.1.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. Minimum 5 minutes",
5
)
.option("-p, --password", "To password protect your file.");

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. Minimum 5 minutes', 5);

program.on('--help', () => {
console.log('Example call:');
console.log('$ fha -f hello.txt');
console.log('$ fha -f hello.txt -e 30');
program.on("--help", () => {
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();
Expand All @@ -29,60 +35,104 @@ if (program.expire < 5) {
console.error("Expiry of objects should be more than 5 minutes");
}

const axios = require('axios').default;
const baseURL = "https://fha.omkarshelar.dev"
let binaryFile;
try {
binaryFile = fs.readFileSync(program.file);
} catch {
console.error(`Error: File named ${program.file} NOT found.`);
process.exit(1);
// Ask for password
if (program.password) {
utils.askPassword().then(password => {
console.log("\n");
uploadFile(password);
});
}

fileName = program.file.slice(program.file.lastIndexOf("/") + 1);

if (!process.env.FH_API_KEY) {
console.log("API KEY not in environment variables. Please provide API KEY.");
process.exit(1);
else {
uploadFile();
}

// API key needed to authenticate
const baseHeaders = {
"x-api-key": process.env.FH_API_KEY
};
function uploadFile(password = null) {
const axios = require("axios").default;
const baseURL = "https://fha.omkarshelar.dev";
let binaryFile;
try {
binaryFile = fs.readFileSync(program.file);
} catch {
console.error(`Error: File named ${program.file} NOT found.`);
process.exit(1);
}

fileName = program.file.slice(program.file.lastIndexOf("/") + 1);

// Make requests to the API and S3
axios.get(`${baseURL}/signed-url-upload/${fileName}`, {
headers: {
...baseHeaders
if (!process.env.FH_API_KEY) {
console.log(
"API KEY not in environment variables. Please provide API KEY."
);
process.exit(1);
}
}).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, {

// 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: {
'Content-Type': contentType
...baseHeaders
}
}).then(res => {
if (res.status === 200) {
const expiryTime = Math.floor(new Date().getTime() / 1000) + 60 * program.expire;
axios.post(`${baseURL}/custom-uri/${objectKey}/${expiryTime}`, {}, {
headers: {
...baseHeaders
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: {
"Content-Type": contentType
}
}).then(res => {
if (res.status === 200) {
const expiryTime =
Math.floor(
new Date().getTime() / 1000
) +
60 * program.expire;
let customURIData = {};
if (password) {
customURIData.password = password;
}
})
.then(res => {
console.log(`Download URL: ${baseURL}/asset/${res.data.URL}`);
const options = {
weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', timeZoneName: 'long', hour: 'numeric',
minute: 'numeric',
second: 'numeric'
};
console.log(`URL Expiry : ${new Date(expiryTime * 1000).toLocaleString('en-US', options)}`)
})
.catch(err => console.error(err));
}
axios.post(
`${baseURL}/custom-uri/${objectKey}/${expiryTime}`,
customURIData,
{
headers: {
...baseHeaders
}
}
)
.then(res => {
console.log(
`Download URL: ${baseURL}/asset/${res.data.URL}`
);
const options = {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
timeZoneName: "long",
hour: "numeric",
minute: "numeric",
second: "numeric"
};
console.log(
`URL Expiry : ${new Date(
expiryTime *
1000
).toLocaleString(
"en-US",
options
)}`
);
})
.catch(err => console.error(err));
}
});
});
});
}
32 changes: 32 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var readline = require("readline");
var Writable = require("stream").Writable;

function askPassword() {
var mutableStdout = new Writable({
write: function (chunk, encoding, callback) {
if (!this.muted) process.stdout.write(chunk, encoding);
callback();
}
});

mutableStdout.muted = false;

var rl = readline.createInterface({
input: process.stdin,
output: mutableStdout,
terminal: true
});

let pass = new Promise((resolve, reject) => {
rl.question("Enter Password: ", function (password) {
rl.close();
resolve(password);
});
});
mutableStdout.muted = true;
return pass;


}

module.exports.askPassword = askPassword;

0 comments on commit bb3d638

Please sign in to comment.