Move to some of our global utility methods

This commit is contained in:
Scott Nonnenberg 2018-04-26 12:01:31 -07:00
parent 403fb1fd60
commit 84c7a4c293
2 changed files with 25 additions and 26 deletions

View file

@ -628,7 +628,10 @@
async makeThumbnailAttachment(attachment) { async makeThumbnailAttachment(attachment) {
const attachmentWithData = await loadAttachmentData(attachment); const attachmentWithData = await loadAttachmentData(attachment);
const { data, contentType } = attachmentWithData; const { data, contentType } = attachmentWithData;
const objectUrl = this.makeObjectUrl(data, contentType); const objectUrl = Signal.Util.arrayBufferToObjectURL({
data,
type: contentType,
});
const thumbnail = Signal.Util.GoogleChrome.isImageTypeSupported(contentType) const thumbnail = Signal.Util.GoogleChrome.isImageTypeSupported(contentType)
? await Whisper.FileInputView.makeImageThumbnail(128, objectUrl) ? await Whisper.FileInputView.makeImageThumbnail(128, objectUrl)
@ -638,7 +641,10 @@
const arrayBuffer = await this.blobToArrayBuffer(thumbnail); const arrayBuffer = await this.blobToArrayBuffer(thumbnail);
const finalContentType = 'image/png'; const finalContentType = 'image/png';
const finalObjectUrl = this.makeObjectUrl(arrayBuffer, finalContentType); const finalObjectUrl = Signal.Util.arrayBufferToObjectURL({
data: arrayBuffer,
type: finalContentType,
});
return { return {
data: arrayBuffer, data: arrayBuffer,
@ -1126,12 +1132,6 @@
forceRender(message) { forceRender(message) {
message.trigger('change', message); message.trigger('change', message);
}, },
makeObjectUrl(data, contentType) {
const blob = new Blob([data], {
type: contentType,
});
return URL.createObjectURL(blob);
},
makeMessagesLookup(messages) { makeMessagesLookup(messages) {
return messages.reduce((acc, message) => { return messages.reduce((acc, message) => {
const { source, sent_at: sentAt } = message.attributes; const { source, sent_at: sentAt } = message.attributes;
@ -1189,7 +1189,7 @@
} catch (error) { } catch (error) {
console.log( console.log(
'Problem loading attachment data for quoted message from database', 'Problem loading attachment data for quoted message from database',
error && error.stack ? error.stack : error Signal.Types.Errors.toLogFormat(error)
); );
return false; return false;
} }
@ -1244,10 +1244,11 @@
} }
try { try {
const thumbnailWithData = await loadAttachmentData(thumbnail); const thumbnailWithData = await loadAttachmentData(thumbnail);
thumbnailWithData.objectUrl = this.makeObjectUrl( const { data, contentType } = thumbnailWithData;
thumbnailWithData.data, thumbnailWithData.objectUrl = Signal.Util.arrayBufferToObjectURL({
thumbnailWithData.contentType data,
); type: contentType,
});
// If we update this data in place, there's the risk that this data could be // If we update this data in place, there's the risk that this data could be
// saved back to the database // saved back to the database

View file

@ -84,7 +84,7 @@
video.addEventListener('error', (error) => { video.addEventListener('error', (error) => {
console.log( console.log(
'makeVideoThumbnail error', 'makeVideoThumbnail error',
error && error.stack ? error.stack : error Signal.Types.Errors.toLogFormat(error)
); );
reject(error); reject(error);
}); });
@ -93,13 +93,6 @@
})); }));
} }
function makeObjectUrl(data, contentType) {
const blob = new Blob([data], {
type: contentType,
});
return URL.createObjectURL(blob);
}
function blobToArrayBuffer(blob) { function blobToArrayBuffer(blob) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const fileReader = new FileReader(); const fileReader = new FileReader();
@ -114,8 +107,11 @@
async function makeVideoThumbnail(size, videoObjectUrl) { async function makeVideoThumbnail(size, videoObjectUrl) {
const blob = await makeVideoScreenshot(videoObjectUrl); const blob = await makeVideoScreenshot(videoObjectUrl);
const arrayBuffer = await blobToArrayBuffer(blob); const data = await blobToArrayBuffer(blob);
const screenshotObjectUrl = makeObjectUrl(arrayBuffer, 'image/png'); const screenshotObjectUrl = Signal.Util.arrayBufferToObjectURL({
data,
type: 'image/png',
});
const thumbnail = await makeImageThumbnail(size, screenshotObjectUrl); const thumbnail = await makeImageThumbnail(size, screenshotObjectUrl);
URL.revokeObjectURL(screenshotObjectUrl); URL.revokeObjectURL(screenshotObjectUrl);
@ -244,9 +240,11 @@
const thumbnail = await makeVideoScreenshot(this.previewObjectUrl); const thumbnail = await makeVideoScreenshot(this.previewObjectUrl);
URL.revokeObjectURL(this.previewObjectUrl); URL.revokeObjectURL(this.previewObjectUrl);
const arrayBuffer = await blobToArrayBuffer(thumbnail); const data = await blobToArrayBuffer(thumbnail);
this.previewObjectUrl = Signal.Util.arrayBufferToObjectURL({
this.previewObjectUrl = makeObjectUrl(arrayBuffer, 'image/png'); data,
type: 'image/png',
});
this.addThumb(this.previewObjectUrl, { addPlayIcon: true }); this.addThumb(this.previewObjectUrl, { addPlayIcon: true });
}; };