fix encoding of embed data & compress (#2240)

This commit is contained in:
David Luzar 2020-10-15 21:31:21 +02:00 committed by GitHub
parent e8a39b5f84
commit b3263c2a69
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 483 additions and 122 deletions

View file

@ -4,16 +4,20 @@ import { t } from "../i18n";
import { AppState } from "../types";
import { LibraryData, ImportedDataState } from "./types";
import { calculateScrollCenter } from "../scene";
import { MIME_TYPES } from "../constants";
import { base64ToString } from "../base64";
export const parseFileContents = async (blob: Blob | File) => {
let contents: string;
if (blob.type === "image/png") {
const metadata = await (await import("./png")).getTEXtChunk(blob);
if (metadata?.keyword === MIME_TYPES.excalidraw) {
return metadata.text;
try {
return await (await import("./image")).decodePngMetadata(blob);
} catch (error) {
if (error.message === "INVALID") {
throw new Error(t("alerts.imageDoesNotContainScene"));
} else {
throw new Error(t("alerts.cannotRestoreFromImage"));
}
}
throw new Error(t("alerts.imageDoesNotContainScene"));
} else {
if ("text" in Blob) {
contents = await blob.text();
@ -29,16 +33,17 @@ export const parseFileContents = async (blob: Blob | File) => {
});
}
if (blob.type === "image/svg+xml") {
if (contents.includes(`payload-type:${MIME_TYPES.excalidraw}`)) {
const match = contents.match(
/<!-- payload-start -->(.+?)<!-- payload-end -->/,
);
if (!match) {
try {
return await (await import("./image")).decodeSvgMetadata({
svg: contents,
});
} catch (error) {
if (error.message === "INVALID") {
throw new Error(t("alerts.imageDoesNotContainScene"));
} else {
throw new Error(t("alerts.cannotRestoreFromImage"));
}
return base64ToString(match[1]);
}
throw new Error(t("alerts.imageDoesNotContainScene"));
}
}
return contents;