fix avatar download on restore when linking device

Fixes  #1601
pull/1612/head
Audric Ackermann 4 years ago
parent 77f80c4bc1
commit 531fc5c7ff
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -63,14 +63,22 @@ export const uploadFileToFsV2 = async (
/** /**
* Download a file given the fileId from the fileserver v2 * 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 * @returns the data as an Uint8Array or null
*/ */
export const downloadFileFromFSv2 = async (fileId: string): Promise<ArrayBuffer | null> => { export const downloadFileFromFSv2 = async (
if (!fileId) { fileIdOrCompleteUrl: string
window.log.warn(''); ): Promise<ArrayBuffer | null> => {
let fileId = fileIdOrCompleteUrl;
if (!fileIdOrCompleteUrl) {
window.log.warn('Empty url to download for file v2');
return null; return null;
} }
const completeUrlPrefix = `${fileServerV2URL}/${FILES_ENDPOINT}/`;
if (fileIdOrCompleteUrl.startsWith(completeUrlPrefix)) {
fileId = fileId.substr(completeUrlPrefix.length);
}
const request: FileServerV2Request = { const request: FileServerV2Request = {
method: 'GET', method: 'GET',
endpoint: `${FILES_ENDPOINT}/${fileId}`, endpoint: `${FILES_ENDPOINT}/${fileId}`,

@ -14,7 +14,8 @@ import { FSv2 } from '../fileserver';
import { getUnpaddedAttachment } from '../session/crypto/BufferPadding'; import { getUnpaddedAttachment } from '../session/crypto/BufferPadding';
export async function downloadAttachment(attachment: any) { 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 // The fileserver adds the `-static` part for some reason
const defaultFileserver = _.includes( const defaultFileserver = _.includes(
@ -27,12 +28,13 @@ export async function downloadAttachment(attachment: any) {
let res: ArrayBuffer | null = null; let res: ArrayBuffer | null = null;
if (defaultFsV2) { if (defaultFsV2) {
if (!attachment.id) { let attachmentId = attachment.id;
window.log.warn('Cannot download fsv2 file with empty id'); if (!attachmentId) {
return; // try to get the fileId from the end of the URL
attachmentId = attachment.url;
} }
window.log.info('Download v2 file server attachment'); window.log.info('Download v2 file server attachment');
res = await FSv2.downloadFileFromFSv2(attachment.id); res = await FSv2.downloadFileFromFSv2(attachmentId);
} else if (!defaultFileserver) { } else if (!defaultFileserver) {
// TODO: we need attachments to remember which API should be used to retrieve them // TODO: we need attachments to remember which API should be used to retrieve them
const serverAPI = await window.lokiPublicChatAPI.findOrCreateServer(serverUrl); const serverAPI = await window.lokiPublicChatAPI.findOrCreateServer(serverUrl);

Loading…
Cancel
Save