Skip to main content

GoTipe image uploads: save the path, resolve the URL

Updated 4 min readMarkdown

An image preview seems to work, and it appears that the upload is complete. But if the app saves that preview's blob: URL in the document, once that URL is revoked, the saved string no longer works to fetch the image; the file may still be present.

GoTipe is a block editor library with an application-owned storage contract. Its saveAsset helper function will pass a file to the host's saveAsset callback. The saveAsset callback stores the bytes and returns a reference that will be placed in the document; the resolveAsset callback will convert the reference into a URL to display.

Here, the reference is stored as assets/pixel.png. When the host is asked to resolve the reference, it will read the file and create a new object URL. Changing the display URL does not require changing the document's src attribute.

Revoke the URL, then reopen the document

This example uses a temporary directory and one small PNG. The script writes both the image and the document JSON to disk, destroys the first editor, and creates a new one from the saved JSON. The assertions check the fetched bytes against the original bytes.

Save the JavaScript code below as asset-path-check.mjs in a new directory. We ran the whole script twice, using Node.js 24.12.0 and the pinned version of the published package shown here:

npm init -y
npm install --save-exact @gotipe/core@0.1.6
node asset-path-check.mjs
import assert from "node:assert/strict";
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { createEditor, saveAsset } from "@gotipe/core/js";
import { Image, Paragraph } from "@gotipe/core/extensions";

const png = Buffer.from(
  "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jRZkAAAAASUVORK5CYII=",
  "base64",
);
const directory = mkdtempSync(join(tmpdir(), "gotipe-asset-path-"));
const storedPath = "assets/pixel.png";
const assetFile = join(directory, storedPath);
const documentFile = join(directory, "document.json");
const displayUrls = new Set();
let editor;
try {
  const host = {
    async saveAsset(file, info) {
      assert.deepEqual(info, {
        kind: "image", name: "pixel.png", mediaType: "image/png", size: png.length,
      });
      mkdirSync(join(directory, "assets"));
      writeFileSync(assetFile, Buffer.from(await file.arrayBuffer()), { flag: "wx" });
      return { src: storedPath, name: info.name };
    },
    resolveAsset(src) {
      if (!src.startsWith("assets/")) return src;
      if (src !== storedPath || !existsSync(assetFile)) return null;
      const url = URL.createObjectURL(new Blob([readFileSync(assetFile)], { type: "image/png" }));
      displayUrls.add(url);
      return url;
    },
  };
  const saved = await saveAsset(host, new File([png], "pixel.png", { type: "image/png" }), "image");
  assert.deepEqual(saved, { src: storedPath, name: "pixel.png" });
  editor = await createEditor({ extensions: [Paragraph, Image], host });
  assert.equal(editor.chain().insertBlockAfter("image", {
    src: saved.src, alt: "One-pixel test image",
  }).run(), true);
  writeFileSync(documentFile, JSON.stringify(editor.getJSON()), { flag: "wx" });
  const documentBytes = readFileSync(documentFile);
  const image = editor.getJSON().blocks.find(block => block.type === "image");
  assert.equal(image.attrs.src, storedPath);
  const firstUrl = await host.resolveAsset(image.attrs.src);
  assert.deepEqual(Buffer.from(await (await fetch(firstUrl)).arrayBuffer()), png);
  assert.equal(documentBytes.toString().includes("blob:"), false);

  URL.revokeObjectURL(firstUrl);
  displayUrls.delete(firstUrl);
  await assert.rejects(() => fetch(firstUrl));
  editor.destroy();
  editor = await createEditor({
    extensions: [Paragraph, Image], host,
    content: JSON.parse(readFileSync(documentFile, "utf8")),
  });
  const restored = editor.getJSON().blocks.find(block => block.type === "image");
  assert.equal(restored.attrs.src, storedPath);
  const secondUrl = await host.resolveAsset(restored.attrs.src);
  assert.notEqual(secondUrl, firstUrl);
  assert.deepEqual(Buffer.from(await (await fetch(secondUrl)).arrayBuffer()), png);
  assert.deepEqual(readFileSync(documentFile), documentBytes);
  assert.equal(host.resolveAsset("assets/missing.png"), null);
  console.log("Saved document source: assets/pixel.png; no blob URL stored.");
  console.log("The first display URL worked, then failed after revocation.");
  console.log("Reloaded document: same stored path, new working display URL.");
  console.log("Resolved image bytes and the unchanged document file matched.");
  console.log("Node.js host-contract test only; no browser renderer or cloud upload.");
} finally {
  editor?.destroy();
  for (const url of displayUrls) URL.revokeObjectURL(url);
  rmSync(directory, { recursive: true, force: true });
}

A successful run will print out five lines of confirmation text, with the last being Node.js host-contract test only; no browser renderer or cloud upload. An assertion failure stops the script.

The first URL returns the correct bytes; the URL is revoked explicitly, and the next fetch fails. The second editor still stores assets/pixel.png as the image's src, and resolving that path returns a different URL that works. The saved document file is unchanged, byte-for-byte.

What the application still owns

The script is running the resolver directly in Node.js and it is not rendering the image in the browser or testing a cloud upload. The URL revocation is deliberate and GoTipe has not detected an expired URL or updated the URL automatically.

The host must hold onto the bytes of the asset and manage the URLs it creates. This fixture deletes its temporary files and releases its URLs at the end. It checks reopening within one process, not long-term storage. Its fixed path is not a production filename policy.

The host contract describes the two callbacks. The media reference covers image commands and attributes.

The core package used here is MIT-licensed. GoTipe's ready-made editor UI and themes use commercial licenses. This post was drafted with AI. The example was executed against the pinned package version above.

About the author

  • GoTipe is a block editor for apps that store Markdown. Our TypeScript and Rust/WASM engines, Markdown codec and headless adapters for React, Vue, Svelte and Solid are MIT-licensed. The ready-made UI and themes use a commercial license. We share runnable examples of Markdown preservation, editor integration, collaboration and AI review. Try the editor and read the docs: https://gotipe.com Source and examples: https://github.com/veniameen/gotipe

No comments yet. Be the first to comment.