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/message_view.js
!js/views/settings_view.js !js/views/settings_view.js
!test/backup_test.js !test/backup_test.js
!test/views/attachment_view_test.js
!libtextsecure/message_receiver.js !libtextsecure/message_receiver.js
!main.js !main.js
!preload.js !preload.js

View file

@ -2,7 +2,6 @@
/* global _: false */ /* global _: false */
/* global Backbone: false */ /* global Backbone: false */
/* global filesize: false */ /* global filesize: false */
/* global moment: false */
/* global i18n: false */ /* global i18n: false */
/* global Signal: false */ /* global Signal: false */
@ -103,12 +102,6 @@
this.remove(); this.remove();
}, },
getFileType() {
switch (this.model.contentType) {
case 'video/quicktime': return 'mov';
default: return this.model.contentType.split('/')[1];
}
},
onClick() { onClick() {
if (!this.isImage()) { if (!this.isImage()) {
this.saveFile(); this.saveFile();
@ -182,26 +175,11 @@
return i18n('unnamedFile'); 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() { saveFile() {
const url = window.URL.createObjectURL(this.blob, { type: 'octet/stream' }); Signal.Types.AttachmentTS.save({
const a = $('<a>').attr({ href: url, download: this.suggestedName() }); attachment: this.model,
a[0].click(); timestamp: this.timestamp,
window.URL.revokeObjectURL(url); });
}, },
render() { render() {
if (!this.isImage()) { if (!this.isImage()) {

View file

@ -201,6 +201,7 @@ window.Signal.Startup = require('./js/modules/startup');
window.Signal.Types = {}; window.Signal.Types = {};
window.Signal.Types.Attachment = Attachment; window.Signal.Types.Attachment = Attachment;
window.Signal.Types.AttachmentTS = require('./ts/types/Attachment');
window.Signal.Types.Conversation = require('./ts/types/Conversation'); window.Signal.Types.Conversation = require('./ts/types/Conversation');
window.Signal.Types.Errors = require('./js/modules/types/errors'); 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() { /* global Whisper: false */
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");
});
it('should display a filename', function() { 'use strict';
var epoch = new Date((new Date(0)).getTimezoneOffset() * 60 * 1000);
var attachment = { contentType: 'image/png', data: 'grumpy cat' }; describe('AttachmentView', () => {
var result = new Whisper.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, model: attachment,
timestamp: epoch timestamp: now,
}).suggestedName(); }).render();
assert.equal(view.el.firstChild.tagName, 'IMG');
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');
}); });
}); });