Use avatar specific endpoint

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

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

@ -446,6 +446,33 @@ class LokiAppDotNetServerAPI {
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) {
const endpoint = 'files';
const options = {

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

@ -463,6 +463,7 @@ function initialize({
getSenderCertificate,
makeProxiedRequest,
putAttachment,
putAvatar,
registerKeys,
registerSupportForUnauthenticatedDelivery,
removeSignalingKey,
@ -854,9 +855,9 @@ function initialize({
});
}
function putAttachment(encryptedBin) {
function putAttachment(maybeEncryptedBin) {
const formData = new FormData();
const buffer = Buffer.from(encryptedBin);
const buffer = Buffer.from(maybeEncryptedBin);
formData.append('type', 'network.loki');
formData.append('content', buffer, {
contentType: 'application/octet-stream',
@ -867,6 +868,17 @@ function initialize({
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
async function getProxiedSize(url) {
const result = await _outerAjax(url, {

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

Loading…
Cancel
Save