From 84c7a4c29321d80a5b1545458d3e3f84a337808e Mon Sep 17 00:00:00 2001
From: Scott Nonnenberg <scott@signal.org>
Date: Thu, 26 Apr 2018 12:01:31 -0700
Subject: [PATCH] Move to some of our global utility methods

---
 js/models/conversations.js  | 27 ++++++++++++++-------------
 js/views/file_input_view.js | 24 +++++++++++-------------
 2 files changed, 25 insertions(+), 26 deletions(-)

diff --git a/js/models/conversations.js b/js/models/conversations.js
index aea404a6f..b714a5363 100644
--- a/js/models/conversations.js
+++ b/js/models/conversations.js
@@ -628,7 +628,10 @@
     async makeThumbnailAttachment(attachment) {
       const attachmentWithData = await loadAttachmentData(attachment);
       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)
         ? await Whisper.FileInputView.makeImageThumbnail(128, objectUrl)
@@ -638,7 +641,10 @@
 
       const arrayBuffer = await this.blobToArrayBuffer(thumbnail);
       const finalContentType = 'image/png';
-      const finalObjectUrl = this.makeObjectUrl(arrayBuffer, finalContentType);
+      const finalObjectUrl = Signal.Util.arrayBufferToObjectURL({
+        data: arrayBuffer,
+        type: finalContentType,
+      });
 
       return {
         data: arrayBuffer,
@@ -1126,12 +1132,6 @@
     forceRender(message) {
       message.trigger('change', message);
     },
-    makeObjectUrl(data, contentType) {
-      const blob = new Blob([data], {
-        type: contentType,
-      });
-      return URL.createObjectURL(blob);
-    },
     makeMessagesLookup(messages) {
       return messages.reduce((acc, message) => {
         const { source, sent_at: sentAt } = message.attributes;
@@ -1189,7 +1189,7 @@
       } catch (error) {
         console.log(
           'Problem loading attachment data for quoted message from database',
-          error && error.stack ? error.stack : error
+          Signal.Types.Errors.toLogFormat(error)
         );
         return false;
       }
@@ -1244,10 +1244,11 @@
       }
       try {
         const thumbnailWithData = await loadAttachmentData(thumbnail);
-        thumbnailWithData.objectUrl = this.makeObjectUrl(
-          thumbnailWithData.data,
-          thumbnailWithData.contentType
-        );
+        const { data, contentType } = thumbnailWithData;
+        thumbnailWithData.objectUrl = Signal.Util.arrayBufferToObjectURL({
+          data,
+          type: contentType,
+        });
 
         // If we update this data in place, there's the risk that this data could be
         //   saved back to the database
diff --git a/js/views/file_input_view.js b/js/views/file_input_view.js
index 190d168cf..fd8cb1b3f 100644
--- a/js/views/file_input_view.js
+++ b/js/views/file_input_view.js
@@ -84,7 +84,7 @@
       video.addEventListener('error', (error) => {
         console.log(
           'makeVideoThumbnail error',
-          error && error.stack ? error.stack : error
+          Signal.Types.Errors.toLogFormat(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) {
     return new Promise((resolve, reject) => {
       const fileReader = new FileReader();
@@ -114,8 +107,11 @@
 
   async function makeVideoThumbnail(size, videoObjectUrl) {
     const blob = await makeVideoScreenshot(videoObjectUrl);
-    const arrayBuffer = await blobToArrayBuffer(blob);
-    const screenshotObjectUrl = makeObjectUrl(arrayBuffer, 'image/png');
+    const data = await blobToArrayBuffer(blob);
+    const screenshotObjectUrl = Signal.Util.arrayBufferToObjectURL({
+      data,
+      type: 'image/png',
+    });
 
     const thumbnail = await makeImageThumbnail(size, screenshotObjectUrl);
     URL.revokeObjectURL(screenshotObjectUrl);
@@ -244,9 +240,11 @@
         const thumbnail = await makeVideoScreenshot(this.previewObjectUrl);
         URL.revokeObjectURL(this.previewObjectUrl);
 
-        const arrayBuffer = await blobToArrayBuffer(thumbnail);
-
-        this.previewObjectUrl = makeObjectUrl(arrayBuffer, 'image/png');
+        const data = await blobToArrayBuffer(thumbnail);
+        this.previewObjectUrl = Signal.Util.arrayBufferToObjectURL({
+          data,
+          type: 'image/png',
+        });
         this.addThumb(this.previewObjectUrl, { addPlayIcon: true });
       };