From 531fc5c7ffcdbfbcbfc9c21e3fdffe635906ed33 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Mon, 10 May 2021 11:23:06 +1000 Subject: [PATCH] fix avatar download on restore when linking device Fixes #1601 --- ts/fileserver/FileServerApiV2.ts | 16 ++++++++++++---- ts/receiver/attachments.ts | 12 +++++++----- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/ts/fileserver/FileServerApiV2.ts b/ts/fileserver/FileServerApiV2.ts index 7d752e199..0cbf973c6 100644 --- a/ts/fileserver/FileServerApiV2.ts +++ b/ts/fileserver/FileServerApiV2.ts @@ -63,14 +63,22 @@ export const uploadFileToFsV2 = async ( /** * Download a file given the fileId from the fileserver v2 - * @param fileId the fileId to download + * @param fileIdOrCompleteUrl the fileId to download or the completeUrl to the fileitself * @returns the data as an Uint8Array or null */ -export const downloadFileFromFSv2 = async (fileId: string): Promise => { - if (!fileId) { - window.log.warn(''); +export const downloadFileFromFSv2 = async ( + fileIdOrCompleteUrl: string +): Promise => { + let fileId = fileIdOrCompleteUrl; + if (!fileIdOrCompleteUrl) { + window.log.warn('Empty url to download for file v2'); return null; } + + const completeUrlPrefix = `${fileServerV2URL}/${FILES_ENDPOINT}/`; + if (fileIdOrCompleteUrl.startsWith(completeUrlPrefix)) { + fileId = fileId.substr(completeUrlPrefix.length); + } const request: FileServerV2Request = { method: 'GET', endpoint: `${FILES_ENDPOINT}/${fileId}`, diff --git a/ts/receiver/attachments.ts b/ts/receiver/attachments.ts index 7430f0f64..36e4a1988 100644 --- a/ts/receiver/attachments.ts +++ b/ts/receiver/attachments.ts @@ -14,7 +14,8 @@ import { FSv2 } from '../fileserver'; import { getUnpaddedAttachment } from '../session/crypto/BufferPadding'; export async function downloadAttachment(attachment: any) { - const serverUrl = new URL(attachment.url).origin; + const asURL = new URL(attachment.url); + const serverUrl = asURL.origin; // The fileserver adds the `-static` part for some reason const defaultFileserver = _.includes( @@ -27,12 +28,13 @@ export async function downloadAttachment(attachment: any) { let res: ArrayBuffer | null = null; if (defaultFsV2) { - if (!attachment.id) { - window.log.warn('Cannot download fsv2 file with empty id'); - return; + let attachmentId = attachment.id; + if (!attachmentId) { + // try to get the fileId from the end of the URL + attachmentId = attachment.url; } window.log.info('Download v2 file server attachment'); - res = await FSv2.downloadFileFromFSv2(attachment.id); + res = await FSv2.downloadFileFromFSv2(attachmentId); } else if (!defaultFileserver) { // TODO: we need attachments to remember which API should be used to retrieve them const serverAPI = await window.lokiPublicChatAPI.findOrCreateServer(serverUrl);