@dosmond37/firebase-storage-cli
Version:
Upload anything, right from your command line.
129 lines (128 loc) • 4.77 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ora = require("ora");
const path = require("path");
const chalk = require("chalk");
const fs = require("fs-extra");
const clipboardy = require("clipboardy");
const command_1 = require("@oclif/command");
const utils_1 = require("../utils");
class Upload extends command_1.Command {
async copyToClipboard(link) {
await clipboardy.write(link);
this.log(chalk.gray.bold('\nURL has been copied to your clipboard.'));
}
async appendToHistory(event) {
let history = [];
await fs.ensureDir(this.config.configDir);
const historyPath = path.join(this.config.configDir, 'history.json');
try {
history = await fs.readJSON(historyPath);
}
catch (error) { }
history = history.filter((x) => x.name !== event.name);
history.unshift(event);
await fs.writeJSON(historyPath, history, { spaces: 2 });
}
async run() {
const { args, flags } = this.parse(Upload);
// Get Firebase Storage Bucket
const bucket = await utils_1.getStorageBucket(this);
// Check for path of file to be uploaded
// Priority: flag > CLI argument
const filePath = flags.file || args.file || '';
if (filePath === '') {
this.error(chalk.bold.red('You need to specify a file to upload, either by passing it as the first argument or using the -f option.'));
}
else if (!(await fs.pathExists(filePath))) {
this.error(chalk.bold.red('The specified file does not exist in the path you have provided, please provide a valid file path.'));
}
// Start Spinner
const spinner = ora({
text: 'Uploading file...',
spinner: 'triangle',
}).start();
// Upload File using Cloud Storage SDK
const response = await bucket.upload(filePath, {
public: flags.public,
resumable: false
});
const file = response[0];
// Stop Spinner
spinner.stop();
// Notify the terminal that file has been successfully uploaded
this.log(chalk.blue('Your file has been uploaded successfully!\n'));
const event = {
id: file.metadata.id,
name: file.metadata.name,
timeCreated: file.metadata.timeCreated,
};
this.appendToHistory(event);
// If public, then get Public URL
// Else if private, check for '-l' flag for link generation
let url = '';
if (flags.public) {
url = file.metadata.mediaLink;
this.log(chalk.blue('Public URL:'), chalk.bold.underline(url));
}
else if (flags.link) {
const fourHoursFromNow = new Date(new Date().getTime() + 60 * 60 * flags.expiry * 1000);
const signedUrlResponse = await file.getSignedUrl({
action: 'read',
expires: fourHoursFromNow,
});
url = signedUrlResponse[0];
this.log(chalk.blue.bold(`This link is valid for the next ${flags.expiry} hours.`));
this.log(chalk.blue('Private URL:'), chalk.bold.underline(url));
}
// Copy to clipboard if flag has been set
if (url !== '' && flags.clipboard) {
this.copyToClipboard(url);
}
}
}
exports.default = Upload;
Upload.description = 'upload file to storage bucket';
Upload.aliases = ['up'];
Upload.examples = [
`$ fireup upload /path/to/file`,
`$ fireup upload -f /path/to/file --public`,
`$ fireup upload -f /path/to/file -pc`,
`$ fireup upload /path/to/file -le 10`,
];
Upload.flags = {
file: command_1.flags.string({
char: 'f',
description: 'path of the file you want to upload',
}),
bucket: command_1.flags.string({
char: 'b',
description: 'link to firebase storage bucket,',
}),
public: command_1.flags.boolean({
char: 'p',
default: false,
description: 'make the file publicly-accessible',
exclusive: ['link'],
}),
link: command_1.flags.boolean({
char: 'l',
default: false,
description: 'if private, generate a temporarily downloadable link',
exclusive: ['public'],
}),
expiry: command_1.flags.integer({
char: 'e',
default: 4,
description: 'hours until expiry of private link',
}),
clipboard: command_1.flags.boolean({
char: 'c',
default: false,
description: 'copy generated link to clipboard',
}),
help: command_1.flags.help({ char: 'h' }),
};
Upload.args = [
{ name: 'file', description: 'path of the file you want to upload' },
];