obsidian_import_attachment
Import a base64-encoded attachment to a specified vault path in your Obsidian vault. Optionally overwrite existing files.
Instructions
Import a base64-encoded attachment into the vault.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| vault | No | Optional configured vault name. Defaults to the server default vault. | |
| path | Yes | Vault-relative path. Absolute paths and traversal are rejected. | |
| dataBase64 | Yes | ||
| overwrite | No |
Implementation Reference
- src/tools.ts:1048-1058 (handler)The tool handler for 'obsidian_import_attachment'. It accepts a vault-relative path, base64-encoded data, and an overwrite flag, then calls vaults.importBinary() with a Buffer decoded from base64.
tool( "obsidian_import_attachment", "Import a base64-encoded attachment into the vault.", { vault: vaultArg, path: pathArg, dataBase64: z.string(), overwrite: z.boolean().optional().default(false), }, async (args) => vaults.importBinary(args.path, Buffer.from(args.dataBase64, "base64"), args.vault, { overwrite: args.overwrite }), ); - src/tools.ts:1051-1056 (schema)Zod input schema for obsidian_import_attachment: vault (optional string), path (required string), dataBase64 (required string), overwrite (optional boolean, default false).
{ vault: vaultArg, path: pathArg, dataBase64: z.string(), overwrite: z.boolean().optional().default(false), }, - src/tools.ts:1048-1058 (registration)The tool is registered via the local 'tool()' helper function within registerObsidianTools() in src/tools.ts, with the name 'obsidian_import_attachment'.
tool( "obsidian_import_attachment", "Import a base64-encoded attachment into the vault.", { vault: vaultArg, path: pathArg, dataBase64: z.string(), overwrite: z.boolean().optional().default(false), }, async (args) => vaults.importBinary(args.path, Buffer.from(args.dataBase64, "base64"), args.vault, { overwrite: args.overwrite }), ); - src/vault.ts:194-207 (helper)The importBinary() method on VaultManager handles the actual binary file write. It resolves the path, checks overwrite, creates parent directories, writes atomically via atomicWriteBuffer, and returns the path and byte count.
async importBinary( filePath: string, data: Buffer, vaultName?: string | null, options: { overwrite?: boolean } = {}, ): Promise<{ path: string; bytes: number }> { this.assertWritable(); const resolved = this.resolvePath(filePath, vaultName); if (!options.overwrite && fssync.existsSync(resolved.absolute)) throw new Error(`File already exists: ${resolved.relative}`); await fs.mkdir(path.dirname(resolved.absolute), { recursive: true }); await atomicWriteBuffer(resolved.absolute, data); this.onInvalidate?.(resolved.vault.name); return { path: resolved.relative, bytes: data.byteLength }; } - src/vault.ts:314-326 (helper)The atomicWriteBuffer helper function that writes binary data to a temp file, syncs it (on non-Windows), then atomically renames it to the target path.
async function atomicWriteBuffer(filePath: string, data: Buffer): Promise<void> { const tmp = path.join(path.dirname(filePath), `.${path.basename(filePath)}.${process.pid}.${Date.now()}.tmp`); await fs.writeFile(tmp, data); if (os.platform() !== "win32") { const handle = await fs.open(tmp, "r"); try { await handle.sync(); } finally { await handle.close(); } } await fs.rename(tmp, filePath); }