mirror of
https://github.com/cupcakearmy/cryptgeon.git
synced 2024-12-22 08:16:28 +00:00
add --all option, stdin and password option
This commit is contained in:
parent
2e89007c83
commit
b43b802221
@ -7,7 +7,7 @@ import pretty from 'pretty-bytes'
|
||||
|
||||
import { exit } from './utils'
|
||||
|
||||
export async function download(url: URL) {
|
||||
export async function download(url: URL, all: boolean, suggestedPassword?: string) {
|
||||
setBase(url.origin)
|
||||
const id = url.pathname.split('/')[2]
|
||||
const preview = await info(id).catch(() => exit('Note does not exist or is expired'))
|
||||
@ -16,6 +16,9 @@ export async function download(url: URL) {
|
||||
let password: string
|
||||
const derivation = preview?.meta.derivation
|
||||
if (derivation) {
|
||||
if (suggestedPassword) {
|
||||
password = suggestedPassword
|
||||
} else {
|
||||
const response = await inquirer.prompt([
|
||||
{
|
||||
type: 'password',
|
||||
@ -24,6 +27,7 @@ export async function download(url: URL) {
|
||||
},
|
||||
])
|
||||
password = response.password
|
||||
}
|
||||
} else {
|
||||
password = url.hash.slice(1)
|
||||
}
|
||||
@ -39,6 +43,11 @@ export async function download(url: URL) {
|
||||
exit('No files found in note')
|
||||
return
|
||||
}
|
||||
|
||||
let selected: typeof files
|
||||
if (all) {
|
||||
selected = files
|
||||
} else {
|
||||
const { names } = await inquirer.prompt([
|
||||
{
|
||||
type: 'checkbox',
|
||||
@ -51,13 +60,12 @@ export async function download(url: URL) {
|
||||
})),
|
||||
},
|
||||
])
|
||||
|
||||
const selected = files.filter((file) => names.includes(file.name))
|
||||
selected = files.filter((file) => names.includes(file.name))
|
||||
}
|
||||
|
||||
if (!selected.length) exit('No files selected')
|
||||
|
||||
await Promise.all(
|
||||
files.map(async (file) => {
|
||||
selected.map(async (file) => {
|
||||
let filename = resolve(file.name)
|
||||
try {
|
||||
// If exists -> prepend timestamp to not overwrite the current file
|
||||
@ -68,6 +76,7 @@ export async function download(url: URL) {
|
||||
console.log(`Saved: ${basename(filename)}`)
|
||||
})
|
||||
)
|
||||
|
||||
break
|
||||
case 'text':
|
||||
const plaintext = await Adapters.Text.decrypt(note.contents, key).catch(couldNotDecrypt)
|
||||
|
@ -15,6 +15,7 @@ const server = new Option('-s --server <url>', 'the cryptgeon server to use').de
|
||||
const files = new Argument('<file...>', 'Files to be sent').argParser(parseFile)
|
||||
const text = new Argument('<text>', 'Text content of the note')
|
||||
const password = new Option('-p --password <string>', 'manually set a password')
|
||||
const all = new Option('-a --all', 'Save all files without prompt').default(false)
|
||||
const url = new Argument('<url>', 'The url to open')
|
||||
const views = new Option('-v --views <number>', 'Amount of views before getting destroyed').argParser(parseNumber)
|
||||
const minutes = new Option('-m --minutes <number>', 'Minutes before the note expires').argParser(parseNumber)
|
||||
@ -86,10 +87,13 @@ send
|
||||
program
|
||||
.command('open')
|
||||
.addArgument(url)
|
||||
.addOption(password)
|
||||
.addOption(all)
|
||||
.action(async (note, options) => {
|
||||
try {
|
||||
const url = new URL(note)
|
||||
await download(url)
|
||||
options.password ||= await getStdin()
|
||||
await download(url, options.all, options.password)
|
||||
} catch {
|
||||
exit('Invalid URL')
|
||||
}
|
||||
|
@ -2,19 +2,23 @@ export function getStdin(timeout: number = 10): Promise<string> {
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
// Store the data from stdin in a buffer
|
||||
let buffer = ''
|
||||
process.stdin.on('data', (d) => (buffer += d.toString()))
|
||||
let t: NodeJS.Timeout
|
||||
|
||||
const dataHandler = (d: Buffer) => (buffer += d.toString())
|
||||
const endHandler = () => {
|
||||
clearTimeout(t)
|
||||
resolve(buffer.trim())
|
||||
}
|
||||
|
||||
// Stop listening for data after the timeout, otherwise hangs indefinitely
|
||||
const t = setTimeout(() => {
|
||||
process.stdin.destroy()
|
||||
t = setTimeout(() => {
|
||||
process.stdin.removeListener('data', dataHandler)
|
||||
process.stdin.removeListener('end', endHandler)
|
||||
process.stdin.pause()
|
||||
resolve('')
|
||||
}, timeout)
|
||||
|
||||
// Listen for end and error events
|
||||
process.stdin.on('end', () => {
|
||||
clearTimeout(t)
|
||||
resolve(buffer.trim())
|
||||
})
|
||||
process.stdin.on('error', reject)
|
||||
process.stdin.on('data', dataHandler)
|
||||
process.stdin.on('end', endHandler)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user