Files
plainleaf/plugs/editor/upload.ts
T
Zef HemelandGitHub dc2987a3f9 Sync engine rearchitecture (#1516)
* Don't require auth for .ping
* Finally introducing loggers
* Remove file list caching from disk_space_primitives
I don't think this is required anymore with the sync model
* Simplification of space primitives and more tests
* More reliable online status
* Add .zed settings
* Remove selfUpdate argument from `writeFile` no longer used
- Add `sync` config schema and document sync options
- Move sync config handling to client boot logic
- Move BootConfig type to ui_types.ts
- Remove sync env vars from server
- Update documentation for sync and architecture
* Improve sync logic and proxy routing for full sync state
* Add BoxProxy for late-bound client and improve Lua boot
- Add isMobile syscall for editor
* Add concurrency to sync process and log indexing duration
* Better service worker lifecycle management
2025-09-12 14:43:57 +02:00

68 lines
2.0 KiB
TypeScript

import { editor, space, system } from "@silverbulletmd/silverbullet/syscalls";
import { defaultLinkStyle, maximumDocumentSize } from "../../lib/constants.ts";
import { resolveMarkdownLink } from "@silverbulletmd/silverbullet/lib/resolve";
import {
encodePageURI,
isValidPath,
} from "@silverbulletmd/silverbullet/lib/ref";
import type { UploadFile } from "@silverbulletmd/silverbullet/type/client";
export async function saveFile(file: UploadFile) {
const maxSize = await system.getConfig<number>(
"maximumDocumentSize",
maximumDocumentSize,
);
if (typeof maxSize !== "number") {
await editor.flashNotification(
"The setting 'maximumDocumentSize' must be a number",
"error",
);
}
if (file.content.length > maxSize * 1024 * 1024) {
editor.flashNotification(
`Document is too large, maximum is ${maxSize}MiB`,
"error",
);
return;
}
const finalFilePath = await editor.prompt(
"File name for pasted document",
resolveMarkdownLink(
await editor.getCurrentPath(),
isValidPath(file.name)
? file.name
: `file.${
file.name.indexOf(".") !== -1 ? file.name.split(".").pop() : "txt"
}`,
),
);
if (!finalFilePath || !isValidPath(finalFilePath)) {
return;
}
await space.writeDocument(finalFilePath, file.content);
if (await editor.getCurrentEditor() === "page") {
const linkStyle = await system.getConfig(
"defaultLinkStyle",
defaultLinkStyle,
);
let documentMarkdown = "";
if (linkStyle === "wikilink") {
documentMarkdown = `[[${finalFilePath}]]`;
} else {
documentMarkdown = `[${finalFilePath}](${encodePageURI(finalFilePath)})`;
}
if (file.contentType.startsWith("image/")) {
documentMarkdown = "!" + documentMarkdown;
}
editor.insertAtCursor(documentMarkdown);
}
}
export async function uploadFile(_ctx: any, accept?: string, capture?: string) {
const uploadFile = await editor.uploadFile(accept, capture);
await saveFile(uploadFile);
}