Replace Backbone `saveFile` with `Attachment.save`

This commit is contained in:
Daniel Gasienica 2018-04-25 13:29:34 -04:00
parent 9a76d4bcf8
commit 41fe50553f
4 changed files with 45 additions and 80 deletions

View File

@ -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

View File

@ -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 = $('<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()) {

View File

@ -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');

View File

@ -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');
});
});