mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
fix encoding of embed data & compress (#2240)
This commit is contained in:
parent
e8a39b5f84
commit
b3263c2a69
15 changed files with 483 additions and 122 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue