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