diff --git a/jslib b/jslib index ed89dfaba7..a421f6e64a 160000 --- a/jslib +++ b/jslib @@ -1 +1 @@ -Subproject commit ed89dfaba70b60925817a0ce6f0c179b3f8bd2fb +Subproject commit a421f6e64a1f47c34806419012b3983bd0505bc6 diff --git a/package-lock.json b/package-lock.json index 6c675b121d..f64a55a908 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,15 @@ "commander": "2.15.1" } }, + "@types/form-data": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/@types/form-data/-/form-data-2.2.1.tgz", + "integrity": "sha512-JAMFhOaHIciYVh8fb5/83nmuO/AHwmto+Hq7a9y8FzLDcC1KCU344XDOMEmahnrTFlHjgh4L0WJFczNIX2GxnQ==", + "dev": true, + "requires": { + "@types/node": "10.0.8" + } + }, "@types/lodash": { "version": "4.14.108", "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.108.tgz", @@ -275,8 +284,7 @@ "asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=", - "dev": true + "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" }, "atob": { "version": "2.1.1", @@ -804,7 +812,6 @@ "version": "1.0.6", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.6.tgz", "integrity": "sha1-cj599ugBrFYTETp+RFqbactjKBg=", - "dev": true, "requires": { "delayed-stream": "1.0.0" } @@ -1108,8 +1115,7 @@ "delayed-stream": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", - "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=", - "dev": true + "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" }, "des.js": { "version": "1.0.0", @@ -1658,10 +1664,9 @@ "dev": true }, "form-data": { - "version": "2.1.4", - "resolved": "https://registry.npmjs.org/form-data/-/form-data-2.1.4.tgz", - "integrity": "sha1-M8GDrPGTJ27KqYFDpp6Uv+4XUNE=", - "dev": true, + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-2.3.2.tgz", + "integrity": "sha1-SXBJi+YEwgwAXU9cI67NIda0kJk=", "requires": { "asynckit": "0.4.0", "combined-stream": "1.0.6", @@ -3029,14 +3034,12 @@ "mime-db": { "version": "1.33.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.33.0.tgz", - "integrity": "sha512-BHJ/EKruNIqJf/QahvxwQZXKygOQ256myeN/Ew+THcAa5q+PjyTTMMeNQC4DZw5AwfvelsUrA6B67NKMqXDbzQ==", - "dev": true + "integrity": "sha512-BHJ/EKruNIqJf/QahvxwQZXKygOQ256myeN/Ew+THcAa5q+PjyTTMMeNQC4DZw5AwfvelsUrA6B67NKMqXDbzQ==" }, "mime-types": { "version": "2.1.18", "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.18.tgz", "integrity": "sha512-lc/aahn+t4/SWV/qcmumYjymLsWfN3ELhpmVuUFjgsORruuZPVSwAQryq+HHGvO/SI2KVX26bx+En+zhM8g8hQ==", - "dev": true, "requires": { "mime-db": "1.33.0" } @@ -3960,6 +3963,19 @@ "tough-cookie": "2.3.4", "tunnel-agent": "0.6.0", "uuid": "3.2.1" + }, + "dependencies": { + "form-data": { + "version": "2.1.4", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-2.1.4.tgz", + "integrity": "sha1-M8GDrPGTJ27KqYFDpp6Uv+4XUNE=", + "dev": true, + "requires": { + "asynckit": "0.4.0", + "combined-stream": "1.0.6", + "mime-types": "2.1.18" + } + } } }, "request-progress": { diff --git a/package.json b/package.json index df95167817..f36917b005 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ }, "devDependencies": { "@types/commander": "^2.12.2", + "@types/form-data": "^2.2.1", "@types/lowdb": "^1.0.1", "@types/lunr": "^2.1.5", "@types/node": "^10.0.8", @@ -65,6 +66,7 @@ "dependencies": { "chalk": "2.4.1", "commander": "2.15.1", + "form-data": "2.3.2", "lowdb": "1.0.0", "node-fetch": "2.1.2", "node-forge": "0.7.1", diff --git a/src/commands/create.command.ts b/src/commands/create.command.ts index 8873f70206..194845f711 100644 --- a/src/commands/create.command.ts +++ b/src/commands/create.command.ts @@ -8,7 +8,6 @@ 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'; @@ -72,7 +71,8 @@ export class CreateCommand { // TODO: premium and key check - const cipher = await this.cipherService.get(cmd.itemid); + const itemId = cmd.itemid.toLowerCase(); + const cipher = await this.cipherService.get(itemId); if (cipher == null) { return Response.notFound(); } diff --git a/src/commands/delete.command.ts b/src/commands/delete.command.ts index 2c71cbf702..7fafe5dd21 100644 --- a/src/commands/delete.command.ts +++ b/src/commands/delete.command.ts @@ -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 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); diff --git a/src/commands/get.command.ts b/src/commands/get.command.ts index 032425b2a2..f4ae99943d 100644 --- a/src/commands/get.command.ts +++ b/src/commands/get.command.ts @@ -22,7 +22,6 @@ import { MessageResponse } from '../models/response/messageResponse'; import { StringResponse } from '../models/response/stringResponse'; import { TemplateResponse } from '../models/response/templateResponse'; -import { Attachment } from '../models/attachment'; import { Card } from '../models/card'; import { Cipher } from '../models/cipher'; import { Collection } from '../models/collection'; @@ -47,11 +46,7 @@ export class GetCommand { switch (object.toLowerCase()) { case 'item': - if (cmd.attachmentid == null || cmd.attachmentid === '') { - return await this.getCipher(id); - } else { - return await this.getAttachment(id, cmd.attachmentid, cmd); - } + return await this.getCipher(id); case 'username': return await this.getUsername(id); case 'password': @@ -62,6 +57,8 @@ export class GetCommand { return await this.getTotp(id); case 'exposed': return await this.getExposed(id); + case 'attachment': + return await this.getAttachment(id, cmd); case 'folder': return await this.getFolder(id); case 'collection': @@ -192,12 +189,15 @@ export class GetCommand { return Response.success(res); } - private async getAttachment(id: string, attachmentId: string, cmd: program.Command) { - attachmentId = attachmentId.toLowerCase(); + private async getAttachment(id: string, cmd: program.Command) { + if (cmd.itemid == null || cmd.itemid === '') { + return Response.badRequest('--itemid required.'); + } // TODO: Premium check - const cipherResponse = await this.getCipher(id); + const itemId = cmd.itemid.toLowerCase(); + const cipherResponse = await this.getCipher(itemId); if (!cipherResponse.success) { return cipherResponse; } @@ -207,10 +207,10 @@ export class GetCommand { 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 || + (a.fileName != null && a.fileName.toLowerCase().indexOf(id) > -1)); if (attachments.length === 0) { - return Response.error('Attachment `' + attachmentId + '` was not found.'); + return Response.error('Attachment `' + id + '` was not found.'); } if (attachments.length > 1) { return Response.multipleResults(attachments.map((a) => a.id)); @@ -316,9 +316,6 @@ export class GetCommand { case 'securenote': template = SecureNote.template(); break; - case 'attachment': - template = Attachment.template(); - break; case 'folder': template = Folder.template(); break; diff --git a/src/models/attachment.ts b/src/models/attachment.ts deleted file mode 100644 index 1ca4485d31..0000000000 --- a/src/models/attachment.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { AttachmentView } from 'jslib/models/view/attachmentView'; - -export class Attachment { - static template(): Attachment { - const req = new Attachment(); - req.fileName = 'photo.jpg'; - return req; - } - - static toView(req: Attachment, view = new AttachmentView()) { - view.fileName = req.fileName; - return view; - } - - fileName: string; - - // Use build method instead of ctor so that we can control order of JSON stringify for pretty print - build(o: AttachmentView) { - this.fileName = o.fileName; - } -} diff --git a/src/models/response/attachmentResponse.ts b/src/models/response/attachmentResponse.ts index 9f1c315891..8fef2aba44 100644 --- a/src/models/response/attachmentResponse.ts +++ b/src/models/response/attachmentResponse.ts @@ -1,17 +1,15 @@ import { AttachmentView } from 'jslib/models/view/attachmentView'; -import { Attachment } from '../attachment'; - -export class AttachmentResponse extends Attachment { +export class AttachmentResponse { id: string; + fileName: string; size: number; sizeName: string; url: string; constructor(o: AttachmentView) { - super(); this.id = o.id; - this.build(o); + this.fileName = o.fileName; this.size = o.size; this.sizeName = o.sizeName; this.url = o.url; diff --git a/src/program.ts b/src/program.ts index 1188c21b62..3da4d35f02 100644 --- a/src/program.ts +++ b/src/program.ts @@ -225,7 +225,7 @@ export class Program { program .command('get ') .description('Get an object.') - .option('--attachmentid ', 'Get an item\'s attachment.') + .option('--itemid ', 'Attachment\'s item id.') .option('--output ', 'Output directory or filename for attachment.') .on('--help', () => { writeLn('\n Objects:'); @@ -236,13 +236,14 @@ export class Program { writeLn(' uri'); writeLn(' totp'); writeLn(' exposed'); + writeLn(' attachment'); writeLn(' folder'); writeLn(' collection'); writeLn(' template'); writeLn(''); writeLn(' Id:'); writeLn(''); - writeLn(' Search term or GUID.'); + writeLn(' Search term or object\'s globally unique `id`.'); writeLn(''); writeLn(' Examples:'); writeLn(''); @@ -250,8 +251,9 @@ export class Program { writeLn(' bw get password https://google.com'); writeLn(' bw get totp google.com'); writeLn(' bw get exposed yahoo.com'); - writeLn(' bw get item google --attachmentid b857igwl1dzrs2 --output ./photo.jpg'); - writeLn(' bw get item google --attachmentid photo.jpg --raw'); + writeLn(' bw get attachment b857igwl1dzrs2 --itemid 99ee88d2-6046-4ea7-92c2-acac464b1412 ' + + '--output ./photo.jpg'); + writeLn(' bw get attachment photo.jpg --itemid 99ee88d2-6046-4ea7-92c2-acac464b1412 --raw'); writeLn(' bw get folder email'); writeLn(' bw get template folder'); writeLn(''); @@ -306,7 +308,7 @@ export class Program { writeLn(''); writeLn(' Id:'); writeLn(''); - writeLn(' Must be a GUID.'); + writeLn(' Must be object\'s globally unique `id`.'); writeLn(''); writeLn(' Notes:'); writeLn(''); @@ -328,24 +330,24 @@ export class Program { program .command('delete ') - .option('--attachmentid ', 'Delete an item\'s attachment.') + .option('--itemid ', 'Attachment\'s item id.') .description('Delete an object.') .on('--help', () => { writeLn('\n Objects:'); writeLn(''); writeLn(' item'); + writeLn(' attachment'); writeLn(' folder'); writeLn(''); writeLn(' Id:'); writeLn(''); - writeLn(' Must be a GUID.'); + writeLn(' Must be object\'s globally unique `id`.'); writeLn(''); writeLn(' Examples:'); writeLn(''); writeLn(' bw delete item 7063feab-4b10-472e-b64c-785e2b870b92'); 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(' bw delete attachment b857igwl1dzrs2 --itemid 310d5ffd-e9a2-4451-af87-ea054dce0f78'); writeLn(''); }) .action(async (object, id, cmd) => {