bayfiles-cli
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "bayfiles-cli", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "a cli for bayfiles.com, anonfile.com and megaupload.nz", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,2 +5,3 @@ #!/usr/bin/env node | ||
const { red, green, yellow } = require('colors') | ||
const fs = require('fs') | ||
@@ -13,3 +14,3 @@ require('yargs').command( | ||
.positional('file', { | ||
describe: 'the file to upload', | ||
describe: 'The file to upload', | ||
type: 'string' | ||
@@ -19,3 +20,3 @@ }) | ||
alias: 's', | ||
describe: 'site to upload', | ||
describe: 'Site to upload', | ||
choices: ['bayfiles', 'anonfile', 'megaupload'], | ||
@@ -26,12 +27,18 @@ default: 'bayfiles' | ||
alias: 'q', | ||
describe: 'If this option is set to false, log messages won\'t appear', | ||
default: false | ||
describe: 'If set, log messages won\'t appear', | ||
type: 'boolean' | ||
}) | ||
.option('write-to-file', { | ||
alias: 'w', | ||
describe: 'Append the urls to a file' | ||
}) | ||
}, | ||
async ({ site, files, quiet }) => { | ||
async ({ site, files, quiet, w: writeTo }) => { | ||
writeTo = writeTo && fs.createWriteStream(writeTo, { flags: 'a' }) | ||
for (const file of files) { | ||
try { | ||
if(!quiet) console.log(yellow(`Uploading ${file}...`)) | ||
if (!quiet) console.log(yellow(`Uploading ${file}...`)) | ||
const result = await upload(site, join(process.cwd(), file)) | ||
if(!quiet) { | ||
if (!quiet) { | ||
console.log(green(`File uploaded successfully: ${result.url.full}`)) | ||
@@ -41,2 +48,4 @@ } else { | ||
} | ||
writeTo && writeTo.write(result.url.full + "\n") | ||
} catch (e) { | ||
@@ -46,3 +55,5 @@ console.error(red(`An error occurred when uploading the file: ${e.message}`)) | ||
} | ||
writeTo && writeTo.close() | ||
} | ||
).argv |
@@ -16,33 +16,39 @@ const fs = require('fs') | ||
async function upload(api, file) { | ||
if (!api || !api.match(/^https?:\/\//)) { | ||
if (!apiUrls[api]) throw new Error('Api url is not valid.') | ||
let opened | ||
try { | ||
if (!api || !api.match(/^https?:\/\//)) { | ||
if (!apiUrls[api]) throw new Error('Api url is not valid.') | ||
api = apiUrls[api] | ||
} | ||
api = apiUrls[api] | ||
} | ||
if (!file || typeof file === 'string') { | ||
if (!(await exists(file)) || !(await stat(file)).isFile()) | ||
throw new Error('File does not exist or is not a file.') | ||
if (!file || typeof file === 'string') { | ||
if (!(await exists(file)) || !(await stat(file)).isFile()) | ||
throw new Error('File does not exist or is not a file.') | ||
file = fs.createReadStream(file) | ||
} | ||
file = fs.createReadStream(file) | ||
opened = true | ||
} | ||
const form = new FormData() | ||
form.append('file', file) | ||
const form = new FormData() | ||
form.append('file', file) | ||
const res = await fetch(api, { | ||
method: 'POST', | ||
body: form | ||
}) | ||
const res = await fetch(api, { | ||
method: 'POST', | ||
body: form | ||
}) | ||
if (!res.ok && res.status !== 400) | ||
throw new Error( | ||
`Upload not ok: status ${res.status}, content: ${await res.text()}.` | ||
) | ||
if (!res.ok && res.status !== 400) | ||
throw new Error( | ||
`Upload not ok: status ${res.status}, content: ${await res.text()}.` | ||
) | ||
const json = await res.json() | ||
if (!json.status) | ||
throw new BayfileError(json.error.message, json.error.type, json.error.code) | ||
const json = await res.json() | ||
if (!json.status) | ||
throw new BayfileError(json.error.message, json.error.type, json.error.code) | ||
return json.data.file | ||
return json.data.file | ||
} finally { | ||
opened && file.close() | ||
} | ||
} | ||
@@ -49,0 +55,0 @@ |
6097
102
2