Use avatar specific endpoint

pull/610/head
Maxim Shishmarev 6 years ago
parent 4dd314c18f
commit 5a08ac9cc1

@ -835,8 +835,8 @@
const readFile = attachment => const readFile = attachment =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
const FR = new FileReader(); const fileReader = new FileReader();
FR.onload = e => { fileReader.onload = e => {
const data = e.target.result; const data = e.target.result;
resolve({ resolve({
...attachment, ...attachment,
@ -844,9 +844,9 @@
size: data.byteLength, size: data.byteLength,
}); });
}; };
FR.onerror = reject; fileReader.onerror = reject;
FR.onabort = reject; fileReader.onabort = reject;
FR.readAsArrayBuffer(attachment.file); fileReader.readAsArrayBuffer(attachment.file);
}); });
const avatarPath = conversation.getAvatarPath(); const avatarPath = conversation.getAvatarPath();

@ -446,6 +446,33 @@ class LokiAppDotNetServerAPI {
return false; return false;
} }
async uploadAvatar(data) {
const endpoint = 'users/me/avatar';
const options = {
method: 'POST',
rawBody: data,
};
const { statusCode, response } = await this.serverRequest(
endpoint,
options
);
if (statusCode !== 200) {
log.warn('Failed to upload avatar to fileserver');
return null;
}
const url =
response.data &&
response.data.avatar_image &&
response.data.avatar_image.url;
return {
url,
};
}
async uploadData(data) { async uploadData(data) {
const endpoint = 'files'; const endpoint = 'files';
const options = { const options = {

@ -217,6 +217,10 @@ class LokiFileServerAPI {
); );
} }
uploadAvatar(data) {
return this._server.uploadAvatar(data);
}
uploadPrivateAttachment(data) { uploadPrivateAttachment(data) {
return this._server.uploadData(data); return this._server.uploadData(data);
} }

@ -463,6 +463,7 @@ function initialize({
getSenderCertificate, getSenderCertificate,
makeProxiedRequest, makeProxiedRequest,
putAttachment, putAttachment,
putAvatar,
registerKeys, registerKeys,
registerSupportForUnauthenticatedDelivery, registerSupportForUnauthenticatedDelivery,
removeSignalingKey, removeSignalingKey,
@ -854,9 +855,9 @@ function initialize({
}); });
} }
function putAttachment(encryptedBin) { function putAttachment(maybeEncryptedBin) {
const formData = new FormData(); const formData = new FormData();
const buffer = Buffer.from(encryptedBin); const buffer = Buffer.from(maybeEncryptedBin);
formData.append('type', 'network.loki'); formData.append('type', 'network.loki');
formData.append('content', buffer, { formData.append('content', buffer, {
contentType: 'application/octet-stream', contentType: 'application/octet-stream',
@ -867,6 +868,17 @@ function initialize({
return lokiFileServerAPI.uploadPrivateAttachment(formData); return lokiFileServerAPI.uploadPrivateAttachment(formData);
} }
function putAvatar(bin) {
const formData = new FormData();
const buffer = Buffer.from(bin);
formData.append('avatar', buffer, {
contentType: 'application/octet-stream',
name: 'avatar',
filename: 'attachment',
});
return lokiFileServerAPI.uploadAvatar(formData);
}
// eslint-disable-next-line no-shadow // eslint-disable-next-line no-shadow
async function getProxiedSize(url) { async function getProxiedSize(url) {
const result = await _outerAjax(url, { const result = await _outerAjax(url, {

@ -177,7 +177,12 @@ MessageSender.prototype = {
constructor: MessageSender, constructor: MessageSender,
// makeAttachmentPointer :: Attachment -> Promise AttachmentPointerProto // makeAttachmentPointer :: Attachment -> Promise AttachmentPointerProto
async makeAttachmentPointer(attachment, publicServer = null, isRaw = false) { async makeAttachmentPointer(
attachment,
publicServer = null,
isRaw = false,
isAvatar = false
) {
if (typeof attachment !== 'object' || attachment == null) { if (typeof attachment !== 'object' || attachment == null) {
return Promise.resolve(undefined); return Promise.resolve(undefined);
} }
@ -217,7 +222,10 @@ MessageSender.prototype = {
attachmentData = result.ciphertext; attachmentData = result.ciphertext;
} }
const result = await server.putAttachment(attachmentData); const result = isAvatar
? await server.putAvatar(attachmentData)
: await server.putAttachment(attachmentData);
if (!result) { if (!result) {
return Promise.reject( return Promise.reject(
new Error('Failed to upload data to attachment fileserver') new Error('Failed to upload data to attachment fileserver')
@ -553,7 +561,7 @@ MessageSender.prototype = {
}, },
uploadAvatar(attachment) { uploadAvatar(attachment) {
return this.makeAttachmentPointer(attachment, null, true); return this.makeAttachmentPointer(attachment, null, true, true);
}, },
sendRequestConfigurationSyncMessage(options) { sendRequestConfigurationSyncMessage(options) {

Loading…
Cancel
Save