No longer serialize binary blobs as data URLs

This commit is contained in:
Zef Hemel
2023-05-26 14:04:32 +02:00
parent 25c789538d
commit 1a1b942f92
10 changed files with 34 additions and 94 deletions
+1 -1
View File
@@ -180,7 +180,7 @@ export function attachmentExtension(editor: Editor) {
if (!finalFileName) {
return;
}
await editor.space.writeAttachment(finalFileName, "arraybuffer", data!);
await editor.space.writeAttachment(finalFileName, new Uint8Array(data));
let attachmentMarkdown = `[${finalFileName}](${
encodeURIComponent(finalFileName)
})`;
+3 -11
View File
@@ -33,24 +33,16 @@ class TableViewWidget extends WidgetType {
});
});
renderMarkdownToHtml(this.t, {
dom.innerHTML = renderMarkdownToHtml(this.t, {
// Annotate every element with its position so we can use it to put
// the cursor there when the user clicks on the table.
annotationPositions: true,
inlineAttachments: async (url): Promise<string> => {
inlineAttachments: (url) => {
if (!url.includes("://")) {
try {
const d = await this.editor.space.readAttachment(url, "dataurl");
return d.data as string;
} catch (e: any) {
console.error(e);
return url;
}
return `/.fs/${url}`;
}
return url;
},
}).then((html) => {
dom.innerHTML = html;
});
return dom;
}
-1
View File
@@ -3,7 +3,6 @@ import { mime } from "https://deno.land/x/mimetypes@v1.0.0/mod.ts";
import type { FileContent } from "../common/spaces/indexeddb_space_primitives.ts";
import { simpleHash } from "../common/crypto.ts";
import { clientStore } from "../plug-api/silverbullet-syscall/mod.ts";
const CACHE_NAME = "{{CACHE_NAME}}";
+9 -47
View File
@@ -10,9 +10,6 @@ import {
import { mime } from "./deps.ts";
import { AttachmentMeta, PageMeta } from "./types.ts";
export type FileEncoding = "utf8" | "arraybuffer" | "dataurl";
export type FileData = ArrayBuffer | string;
export type SpaceEvents = {
pageCreated: (meta: PageMeta) => void;
pageChanged: (meta: PageMeta) => void;
@@ -190,31 +187,12 @@ export class Space extends EventEmitter<SpaceEvents> {
/**
* Reads an attachment
* @param name path of the attachment
* @param encoding how the return value is expected to be encoded
* @returns
*/
async readAttachment(
readAttachment(
name: string,
encoding: FileEncoding,
): Promise<{ data: FileData; meta: AttachmentMeta }> {
const { data, meta } = await this.spacePrimitives.readFile(name);
switch (encoding) {
case "arraybuffer":
return { data, meta };
case "dataurl":
return {
data: base64EncodedDataUrl(
mime.getType(name) || "application/octet-stream",
data,
),
meta,
};
case "utf8":
return {
data: new TextDecoder().decode(data),
meta,
};
}
): Promise<{ data: Uint8Array; meta: AttachmentMeta }> {
return this.spacePrimitives.readFile(name);
}
getAttachmentMeta(name: string): Promise<AttachmentMeta> {
@@ -223,30 +201,14 @@ export class Space extends EventEmitter<SpaceEvents> {
writeAttachment(
name: string,
encoding: FileEncoding,
data: FileData,
data: Uint8Array,
selfUpdate?: boolean | undefined,
): Promise<AttachmentMeta> {
switch (encoding) {
case "arraybuffer":
return this.spacePrimitives.writeFile(
name,
data as Uint8Array,
selfUpdate,
);
case "dataurl":
return this.spacePrimitives.writeFile(
name,
base64DecodeDataUrl(data as string),
selfUpdate,
);
case "utf8":
return this.spacePrimitives.writeFile(
name,
new TextEncoder().encode(data as string),
selfUpdate,
);
}
return this.spacePrimitives.writeFile(
name,
data as Uint8Array,
selfUpdate,
);
}
deleteAttachment(name: string): Promise<void> {
+5 -7
View File
@@ -1,7 +1,6 @@
import { Editor } from "../editor.tsx";
import { SysCallMapping } from "../../plugos/system.ts";
import { AttachmentMeta, PageMeta } from "../types.ts";
import { FileData, FileEncoding } from "../space.ts";
export function spaceSyscalls(editor: Editor): SysCallMapping {
const space = editor.space;
@@ -44,8 +43,8 @@ export function spaceSyscalls(editor: Editor): SysCallMapping {
"space.readAttachment": async (
_ctx,
name: string,
): Promise<FileData> => {
return (await space.readAttachment(name, "dataurl")).data;
): Promise<Uint8Array> => {
return (await space.readAttachment(name)).data;
},
"space.getAttachmentMeta": async (
_ctx,
@@ -53,13 +52,12 @@ export function spaceSyscalls(editor: Editor): SysCallMapping {
): Promise<AttachmentMeta> => {
return await space.getAttachmentMeta(name);
},
"space.writeAttachment": async (
"space.writeAttachment": (
_ctx,
name: string,
encoding: FileEncoding,
data: string,
data: Uint8Array,
): Promise<AttachmentMeta> => {
return await space.writeAttachment(name, encoding, data);
return space.writeAttachment(name, data);
},
"space.deleteAttachment": async (_ctx, name: string) => {
await space.deleteAttachment(name);