import { readSetting } from "$sb/lib/settings_page.ts"; import { editor, space } from "$sb/syscalls.ts"; import { UploadFile } from "$sb/types.ts"; import { defaultLinkStyle, maximumAttachmentSize, } from "../../web/constants.ts"; import { resolvePath } from "$sb/lib/resolve.ts"; export async function saveFile(file: UploadFile) { const maxSize = await readSetting( "maximumAttachmentSize", maximumAttachmentSize, ); if (typeof maxSize !== "number") { await editor.flashNotification( "The setting 'maximumAttachmentSize' must be a number", "error", ); } if (file.content.length > maxSize * 1024 * 1024) { editor.flashNotification( `Attachment is too large, maximum is ${maxSize}MiB`, "error", ); return; } const finalFileName = await editor.prompt( "File name for pasted attachment", file.name, ); if (!finalFileName) { return; } const attachmentPath = resolvePath( await editor.getCurrentPage(), finalFileName, ); await space.writeAttachment(attachmentPath, file.content); const linkStyle = await readSetting( "defaultLinkStyle", defaultLinkStyle, ); let attachmentMarkdown = ""; if (linkStyle === "wikilink") { attachmentMarkdown = `[[${attachmentPath}]]`; } else { attachmentMarkdown = `[${finalFileName}](${encodeURI(finalFileName)})`; } if (file.contentType.startsWith("image/")) { attachmentMarkdown = "!" + attachmentMarkdown; } editor.insertAtCursor(attachmentMarkdown); } export async function uploadFile(_ctx: any, accept?: string, capture?: string) { const uploadFile = await editor.uploadFile(accept, capture); await saveFile(uploadFile); }