1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-09-14 02:08:50 +02:00

delete attachment from item

This commit is contained in:
Kyle Spearrin 2018-05-17 13:43:53 -04:00
parent df024379c8
commit 7abc87992a
3 changed files with 52 additions and 13 deletions

View File

@ -15,7 +15,11 @@ export class DeleteCommand {
switch (object.toLowerCase()) { switch (object.toLowerCase()) {
case 'item': case 'item':
return await this.deleteCipher(id); if (cmd.attachmentid == null || cmd.attachmentid === '') {
return await this.deleteCipher(id);
} else {
return await this.deleteAttachment(id, cmd.attachmentid);
}
case 'folder': case 'folder':
return await this.deleteFolder(id); return await this.deleteFolder(id);
default: default:
@ -37,6 +41,36 @@ export class DeleteCommand {
} }
} }
private async deleteAttachment(id: string, attachmentId: string) {
attachmentId = attachmentId.toLowerCase();
const encCipher = await this.cipherService.get(id);
if (encCipher == null) {
return Response.notFound();
}
const cipher = await encCipher.decrypt();
if (cipher.attachments == null || cipher.attachments.length === 0) {
return Response.error('No attachments available for this item.');
}
const attachments = cipher.attachments.filter((a) =>
a.id.toLowerCase() === attachmentId || a.fileName.toLowerCase() === attachmentId);
if (attachments.length === 0) {
return Response.error('Attachment `' + attachmentId + '` was not found.');
}
if (attachments.length > 1) {
return Response.multipleResults(attachments.map((a) => a.id));
}
try {
await this.cipherService.deleteAttachmentWithServer(id, attachments[0].id);
return Response.success();
} catch (e) {
return Response.error(e);
}
}
private async deleteFolder(id: string) { private async deleteFolder(id: string) {
const folder = await this.folderService.get(id); const folder = await this.folderService.get(id);
if (folder == null) { if (folder == null) {

View File

@ -47,11 +47,10 @@ export class GetCommand {
switch (object.toLowerCase()) { switch (object.toLowerCase()) {
case 'item': case 'item':
if (cmd.attachmentid === null || cmd.attachmentid === '') { if (cmd.attachmentid == null || cmd.attachmentid === '') {
return await this.getCipher(id); return await this.getCipher(id);
} else { } else {
return await this.getAttachment(id, cmd.attachmentid, cmd);
return await this.getAttachment(id, cmd);
} }
case 'username': case 'username':
return await this.getUsername(id); return await this.getUsername(id);
@ -193,7 +192,9 @@ export class GetCommand {
return Response.success(res); return Response.success(res);
} }
private async getAttachment(id: string, cmd: program.Command) { private async getAttachment(id: string, attachmentId: string, cmd: program.Command) {
attachmentId = attachmentId.toLowerCase();
// TODO: Premium check // TODO: Premium check
const cipherResponse = await this.getCipher(id); const cipherResponse = await this.getCipher(id);
@ -206,16 +207,16 @@ export class GetCommand {
return Response.error('No attachments available for this item.'); return Response.error('No attachments available for this item.');
} }
const attachment = cipher.attachments.filter((a) => const attachments = cipher.attachments.filter((a) =>
a.id === cmd.attachmentid || a.fileName === cmd.attachmentid); a.id.toLowerCase() === attachmentId || a.fileName.toLowerCase() === attachmentId);
if (attachment.length === 0) { if (attachments.length === 0) {
return Response.error('Attachment `' + cmd.attachmentid + '` was not found.'); return Response.error('Attachment `' + attachmentId + '` was not found.');
} }
if (attachment.length > 1) { if (attachments.length > 1) {
return Response.multipleResults(attachment.map((a) => a.id)); return Response.multipleResults(attachments.map((a) => a.id));
} }
const response = await fet.default(new fet.Request(attachment[0].url, { headers: { cache: 'no-cache' } })); const response = await fet.default(new fet.Request(attachments[0].url, { headers: { cache: 'no-cache' } }));
if (response.status !== 200) { if (response.status !== 200) {
return Response.error('A ' + response.status + ' error occurred while downloading the attachment.'); return Response.error('A ' + response.status + ' error occurred while downloading the attachment.');
} }
@ -224,7 +225,7 @@ export class GetCommand {
const buf = await response.arrayBuffer(); const buf = await response.arrayBuffer();
const key = await this.cryptoService.getOrgKey(cipher.organizationId); const key = await this.cryptoService.getOrgKey(cipher.organizationId);
const decBuf = await this.cryptoService.decryptFromBytes(buf, key); const decBuf = await this.cryptoService.decryptFromBytes(buf, key);
const filePath = await CliUtils.saveFile(new Buffer(decBuf), cmd.output, attachment[0].fileName); const filePath = await CliUtils.saveFile(new Buffer(decBuf), cmd.output, attachments[0].fileName);
const res = new MessageResponse('Saved ' + filePath, null); const res = new MessageResponse('Saved ' + filePath, null);
res.raw = filePath; res.raw = filePath;
return Response.success(res); return Response.success(res);

View File

@ -324,6 +324,7 @@ export class Program {
program program
.command('delete <object> <id>') .command('delete <object> <id>')
.option('--attachmentid <attachmentid>', 'Delete an item\'s attachment.')
.description('Delete an object.') .description('Delete an object.')
.on('--help', () => { .on('--help', () => {
writeLn('\n Objects:'); writeLn('\n Objects:');
@ -337,7 +338,10 @@ export class Program {
writeLn(''); writeLn('');
writeLn(' Examples:'); writeLn(' Examples:');
writeLn(''); writeLn('');
writeLn(' bw delete item 7063feab-4b10-472e-b64c-785e2b870b92');
writeLn(' bw delete folder 5cdfbd80-d99f-409b-915b-f4c5d0241b02'); writeLn(' bw delete folder 5cdfbd80-d99f-409b-915b-f4c5d0241b02');
writeLn(' bw delete item 310d5ffd-e9a2-4451-af87-ea054dce0f78 --attachmentid b857igwl1dzrs2');
writeLn(' bw delete item 310d5ffd-e9a2-4451-af87-ea054dce0f78 --attachmentid photo.jpg');
writeLn(''); writeLn('');
}) })
.action(async (object, id, cmd) => { .action(async (object, id, cmd) => {