From 5ecf43c124f38583078ab1b7e796640e557c826c Mon Sep 17 00:00:00 2001 From: Maxim Shishmarev Date: Thu, 22 Oct 2020 17:14:10 +1100 Subject: [PATCH] Fix open group file uploads using incorrect server --- js/modules/loki_app_dot_net_api.d.ts | 6 ++++++ ts/receiver/attachments.ts | 26 +++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/js/modules/loki_app_dot_net_api.d.ts b/js/modules/loki_app_dot_net_api.d.ts index 68d705850..b898ae4ea 100644 --- a/js/modules/loki_app_dot_net_api.d.ts +++ b/js/modules/loki_app_dot_net_api.d.ts @@ -9,6 +9,11 @@ interface UploadResponse { id?: number; } +interface DownloadResponse { + statucCode: number; + reponse: any; +} + export interface LokiAppDotNetServerInterface { findOrCreateChannel( api: LokiPublicChatFactoryAPI, @@ -19,6 +24,7 @@ export interface LokiAppDotNetServerInterface { uploadAvatar(data: FormData): Promise; putAttachment(data: ArrayBuffer): Promise; putAvatar(data: ArrayBuffer): Promise; + downloadAttachment(url: String): Promise; // todo: add return type } export interface LokiPublicChannelAPI { diff --git a/ts/receiver/attachments.ts b/ts/receiver/attachments.ts index c326f5227..26bf845ee 100644 --- a/ts/receiver/attachments.ts +++ b/ts/receiver/attachments.ts @@ -4,7 +4,31 @@ import _ from 'lodash'; import * as Data from '../../js/modules/data'; export async function downloadAttachment(attachment: any) { - const res = await window.lokiFileServerAPI.downloadAttachment(attachment.url); + const serverUrl = new URL(attachment.url).origin; + + // The fileserver adds the `-static` part for some reason + const defaultFileserver = _.includes( + ['https://file-static.lokinet.org', 'https://file.getsession.org'], + serverUrl + ); + + let res: any; + + // TODO: we need attachments to remember which API should be used to retrieve them + if (!defaultFileserver) { + const serverAPI = await window.lokiPublicChatAPI.findOrCreateServer( + serverUrl + ); + + if (serverAPI) { + res = await serverAPI.downloadAttachment(attachment.url); + } + } + + // Fallback to using the default fileserver + if (defaultFileserver || !res) { + res = await window.lokiFileServerAPI.downloadAttachment(attachment.url); + } // The attachment id is actually just the absolute url of the attachment let data = new Uint8Array(res.response.data).buffer;