mirror of
https://github.com/bitwarden/browser.git
synced 2024-11-23 11:56:00 +01:00
create attachment command
This commit is contained in:
parent
549422ea63
commit
0a518a96b1
@ -1,4 +1,6 @@
|
||||
import * as program from 'commander';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
import { CipherService } from 'jslib/abstractions/cipher.service';
|
||||
import { FolderService } from 'jslib/services/folder.service';
|
||||
@ -6,6 +8,7 @@ import { FolderService } from 'jslib/services/folder.service';
|
||||
import { Response } from '../models/response';
|
||||
import { StringResponse } from '../models/response/stringResponse';
|
||||
|
||||
import { Attachment } from '../models/attachment';
|
||||
import { Cipher } from '../models/cipher';
|
||||
import { Folder } from '../models/folder';
|
||||
|
||||
@ -15,25 +18,29 @@ export class CreateCommand {
|
||||
constructor(private cipherService: CipherService, private folderService: FolderService) { }
|
||||
|
||||
async run(object: string, requestJson: string, cmd: program.Command): Promise<Response> {
|
||||
if (requestJson == null || requestJson === '') {
|
||||
requestJson = await CliUtils.readStdin();
|
||||
}
|
||||
|
||||
if (requestJson == null || requestJson === '') {
|
||||
return Response.badRequest('`requestJson` was not provided.');
|
||||
}
|
||||
|
||||
let req: any = null;
|
||||
try {
|
||||
const reqJson = new Buffer(requestJson, 'base64').toString();
|
||||
req = JSON.parse(reqJson);
|
||||
} catch (e) {
|
||||
return Response.badRequest('Error parsing the encoded request data.');
|
||||
if (object !== 'attachment') {
|
||||
if (requestJson == null || requestJson === '') {
|
||||
requestJson = await CliUtils.readStdin();
|
||||
}
|
||||
|
||||
if (requestJson == null || requestJson === '') {
|
||||
return Response.badRequest('`requestJson` was not provided.');
|
||||
}
|
||||
|
||||
try {
|
||||
const reqJson = new Buffer(requestJson, 'base64').toString();
|
||||
req = JSON.parse(reqJson);
|
||||
} catch (e) {
|
||||
return Response.badRequest('Error parsing the encoded request data.');
|
||||
}
|
||||
}
|
||||
|
||||
switch (object.toLowerCase()) {
|
||||
case 'item':
|
||||
return await this.createCipher(req);
|
||||
case 'attachment':
|
||||
return await this.createAttachment(cmd);
|
||||
case 'folder':
|
||||
return await this.createFolder(req);
|
||||
default:
|
||||
@ -51,6 +58,35 @@ export class CreateCommand {
|
||||
}
|
||||
}
|
||||
|
||||
private async createAttachment(cmd: program.Command) {
|
||||
if (cmd.itemid == null || cmd.itemid === '') {
|
||||
return Response.badRequest('--itemid <itemid> required.');
|
||||
}
|
||||
if (cmd.file == null || cmd.file === '') {
|
||||
return Response.badRequest('--file <file> required.');
|
||||
}
|
||||
const filePath = path.resolve(cmd.file);
|
||||
if (!fs.existsSync(cmd.file)) {
|
||||
return Response.badRequest('Cannot find file at ' + filePath);
|
||||
}
|
||||
|
||||
// TODO: premium and key check
|
||||
|
||||
const cipher = await this.cipherService.get(cmd.itemid);
|
||||
if (cipher == null) {
|
||||
return Response.notFound();
|
||||
}
|
||||
|
||||
try {
|
||||
const fileBuf = fs.readFileSync(filePath);
|
||||
await this.cipherService.saveAttachmentRawWithServer(cipher, path.basename(filePath),
|
||||
new Uint8Array(fileBuf).buffer);
|
||||
return Response.success();
|
||||
} catch (e) {
|
||||
return Response.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
private async createFolder(req: Folder) {
|
||||
const folder = await this.folderService.encrypt(Folder.toView(req));
|
||||
try {
|
||||
|
@ -267,11 +267,14 @@ export class Program {
|
||||
|
||||
program
|
||||
.command('create <object> [encodedJson]')
|
||||
.option('--file <file>', 'Path to file for attachment.')
|
||||
.option('--itemid <itemid>', 'Attachment\'s item id.')
|
||||
.description('Create an object.')
|
||||
.on('--help', () => {
|
||||
writeLn('\n Objects:');
|
||||
writeLn('');
|
||||
writeLn(' item');
|
||||
writeLn(' attachment');
|
||||
writeLn(' folder');
|
||||
writeLn('');
|
||||
writeLn(' Notes:');
|
||||
@ -282,6 +285,7 @@ export class Program {
|
||||
writeLn('');
|
||||
writeLn(' bw create folder eyJuYW1lIjoiTXkgRm9sZGVyIn0K');
|
||||
writeLn(' echo \'eyJuYW1lIjoiTXkgRm9sZGVyIn0K\' | bw create folder');
|
||||
writeLn(' bw create attachment --file ./myfile.csv --itemid 16b15b89-65b3-4639-ad2a-95052a6d8f66');
|
||||
writeLn('');
|
||||
})
|
||||
.action(async (object, encodedJson, cmd) => {
|
||||
|
Loading…
Reference in New Issue
Block a user