Added an option to use v3/lsrpc endpoint for more compact (base64) file reponses

pull/1388/head
Maxim Shishmarev 4 years ago
parent 921dc9ab39
commit 27859b13aa

@ -2304,7 +2304,9 @@
async deletePublicMessages(messages) {
const channelAPI = await this.getPublicSendData();
if (!channelAPI) {
log.error('Unable to get public channel API');
return false;
}

@ -9,11 +9,6 @@ interface UploadResponse {
id?: number;
}
interface DownloadResponse {
statucCode: number;
reponse: any;
}
export interface LokiAppDotNetServerInterface {
findOrCreateChannel(
api: LokiPublicChatFactoryAPI,
@ -24,7 +19,7 @@ export interface LokiAppDotNetServerInterface {
uploadAvatar(data: FormData): Promise<UploadResponse>;
putAttachment(data: ArrayBuffer): Promise<UploadResponse>;
putAvatar(data: ArrayBuffer): Promise<UploadResponse>;
downloadAttachment(url: String): Promise<DownloadResponse>; // todo: add return type
downloadAttachment(url: String): Promise<ArrayBuffer>;
}
export interface LokiPublicChannelAPI {

@ -154,10 +154,7 @@ const sendViaOnion = async (srvPubKey, url, fetchOptions, options = {}) => {
return {
result,
txtResponse: result.body,
response: {
data: result.body,
headers: result.headers,
},
response: result.body,
};
}
@ -174,7 +171,7 @@ const sendViaOnion = async (srvPubKey, url, fetchOptions, options = {}) => {
body = JSON.parse(result.body);
} catch (e) {
log.error(
`loki_app_dot_net:::sendViaOnion #${options.requestNumber} - Cant decode JSON body`,
`loki_app_dot_net:::sendViaOnion #${options.requestNumber} - Can't decode JSON body`,
typeof result.body,
result.body
);
@ -1016,12 +1013,25 @@ class LokiAppDotNetServerAPI {
return this.uploadAvatar(formData);
}
// This should return Uint8Array in response.data
async downloadAttachment(url) {
const endpoint = new URL(url).pathname;
return this.serverRequest(`loki/v1${endpoint}`, {
// With the new protocol, there is no json in body, we shouldn't try to parse it
const noJson = window.lokiFeatureFlags.useFileOnionRequestsV2;
const res = await this.serverRequest(`loki/v1${endpoint}`, {
method: 'GET',
noJson,
});
if (window.lokiFeatureFlags.useFileOnionRequestsV2) {
const buffer = dcodeIO.ByteBuffer.fromBase64(
res.response
).toArrayBuffer();
return buffer;
}
return new Uint8Array(res.response.data).buffer;
}
}

@ -13,5 +13,5 @@ interface DeviceMappingAnnotation {
interface LokiFileServerInstance {
getUserDeviceMapping(pubKey: string): Promise<DeviceMappingAnnotation | null>;
clearOurDeviceMappingAnnotations(): Promise<void>;
downloadAttachment(url: string): Promise<any>;
downloadAttachment(url: string): Promise<ArrayBuffer>;
}

@ -17,6 +17,7 @@ const validOpenGroupServer = async serverUrl => {
const result = await window.tokenlessFileServerAdnAPI.serverRequest(
`loki/v1/getOpenGroupKey/${url.hostname}`
);
if (result.response.meta.code === 200) {
// supports it
const obj = JSON.parse(result.response.data);

@ -461,6 +461,7 @@ window.lokiFeatureFlags = {
useOnionRequests: true,
useOnionRequestsV2: true,
useFileOnionRequests: true,
useFileOnionRequestsV2: false, // more compact encoding of files in response
enableSenderKeys: true,
onionRequestHops: 3,
debugMessageLogs: process.env.ENABLE_MESSAGE_LOGS,

@ -12,7 +12,7 @@ export async function downloadAttachment(attachment: any) {
serverUrl
);
let res: any;
let res: ArrayBuffer | null = null;
// TODO: we need attachments to remember which API should be used to retrieve them
if (!defaultFileserver) {
@ -31,7 +31,7 @@ export async function downloadAttachment(attachment: any) {
}
// The attachment id is actually just the absolute url of the attachment
let data = new Uint8Array(res.response.data).buffer;
let data = res;
if (!attachment.isRaw) {
const { key, digest, size } = attachment;

@ -140,7 +140,11 @@ async function buildOnionCtxs(
const relayingToFinalDestination = i === firstPos; // if last position
if (relayingToFinalDestination && fileServerOptions) {
const target = useV2 ? '/loki/v2/lsrpc' : '/loki/v1/lsrpc';
let target = useV2 ? '/loki/v2/lsrpc' : '/loki/v1/lsrpc';
if (window.lokiFeatureFlags.useFileOnionRequestsV2) {
target = '/loki/v3/lsrpc';
}
dest = {
host: fileServerOptions.host,
@ -397,9 +401,11 @@ const sendOnionRequest = async (
const useV2 = window.lokiFeatureFlags.useOnionRequestsV2;
const isLsrpc = !!finalRelayOptions;
let destCtx;
try {
if (useV2 && !finalRelayOptions) {
if (useV2 && !isLsrpc) {
const body = options.body || '';
delete options.body;

1
ts/window.d.ts vendored

@ -59,6 +59,7 @@ declare global {
useOnionRequests: boolean;
useOnionRequestsV2: boolean;
useFileOnionRequests: boolean;
useFileOnionRequestsV2: boolean;
enableSenderKeys: boolean;
onionRequestHops: number;
debugMessageLogs: boolean;

Loading…
Cancel
Save