1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-13 23:03:32 +00:00

restructure attachment commands

This commit is contained in:
Kyle Spearrin
2018-05-17 15:55:44 -04:00
parent 0a518a96b1
commit c0422ec77f
9 changed files with 72 additions and 82 deletions

View File

@@ -15,11 +15,9 @@ export class DeleteCommand {
switch (object.toLowerCase()) {
case 'item':
if (cmd.attachmentid == null || cmd.attachmentid === '') {
return await this.deleteCipher(id);
} else {
return await this.deleteAttachment(id, cmd.attachmentid);
}
return await this.deleteCipher(id);
case 'attachment':
return await this.deleteAttachment(id, cmd);
case 'folder':
return await this.deleteFolder(id);
default:
@@ -41,30 +39,28 @@ export class DeleteCommand {
}
}
private async deleteAttachment(id: string, attachmentId: string) {
attachmentId = attachmentId.toLowerCase();
private async deleteAttachment(id: string, cmd: program.Command) {
if (cmd.itemid == null || cmd.itemid === '') {
return Response.badRequest('--itemid <itemid> required.');
}
const encCipher = await this.cipherService.get(id);
if (encCipher == null) {
const itemId = cmd.itemid.toLowerCase();
const cipher = await this.cipherService.get(itemId);
if (cipher == 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);
const attachments = cipher.attachments.filter((a) => a.id.toLowerCase() === id);
if (attachments.length === 0) {
return Response.error('Attachment `' + attachmentId + '` was not found.');
}
if (attachments.length > 1) {
return Response.multipleResults(attachments.map((a) => a.id));
return Response.error('Attachment `' + id + '` was not found.');
}
try {
await this.cipherService.deleteAttachmentWithServer(id, attachments[0].id);
await this.cipherService.deleteAttachmentWithServer(cipher.id, attachments[0].id);
return Response.success();
} catch (e) {
return Response.error(e);