image cache on cloudflare r2
README.md

The L4 cache #

screenshot

This is my own image cdn built on cloudflare r2 mainly so I can have fast optimized images on my blog.

Docs #

Create .env:

SLACK_BOT_TOKEN=token
SLACK_SIGNING_SECRET=secret
AUTH_TOKEN=token
bun install
bun start

API #

POST /upload #

Uploads an image. Requires the header Authorization: Bearer <AUTH_TOKEN>.

The request body is multipart form-data with these fields:

  • file (required): the image file.
  • preserveFormat (optional): if "true", keep the original format. Otherwise the server converts the image to WebP at quality 85. SVG files are always kept as-is.

Returns JSON:

{ "success": true, "url": "https://l4.dunkirk.sh/i/abc123def456.webp" }

Errors: 401 without a valid token, 400 if the file field is missing, 500 on upload failure.

The file name is a random 12-character id plus an extension that matches the stored content type.

GET /i/:key #

Fetches an image. Records a hit in the stats database, then returns a 307 redirect to the object in R2. Returns 404 if the key is empty.

GET /health #

Returns { "status": "ok" }.

Stats API #

All stats endpoints return JSON. Time buckets are Unix timestamps in seconds. The days parameter is clamped to the range 1 to 365.

GET /api/stats/overview?days=7 #

Returns totals for the period:

{
  "totalHits": 1234,
  "uniqueImages": 56,
  "topImages": [{ "image_key": "abc123def456.webp", "total": 100 }]
}

topImages lists at most 20 images, sorted by hit count.

GET /api/stats/traffic?days=7 #

Returns hits over time:

{
  "granularity": "hourly",
  "data": [{ "bucket": 1753000000, "hits": 42 }]
}

The granularity depends on the time span: 10min for up to 1 day, hourly for up to 30 days, daily above that. You can also pass start and end (Unix seconds) instead of days.

GET /api/stats/image/:key?days=30 #

Returns hourly hits for one image:

[{ "bucket_hour": 1753000000, "hits": 42 }]

© 2025-present Kieran Klukas