Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Bug 871835 - [MMS] Re-factor attachments to work with Blob data r=gna…
Browse files Browse the repository at this point in the history
…rf37
  • Loading branch information
jugglinmike authored and gnarf committed May 15, 2013
1 parent a09e770 commit 5413788
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
22 changes: 17 additions & 5 deletions apps/sms/js/attachment.js
Expand Up @@ -19,13 +19,18 @@

'use strict';

function Attachment(type, uri, size) {
this.type = type;
this.uri = uri;
this.size = size;
function Attachment(blob, name) {
this.blob = blob;
this.name = name || '';
}

Attachment.prototype = {
get size() {
return this.blob.size;
},
get type() {
return Utils.typeFromMimeType(this.blob.type);
},
render: function() {
var _ = navigator.mozL10n.get;
var el = document.createElement('iframe');
Expand All @@ -36,12 +41,19 @@ Attachment.prototype = {
// We want kilobytes so we divide by 1024, with one fractional digit
var size = Math.floor(this.size / 102.4) / 10;
var sizeString = _('attachmentSize', {n: size});
var objectURL = window.URL.createObjectURL(this.blob);
src += Utils.Template('attachment-tmpl').interpolate({
uri: this.uri,
uri: objectURL,
size: sizeString
});
el.src = src;
el.className = 'attachment';

// When rendering is complete, signal Gecko to release the reference to the
// Blob
el.addEventListener('load',
window.URL.revokeObjectURL.bind(window.URL, objectURL));

return el;
}
};
6 changes: 1 addition & 5 deletions apps/sms/js/compose.js
Expand Up @@ -279,11 +279,7 @@ var Compose = (function() {

activity.onsuccess = function() {
var result = activity.result;
var blob = result.blob;
var objectURL = window.URL.createObjectURL(blob);
// Parse the attachment type from the MIME type
var type = result.type.split('/')[0];
var attachment = new Attachment(type, objectURL, blob.size);
var attachment = new Attachment(result.blob, result.name);

if (typeof requestProxy.onsuccess === 'function') {
requestProxy.onsuccess(attachment);
Expand Down
12 changes: 8 additions & 4 deletions apps/sms/test/unit/compose_test.js
Expand Up @@ -211,16 +211,20 @@ suite('compose_test.js', function() {
var req = Compose.requestAttachment();
req.onsuccess = function(attachment) {
assert.instanceOf(attachment, Attachment);
assert.equal(attachment.type, 'image');
assert.equal(attachment.size, 0);
assert.match(attachment.uri, /^blob:.+$/);

// TODO: Move these assertions to a higher-level test suite that
// concerns interactions between disparate units.
// See: Bug 868056
assert.equal(attachment.name, activity.result.name);
assert.equal(attachment.blob, activity.result.blob);

done();
};

// Simulate a successful 'pick' MozActivity
var activity = MockMozActivity.instances[0];
activity.result = {
type: 'image/jpeg',
name: 'test',
blob: new Blob()
};
activity.onsuccess();
Expand Down
7 changes: 3 additions & 4 deletions apps/sms/test/unit/mock_attachment.js
@@ -1,9 +1,8 @@
'use strict';

function MockAttachment(type, uri, size) {
this.type = type;
this.uri = uri;
this.size = size;
function MockAttachment(blob, name = '') {
this.blob = blob;
this.name = name;
}

MockAttachment.prototype = {
Expand Down

0 comments on commit 5413788

Please sign in to comment.