From 41fe50553f7d64b1abe8b7ddc155053be53f256b Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Wed, 25 Apr 2018 13:29:34 -0400 Subject: [PATCH] Replace Backbone `saveFile` with `Attachment.save` --- .eslintignore | 1 + js/views/attachment_view.js | 30 ++-------- preload.js | 1 + test/views/attachment_view_test.js | 93 +++++++++++++----------------- 4 files changed, 45 insertions(+), 80 deletions(-) diff --git a/.eslintignore b/.eslintignore index 898e6ddb6..79b6e678a 100644 --- a/.eslintignore +++ b/.eslintignore @@ -33,6 +33,7 @@ ts/**/*.js !js/views/message_view.js !js/views/settings_view.js !test/backup_test.js +!test/views/attachment_view_test.js !libtextsecure/message_receiver.js !main.js !preload.js diff --git a/js/views/attachment_view.js b/js/views/attachment_view.js index 209140159..c19e4ebf4 100644 --- a/js/views/attachment_view.js +++ b/js/views/attachment_view.js @@ -2,7 +2,6 @@ /* global _: false */ /* global Backbone: false */ /* global filesize: false */ -/* global moment: false */ /* global i18n: false */ /* global Signal: false */ @@ -103,12 +102,6 @@ this.remove(); }, - getFileType() { - switch (this.model.contentType) { - case 'video/quicktime': return 'mov'; - default: return this.model.contentType.split('/')[1]; - } - }, onClick() { if (!this.isImage()) { this.saveFile(); @@ -182,26 +175,11 @@ return i18n('unnamedFile'); }, - suggestedName() { - if (this.model.fileName) { - return this.model.fileName; - } - - let suggestion = 'signal'; - if (this.timestamp) { - suggestion += moment(this.timestamp).format('-YYYY-MM-DD-HHmmss'); - } - const fileType = this.getFileType(); - if (fileType) { - suggestion += `.${fileType}`; - } - return suggestion; - }, saveFile() { - const url = window.URL.createObjectURL(this.blob, { type: 'octet/stream' }); - const a = $('').attr({ href: url, download: this.suggestedName() }); - a[0].click(); - window.URL.revokeObjectURL(url); + Signal.Types.AttachmentTS.save({ + attachment: this.model, + timestamp: this.timestamp, + }); }, render() { if (!this.isImage()) { diff --git a/preload.js b/preload.js index ad3a0687a..9316fe778 100644 --- a/preload.js +++ b/preload.js @@ -201,6 +201,7 @@ window.Signal.Startup = require('./js/modules/startup'); window.Signal.Types = {}; window.Signal.Types.Attachment = Attachment; +window.Signal.Types.AttachmentTS = require('./ts/types/Attachment'); window.Signal.Types.Conversation = require('./ts/types/Conversation'); window.Signal.Types.Errors = require('./js/modules/types/errors'); diff --git a/test/views/attachment_view_test.js b/test/views/attachment_view_test.js index 68aaf9267..afbd4c99a 100644 --- a/test/views/attachment_view_test.js +++ b/test/views/attachment_view_test.js @@ -1,59 +1,44 @@ -describe('AttachmentView', function() { +/* global assert: false */ - describe('with arbitrary files', function() { - it('should render a file view', function() { - var attachment = { - contentType: 'unused', - size: 1232 - }; - var view = new Whisper.AttachmentView({model: attachment}).render(); - assert.match(view.el.innerHTML, /fileView/); - }); - it('should display the filename if present', function() { - var attachment = { - fileName: 'foo.txt', - contentType: 'unused', - size: 1232, - }; - var view = new Whisper.AttachmentView({model: attachment}).render(); - assert.match(view.el.innerHTML, /foo.txt/); - }); - it('should render a file size', function() { - var attachment = { - size: 1232, - contentType: 'unused' - }; - var view = new Whisper.AttachmentView({model: attachment}).render(); - assert.match(view.el.innerHTML, /1.2 KB/); - }); - }); - it('should render an image for images', function() { - var now = new Date().getTime(); - var attachment = { contentType: 'image/png', data: 'grumpy cat' }; - var view = new Whisper.AttachmentView({model: attachment, timestamp: now}).render(); - assert.equal(view.el.firstChild.tagName, "IMG"); - }); +/* global Whisper: false */ - it('should display a filename', function() { - var epoch = new Date((new Date(0)).getTimezoneOffset() * 60 * 1000); - var attachment = { contentType: 'image/png', data: 'grumpy cat' }; - var result = new Whisper.AttachmentView({ +'use strict'; + +describe('AttachmentView', () => { + describe('with arbitrary files', () => { + it('should render a file view', () => { + const attachment = { + contentType: 'unused', + size: 1232, + }; + const view = new Whisper.AttachmentView({ model: attachment }).render(); + assert.match(view.el.innerHTML, /fileView/); + }); + it('should display the filename if present', () => { + const attachment = { + fileName: 'foo.txt', + contentType: 'unused', + size: 1232, + }; + const view = new Whisper.AttachmentView({ model: attachment }).render(); + assert.match(view.el.innerHTML, /foo.txt/); + }); + it('should render a file size', () => { + const attachment = { + size: 1232, + contentType: 'unused', + }; + const view = new Whisper.AttachmentView({ model: attachment }).render(); + assert.match(view.el.innerHTML, /1.2 KB/); + }); + }); + it('should render an image for images', () => { + const now = new Date().getTime(); + const attachment = { contentType: 'image/png', data: 'grumpy cat' }; + const view = new Whisper.AttachmentView({ model: attachment, - timestamp: epoch - }).suggestedName(); - - var expected = '1970-01-01-000000'; - assert(result === 'signal-' + expected + '.png'); - }); - it('should auto-generate a filename', function() { - var epoch = new Date((new Date(0)).getTimezoneOffset() * 60 * 1000); - var attachment = { contentType: 'image/png', data: 'grumpy cat' }; - var result = new Whisper.AttachmentView({ - model: attachment, - timestamp: epoch - }).suggestedName(); - - var expected = '1970-01-01-000000'; - assert(result === 'signal-' + expected + '.png'); + timestamp: now, + }).render(); + assert.equal(view.el.firstChild.tagName, 'IMG'); }); });