Merge pull request #1612 from Bilb/fix-restore-avatar-link

fix avatar download on restore when linking device
pull/1616/head
Audric Ackermann 4 years ago committed by GitHub
commit 3a534a056c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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<ArrayBuffer | null> => {
if (!fileId) {
window.log.warn('');
export const downloadFileFromFSv2 = async (
fileIdOrCompleteUrl: string
): Promise<ArrayBuffer | null> => {
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}`,

@ -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);

Loading…
Cancel
Save