Skip to content
codingeologistPublic

About

A postgres-based image object store and TUI viewer

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Eidolon

From image to archive


Rationale

Eidolon (εἴδωλον), from the Greek eidos (εἶδος: "form, that which is seen"), is the ancient word for a visible image: a likeness, a phantom, a dream-figure, the face of a person as it survives in a picture or in memory. In Homer, the dead appear to the living as eidola, images of themselves that speak; and it is an eidolon og Helen, a phantom image woven by Hera, that travels to Troy while the woman herself remains in Egpyt. The oldest story in literature about a faithful record of a thing standing in for the thing itself.

For Plato, the distinction ran deeper. The eidos is the Form: the true, unchanging essence of the thing. The eidolon is its image, the copy that lives in a world of appearances, one step removed from the truth. The shadows on the wall of the cave in the Republic. We dwell, Plato says, among eidola: we know things through their pictures.

That is precisely what an object store is. Every image ever stored is an eidolon in the oldest sense: not the record, specimen, place, or person, but its faithful likeness, kept so the original can be somewhere else, or be gone. And like Plato's cave, an archive forces the question that this project takes seriously: "what do you owe a copy?" The answer built into this schema is: its form. The eidos of every stored image; its key, type, size, checksum, is held in the same place as the image itself. So a likeness can never drift from what it claims to be. An eidolon without a record is a phantom; an eidolon catalogued is an archive.

The culmination of the two is the rationale for this tool. Postgres holds both the image and its form at once: the eidolon in bytea, the eidos in the columns around it. No external store, no shadow tables, no architecture one step removed. Where S3 keeps an image in one world and its truth in another, Eidolon keeps the copy and its essence in a single row. In myth, eidola were unreliable by nature. With a schema, a trigger, and a checksum, they don't have to be.

A minimal image object store built on a single PostgreSQL instance serving the buckets, object metadata, and the image bytes. No S3-compatible service required, in the spirit of Postgres For Everything

Plato's Cave Wall

Image generated by Mistral Le Chat


┌────────────┐   HTTP (raw bytes + JSON)   ┌──────────┐
│ index.html │ ◀─────────────────────────▶ │ FastAPI  │ ──┐
└────────────┘     (served by the API)     └──────────┘   │
┌────────────┐   SQL (psycopg)                            ▼
│    TUI     │ ────────────────────────────────▶ ┌────────────┐
└────────────┘   (goes straight to Postgres)     │  Postgres  │
                                                 │  buckets   │
                                                 │  objects   │
                                                 └────────────┘

Why?

Postgres can act as a catalogue (object metadata) in relational tables and an object store (payloads as bytea, compressed and stored out-of-line by TOAST) at the same time. This repo is a working test bed for that idea: a schema and ways to interact with the data, and a place to note design strains.


Storage Model

  • buckets: named containers using the AWS S3 style.
  • objects: one row per object, holding key, content_type, size_bytes, checksum_sha256, status, as well as the bytea payload. (bucket_id and key are unique).
  • Payloads are stored as bytea, out-of-line by TOAST. The column uses STORAGE EXTERNAL (no compression: images are already compressed) so the API can read a large object in cheap slices. size_bytes is maintained by trigger.
  • put_object() takes the raw bytes (and the SHA256 computed by the API), and upserts on key conflict. It backs PUT /buckets/{bucket}/objects/{key}.
  • store_object() (base64 via PostgREST) is legacy: it is still in the schema but nothing uses it, and it will be removed.
  • Full schema in initdb/01-schema.sql.

Quickstart

docker compose up -d

This starts Postgres and the FastAPI service. The schema is created automatically on first start (empty volume only; use docker compose down -v for a clean slate). Open http://localhost:8000 for the web UI, or http://localhost:8000/docs for the interactive API docs.

Add a bucket and upload an image:

# create a bucket
curl -X POST http://localhost:8000/buckets \
  -H "Content-Type: application/json" -d '{"name":"my-images"}'

