2021-11-24 14:45:13 +01:00
|
|
|
import { compressData, decompressData } from "../../data/encode";
|
2021-10-21 22:05:48 +02:00
|
|
|
import {
|
2021-11-07 14:33:21 +01:00
|
|
|
decryptData,
|
2021-10-21 22:05:48 +02:00
|
|
|
generateEncryptionKey,
|
|
|
|
IV_LENGTH_BYTES,
|
|
|
|
} from "../../data/encryption";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { serializeAsJSON } from "../../data/json";
|
|
|
|
import { restore } from "../../data/restore";
|
|
|
|
import { ImportedDataState } from "../../data/types";
|
2021-10-21 22:05:48 +02:00
|
|
|
import { isInitializedImageElement } from "../../element/typeChecks";
|
|
|
|
import { ExcalidrawElement, FileId } from "../../element/types";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { t } from "../../i18n";
|
2021-10-21 22:05:48 +02:00
|
|
|
import {
|
|
|
|
AppState,
|
|
|
|
BinaryFileData,
|
|
|
|
BinaryFiles,
|
|
|
|
UserIdleState,
|
|
|
|
} from "../../types";
|
2021-11-07 14:33:21 +01:00
|
|
|
import { bytesToHexString } from "../../utils";
|
|
|
|
import { FILE_UPLOAD_MAX_BYTES, ROOM_ID_BYTES } from "../app_constants";
|
2021-10-21 22:05:48 +02:00
|
|
|
import { encodeFilesForUpload } from "./FileManager";
|
|
|
|
import { saveFilesToFirebase } from "./firebase";
|
2020-12-05 20:00:53 +05:30
|
|
|
|
|
|
|
const BACKEND_V2_GET = process.env.REACT_APP_BACKEND_V2_GET_URL;
|
2020-12-20 19:44:04 +05:30
|
|
|
const BACKEND_V2_POST = process.env.REACT_APP_BACKEND_V2_POST_URL;
|
2020-12-05 20:00:53 +05:30
|
|
|
|
2021-11-07 14:33:21 +01:00
|
|
|
const generateRoomId = async () => {
|
|
|
|
const buffer = new Uint8Array(ROOM_ID_BYTES);
|
|
|
|
window.crypto.getRandomValues(buffer);
|
|
|
|
return bytesToHexString(buffer);
|
2020-12-05 20:00:53 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
export const SOCKET_SERVER = process.env.REACT_APP_SOCKET_SERVER_URL;
|
|
|
|
|
|
|
|
export type EncryptedData = {
|
|
|
|
data: ArrayBuffer;
|
|
|
|
iv: Uint8Array;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type SocketUpdateDataSource = {
|
|
|
|
SCENE_INIT: {
|
|
|
|
type: "SCENE_INIT";
|
|
|
|
payload: {
|
|
|
|
elements: readonly ExcalidrawElement[];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
SCENE_UPDATE: {
|
|
|
|
type: "SCENE_UPDATE";
|
|
|
|
payload: {
|
|
|
|
elements: readonly ExcalidrawElement[];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
MOUSE_LOCATION: {
|
|
|
|
type: "MOUSE_LOCATION";
|
|
|
|
payload: {
|
|
|
|
socketId: string;
|
|
|
|
pointer: { x: number; y: number };
|
|
|
|
button: "down" | "up";
|
|
|
|
selectedElementIds: AppState["selectedElementIds"];
|
|
|
|
username: string;
|
|
|
|
};
|
|
|
|
};
|
2021-02-04 11:55:43 +01:00
|
|
|
IDLE_STATUS: {
|
|
|
|
type: "IDLE_STATUS";
|
|
|
|
payload: {
|
|
|
|
socketId: string;
|
|
|
|
userState: UserIdleState;
|
|
|
|
username: string;
|
|
|
|
};
|
|
|
|
};
|
2020-12-05 20:00:53 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
export type SocketUpdateDataIncoming =
|
|
|
|
| SocketUpdateDataSource[keyof SocketUpdateDataSource]
|
|
|
|
| {
|
|
|
|
type: "INVALID_RESPONSE";
|
|
|
|
};
|
|
|
|
|
2021-11-01 15:24:05 +02:00
|
|
|
export type SocketUpdateData =
|
|
|
|
SocketUpdateDataSource[keyof SocketUpdateDataSource] & {
|
|
|
|
_brand: "socketUpdateData";
|
|
|
|
};
|
2020-12-05 20:00:53 +05:30
|
|
|
|
|
|
|
export const getCollaborationLinkData = (link: string) => {
|
|
|
|
const hash = new URL(link).hash;
|
2021-02-03 19:14:26 +01:00
|
|
|
const match = hash.match(/^#room=([a-zA-Z0-9_-]+),([a-zA-Z0-9_-]+)$/);
|
2021-09-25 02:04:56 +05:30
|
|
|
if (match && match[2].length !== 22) {
|
|
|
|
window.alert(t("alerts.invalidEncryptionKey"));
|
|
|
|
return null;
|
|
|
|
}
|
2021-02-03 19:14:26 +01:00
|
|
|
return match ? { roomId: match[1], roomKey: match[2] } : null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const generateCollaborationLinkData = async () => {
|
2021-11-07 14:33:21 +01:00
|
|
|
const roomId = await generateRoomId();
|
2021-02-03 19:14:26 +01:00
|
|
|
const roomKey = await generateEncryptionKey();
|
|
|
|
|
|
|
|
if (!roomKey) {
|
|
|
|
throw new Error("Couldn't generate room key");
|
|
|
|
}
|
|
|
|
|
|
|
|
return { roomId, roomKey };
|
2020-12-05 20:00:53 +05:30
|
|
|
};
|
|
|
|
|
2021-02-03 19:14:26 +01:00
|
|
|
export const getCollaborationLink = (data: {
|
|
|
|
roomId: string;
|
|
|
|
roomKey: string;
|
|
|
|
}) => {
|
|
|
|
return `${window.location.origin}${window.location.pathname}#room=${data.roomId},${data.roomKey}`;
|
2020-12-05 20:00:53 +05:30
|
|
|
};
|
|
|
|
|
2021-11-24 14:45:13 +01:00
|
|
|
/**
|
|
|
|
* Decodes shareLink data using the legacy buffer format.
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
const legacy_decodeFromBackend = async ({
|
|
|
|
buffer,
|
|
|
|
decryptionKey,
|
|
|
|
}: {
|
|
|
|
buffer: ArrayBuffer;
|
|
|
|
decryptionKey: string;
|
|
|
|
}) => {
|
|
|
|
let decrypted: ArrayBuffer;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Buffer should contain both the IV (fixed length) and encrypted data
|
|
|
|
const iv = buffer.slice(0, IV_LENGTH_BYTES);
|
|
|
|
const encrypted = buffer.slice(IV_LENGTH_BYTES, buffer.byteLength);
|
|
|
|
decrypted = await decryptData(new Uint8Array(iv), encrypted, decryptionKey);
|
|
|
|
} catch (error: any) {
|
|
|
|
// Fixed IV (old format, backward compatibility)
|
|
|
|
const fixedIv = new Uint8Array(IV_LENGTH_BYTES);
|
|
|
|
decrypted = await decryptData(fixedIv, buffer, decryptionKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to convert the decrypted array buffer to a string
|
|
|
|
const string = new window.TextDecoder("utf-8").decode(
|
|
|
|
new Uint8Array(decrypted),
|
|
|
|
);
|
|
|
|
const data: ImportedDataState = JSON.parse(string);
|
|
|
|
|
|
|
|
return {
|
|
|
|
elements: data.elements || null,
|
|
|
|
appState: data.appState || null,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-12-05 20:00:53 +05:30
|
|
|
const importFromBackend = async (
|
2021-11-02 14:52:25 +02:00
|
|
|
id: string,
|
2021-11-24 14:45:13 +01:00
|
|
|
decryptionKey: string,
|
2020-12-05 20:00:53 +05:30
|
|
|
): Promise<ImportedDataState> => {
|
|
|
|
try {
|
2021-11-02 14:52:25 +02:00
|
|
|
const response = await fetch(`${BACKEND_V2_GET}${id}`);
|
2021-03-22 06:31:35 +01:00
|
|
|
|
2020-12-05 20:00:53 +05:30
|
|
|
if (!response.ok) {
|
|
|
|
window.alert(t("alerts.importBackendFailed"));
|
|
|
|
return {};
|
|
|
|
}
|
2021-11-02 14:52:25 +02:00
|
|
|
const buffer = await response.arrayBuffer();
|
|
|
|
|
|
|
|
try {
|
2021-11-24 14:45:13 +01:00
|
|
|
const { data: decodedBuffer } = await decompressData(
|
|
|
|
new Uint8Array(buffer),
|
|
|
|
{
|
|
|
|
decryptionKey,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
const data: ImportedDataState = JSON.parse(
|
|
|
|
new TextDecoder().decode(decodedBuffer),
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
elements: data.elements || null,
|
|
|
|
appState: data.appState || null,
|
|
|
|
};
|
2021-11-02 14:52:25 +02:00
|
|
|
} catch (error: any) {
|
2021-11-24 14:45:13 +01:00
|
|
|
console.warn(
|
|
|
|
"error when decoding shareLink data using the new format:",
|
|
|
|
error,
|
|
|
|
);
|
|
|
|
return legacy_decodeFromBackend({ buffer, decryptionKey });
|
2020-12-05 20:00:53 +05:30
|
|
|
}
|
2021-11-02 14:24:16 +02:00
|
|
|
} catch (error: any) {
|
2020-12-05 20:00:53 +05:30
|
|
|
window.alert(t("alerts.importBackendFailed"));
|
|
|
|
console.error(error);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const loadScene = async (
|
|
|
|
id: string | null,
|
|
|
|
privateKey: string | null,
|
2021-04-10 19:17:49 +02:00
|
|
|
// Supply local state even if importing from backend to ensure we restore
|
2020-12-05 20:00:53 +05:30
|
|
|
// localStorage user settings which we do not persist on server.
|
|
|
|
// Non-optional so we don't forget to pass it even if `undefined`.
|
2021-04-10 19:17:49 +02:00
|
|
|
localDataState: ImportedDataState | undefined | null,
|
2020-12-05 20:00:53 +05:30
|
|
|
) => {
|
|
|
|
let data;
|
2021-11-02 14:52:25 +02:00
|
|
|
if (id != null && privateKey != null) {
|
2020-12-05 20:00:53 +05:30
|
|
|
// the private key is used to decrypt the content from the server, take
|
|
|
|
// extra care not to leak it
|
|
|
|
data = restore(
|
|
|
|
await importFromBackend(id, privateKey),
|
2021-04-10 19:17:49 +02:00
|
|
|
localDataState?.appState,
|
2021-07-04 22:23:35 +02:00
|
|
|
localDataState?.elements,
|
2020-12-05 20:00:53 +05:30
|
|
|
);
|
|
|
|
} else {
|
2021-07-04 22:23:35 +02:00
|
|
|
data = restore(localDataState || null, null, null);
|
2020-12-05 20:00:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
elements: data.elements,
|
|
|
|
appState: data.appState,
|
2021-10-21 22:05:48 +02:00
|
|
|
// note: this will always be empty because we're not storing files
|
|
|
|
// in the scene database/localStorage, and instead fetch them async
|
|
|
|
// from a different database
|
|
|
|
files: data.files,
|
2020-12-05 20:00:53 +05:30
|
|
|
commitToHistory: false,
|
|
|
|
};
|
|
|
|
};
|
2020-12-20 19:44:04 +05:30
|
|
|
|
|
|
|
export const exportToBackend = async (
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
appState: AppState,
|
2021-10-21 22:05:48 +02:00
|
|
|
files: BinaryFiles,
|
2020-12-20 19:44:04 +05:30
|
|
|
) => {
|
2021-11-24 14:45:13 +01:00
|
|
|
const encryptionKey = await generateEncryptionKey("string");
|
2021-03-22 06:31:35 +01:00
|
|
|
|
2021-11-24 14:45:13 +01:00
|
|
|
const payload = await compressData(
|
|
|
|
new TextEncoder().encode(
|
|
|
|
serializeAsJSON(elements, appState, files, "database"),
|
|
|
|
),
|
|
|
|
{ encryptionKey },
|
|
|
|
);
|
2020-12-20 19:44:04 +05:30
|
|
|
|
|
|
|
try {
|
2021-10-21 22:05:48 +02:00
|
|
|
const filesMap = new Map<FileId, BinaryFileData>();
|
|
|
|
for (const element of elements) {
|
|
|
|
if (isInitializedImageElement(element) && files[element.fileId]) {
|
|
|
|
filesMap.set(element.fileId, files[element.fileId]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const filesToUpload = await encodeFilesForUpload({
|
|
|
|
files: filesMap,
|
|
|
|
encryptionKey,
|
|
|
|
maxBytes: FILE_UPLOAD_MAX_BYTES,
|
|
|
|
});
|
|
|
|
|
2020-12-20 19:44:04 +05:30
|
|
|
const response = await fetch(BACKEND_V2_POST, {
|
|
|
|
method: "POST",
|
2021-11-24 14:45:13 +01:00
|
|
|
body: payload.buffer,
|
2020-12-20 19:44:04 +05:30
|
|
|
});
|
|
|
|
const json = await response.json();
|
|
|
|
if (json.id) {
|
|
|
|
const url = new URL(window.location.href);
|
|
|
|
// We need to store the key (and less importantly the id) as hash instead
|
|
|
|
// of queryParam in order to never send it to the server
|
2021-10-21 22:05:48 +02:00
|
|
|
url.hash = `json=${json.id},${encryptionKey}`;
|
2020-12-20 19:44:04 +05:30
|
|
|
const urlString = url.toString();
|
2021-10-21 22:05:48 +02:00
|
|
|
|
|
|
|
await saveFilesToFirebase({
|
|
|
|
prefix: `/files/shareLinks/${json.id}`,
|
|
|
|
files: filesToUpload,
|
|
|
|
});
|
|
|
|
|
2020-12-20 19:44:04 +05:30
|
|
|
window.prompt(`🔒${t("alerts.uploadedSecurly")}`, urlString);
|
|
|
|
} else if (json.error_class === "RequestTooLargeError") {
|
|
|
|
window.alert(t("alerts.couldNotCreateShareableLinkTooBig"));
|
|
|
|
} else {
|
|
|
|
window.alert(t("alerts.couldNotCreateShareableLink"));
|
|
|
|
}
|
2021-11-02 14:24:16 +02:00
|
|
|
} catch (error: any) {
|
2020-12-20 19:44:04 +05:30
|
|
|
console.error(error);
|
|
|
|
window.alert(t("alerts.couldNotCreateShareableLink"));
|
|
|
|
}
|
|
|
|
};
|