handle image/ avatar paths

pull/934/head
Ryan Tharp 6 years ago
parent 46d401ecbf
commit c36fd8ae62

@ -5,6 +5,7 @@ const nodeFetch = require('node-fetch');
const { URL, URLSearchParams } = require('url'); const { URL, URLSearchParams } = require('url');
const FormData = require('form-data'); const FormData = require('form-data');
const https = require('https'); const https = require('https');
const path = require('path');
// Can't be less than 1200 if we have unauth'd requests // Can't be less than 1200 if we have unauth'd requests
const PUBLICCHAT_MSG_POLL_EVERY = 1.5 * 1000; // 1.5s const PUBLICCHAT_MSG_POLL_EVERY = 1.5 * 1000; // 1.5s
@ -1254,38 +1255,50 @@ class LokiPublicChannelAPI {
this.conversation.setGroupName(note.value.name); this.conversation.setGroupName(note.value.name);
} }
if (note.value && note.value.avatar) { if (note.value && note.value.avatar) {
const avatarAbsUrl = this.serverAPI.baseServerUrl + note.value.avatar; if (note.value.avatar.match(/^images\//)) {
const { // local file avatar
writeNewAttachmentData, const resolvedAvatar = path.normalize(note.value.avatar);
deleteAttachmentData, const base = path.normalize('images/');
} = window.Signal.Migrations; const re = new RegExp(`^${base}`)
// do we already have this image? no, then // do we at least ends up inside images/ somewhere?
if (re.test(resolvedAvatar)) {
// download a copy and save it this.conversation.set('avatar', resolvedAvatar);
const imageData = await nodeFetch(avatarAbsUrl);
// eslint-disable-next-line no-inner-declarations
function toArrayBuffer(buf) {
const ab = new ArrayBuffer(buf.length);
const view = new Uint8Array(ab);
// eslint-disable-next-line no-plusplus
for (let i = 0; i < buf.length; i++) {
view[i] = buf[i];
} }
return ab; } else {
} // relative URL avatar
// eslint-enable-next-line no-inner-declarations const avatarAbsUrl = this.serverAPI.baseServerUrl + note.value.avatar;
const {
const buffer = await imageData.buffer();
const newAttributes = await window.Signal.Types.Conversation.maybeUpdateAvatar(
this.conversation.attributes,
toArrayBuffer(buffer),
{
writeNewAttachmentData, writeNewAttachmentData,
deleteAttachmentData, deleteAttachmentData,
} = window.Signal.Migrations;
// do we already have this image? no, then
// download a copy and save it
const imageData = await nodeFetch(avatarAbsUrl);
// eslint-disable-next-line no-inner-declarations
function toArrayBuffer(buf) {
const ab = new ArrayBuffer(buf.length);
const view = new Uint8Array(ab);
// eslint-disable-next-line no-plusplus
for (let i = 0; i < buf.length; i++) {
view[i] = buf[i];
}
return ab;
} }
); // eslint-enable-next-line no-inner-declarations
// update group
this.conversation.set('avatar', newAttributes.avatar); const buffer = await imageData.buffer();
const newAttributes = await window.Signal.Types.Conversation.maybeUpdateAvatar(
this.conversation.attributes,
toArrayBuffer(buffer),
{
writeNewAttachmentData,
deleteAttachmentData,
}
);
// update group
this.conversation.set('avatar', newAttributes.avatar);
}
} }
// is it mutable? // is it mutable?
// who are the moderators? // who are the moderators?
@ -1420,7 +1433,7 @@ class LokiPublicChannelAPI {
} }
if (quote) { if (quote) {
// TODO: Enable quote attachments again using proper ADN style // Disable quote attachments
quote.attachments = []; quote.attachments = [];
} }

Loading…
Cancel
Save