# upload: the request body is the raw file, the type comes from the Content-Type header
curl -X PUT http://localhost:8000/buckets/my-images/objects/pets/cat.jpg \
     -H "Content-Type: image/jpeg" --data-binary @cat.jpg

See Usage for listing, head-style metadata and deletion


Usage

Three interchangeable methods to interact with the object store:

Interface Method Usage
REST API (FastAPI) http://localhost:8000 curl, scripting; docs at /docs
Web browser UI http://localhost:8000 image catalogue browsing, upload, preview
Terminal UI (TUI) eidolon fast local inspection and image preview

REST Endpoints

Method Path Purpose
GET /buckets list buckets
POST /buckets create a bucket ({"name": "..."}); 409 if it exists
DELETE /buckets/{bucket} delete a bucket; 409 if not empty unless ?force=true
GET /buckets/{bucket}/objects list object metadata (?prefix=, ?limit=, ?offset=)
PUT /buckets/{bucket}/objects/{key} upload raw bytes (create or overwrite); returns metadata
GET /buckets/{bucket}/objects/{key} download the bytes (?meta=true for JSON metadata instead)
HEAD /buckets/{bucket}/objects/{key} headers only: type, size, ETag (the SHA256)
DELETE /buckets/{bucket}/objects/{key} delete an object
GET /healthz database connectivity check

Keys may contain / (pets/cat.jpg). Bucket names are lowercase letters, digits, ., - and _.

# list buckets
curl http://localhost:8000/buckets

# list objects (metadata only, the payload is never included)
curl "http://localhost:8000/buckets/my-images/objects?prefix=pets/"

# "head object"
curl -I http://localhost:8000/buckets/my-images/objects/pets/cat.jpg

# download
curl -o cat.jpg http://localhost:8000/buckets/my-images/objects/pets/cat.jpg

# delete
curl -X DELETE http://localhost:8000/buckets/my-images/objects/pets/cat.jpg

Uploads are capped at MAX_UPLOAD_BYTES (default 50 MiB; 413 above that), because the body is buffered in memory before the insert. Only raster images are served inline; every other type (including SVG) is sent as a download with a restrictive Content-Security-Policy, since uploads are served from the same origin as the UI.


TUI

uv venv venv
source venv/bin/activate
uv pip install -e .
eidolon

Arrow keys / click to select, r for refresh, q to quit. The TUI reads image blobs directly over SQL and renders previews with Pillow. Non-image payloads show a preview error.


Lessons learnt

(the hard way)

  1. Upload raw bytes with PUT. The old base64 route stored whatever text it was given if you used a plain POST /objects, which silently double-encoded the image and inflated it by about 33%. The API now takes the bytes directly and checksums them.
  2. Postgres hands bytea back as hex (\x...). The old UI had to hex-decode it in JavaScript; the API streams the raw bytes with the right Content-Type instead.
  3. Content types are trusted, not validated. Currently, the store will file a WebP as image/jpeg. Need to verify the image bytes.
  4. bytea cannot be streamed into Postgres, so an upload is buffered in memory (hence the size cap). Downloads are read in 1 MiB slices. Larger objects are a reason to benchmark large objects (see the roadmap).
  5. There is no authentication and the DB role is a superuser. This stack is for local use. The API port is published on 127.0.0.1 only; do not expose it or Postgres to a network without adding authentication and a least-privilege role.

Roadmap

  • FastAPI service to replace PostgREST: raw binary upload and download, with index.html served from the application.
  • Remove the legacy store_object() SQL function.
  • Authentication and a least-privilege database role for the API.
  • Optional multipart upload route.
  • Magic-byte validation against the declared content_type.
  • uv dependency management and app.py containerisation.
  • Benchmarks: bytea vs pg_largeobjects at least 50 and 200Mb payload sizes.

License

GPL-3.0. See LICENSE.

About

A postgres-based image object store and TUI viewer

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages