From f0c198c7b74bf453515bd1e047910c60a7139943 Mon Sep 17 00:00:00 2001 From: Beaudan Brown Date: Mon, 21 Oct 2019 12:32:59 +1100 Subject: [PATCH] Split uploadData to public and private --- background.html | 2 +- js/modules/loki_app_dot_net_api.js | 40 +++++++++++++++++++++++++++++- js/modules/loki_file_server_api.js | 24 ++---------------- js/modules/web_api.js | 2 +- libtextsecure/message_receiver.js | 30 +++++++++++----------- 5 files changed, 59 insertions(+), 39 deletions(-) diff --git a/background.html b/background.html index 04040201f..d68fbb83a 100644 --- a/background.html +++ b/background.html @@ -138,7 +138,7 @@
- +
diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index eb113d860..17d40e285 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -1,8 +1,9 @@ /* global log, textsecure, libloki, Signal, Whisper, Headers, ConversationController, -clearTimeout, MessageController, libsignal, StringView, window, _, dcodeIO */ +clearTimeout, MessageController, libsignal, StringView, window, _, dcodeIO, Buffer */ const EventEmitter = require('events'); const nodeFetch = require('node-fetch'); const { URL, URLSearchParams } = require('url'); +const FormData = require('form-data'); // Can't be less than 1200 if we have unauth'd requests const PUBLICCHAT_MSG_POLL_EVERY = 1.5 * 1000; // 1.5s @@ -344,6 +345,43 @@ class LokiAppDotNetServerAPI { return false; } + + async uploadData(data) { + const endpoint = 'files'; + const options = { + method: 'POST', + rawBody: data, + }; + + const { statusCode, response } = await this.serverRequest( + endpoint, + options + ); + if (statusCode !== 200) { + log.warn('Failed to upload data to fileserver'); + return null; + } + + const url = response.data && response.data.url; + const id = response.data && response.data.id; + return { + url, + id, + }; + } + + putAttachment(attachmentBin) { + const formData = new FormData(); + const buffer = Buffer.from(attachmentBin); + formData.append('type', 'network.loki'); + formData.append('content', buffer, { + contentType: 'application/octet-stream', + name: 'content', + filename: 'attachment', + }); + + return this.uploadData(formData); + } } class LokiPublicChannelAPI { diff --git a/js/modules/loki_file_server_api.js b/js/modules/loki_file_server_api.js index b70984ccc..06e062aa5 100644 --- a/js/modules/loki_file_server_api.js +++ b/js/modules/loki_file_server_api.js @@ -38,28 +38,8 @@ class LokiFileServerAPI { ); } - async uploadData(data) { - const endpoint = 'files'; - const options = { - method: 'POST', - rawBody: data, - }; - - const { statusCode, response } = await this._server.serverRequest( - endpoint, - options - ); - if (statusCode !== 200) { - log.warn('Failed to upload data to fileserver'); - return null; - } - - const url = response.data && response.data.url; - const id = response.data && response.data.id; - return { - url, - id, - }; + uploadPrivateAttachment(data) { + return this._server.uploadData(data); } } diff --git a/js/modules/web_api.js b/js/modules/web_api.js index 92bdf937b..7de88a727 100644 --- a/js/modules/web_api.js +++ b/js/modules/web_api.js @@ -864,7 +864,7 @@ function initialize({ filename: 'attachment', }); - return lokiFileServerAPI.uploadData(formData); + return lokiFileServerAPI.uploadPrivateAttachment(formData); } // eslint-disable-next-line no-shadow diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 4a987d6b5..ea2539c8c 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -1438,21 +1438,23 @@ MessageReceiver.prototype.extend({ }, async downloadAttachment(attachment) { // The attachment id is actually just the absolute url of the attachment - const encrypted = await this.server.getAttachment(attachment.url); - const { key, digest, size } = attachment; - - const data = await textsecure.crypto.decryptAttachment( - encrypted, - window.Signal.Crypto.base64ToArrayBuffer(key), - window.Signal.Crypto.base64ToArrayBuffer(digest) - ); - - if (!size || size !== data.byteLength) { - throw new Error( - `downloadAttachment: Size ${size} did not match downloaded attachment size ${ - data.byteLength - }` + let data = await this.server.getAttachment(attachment.url); + if (!attachment.isRaw) { + const { key, digest, size } = attachment; + + data = await textsecure.crypto.decryptAttachment( + data, + window.Signal.Crypto.base64ToArrayBuffer(key), + window.Signal.Crypto.base64ToArrayBuffer(digest) ); + + if (!size || size !== data.byteLength) { + throw new Error( + `downloadAttachment: Size ${size} did not match downloaded attachment size ${ + data.byteLength + }` + ); + } } return {