Files
splatoon3.ink/app/social/buffer/BufferMedia.mjs
Matt Isenhower 21a4f5d85c Route backend file access through storage and auth seams
Prepare the shared updater and social code to run against R2 in a Worker
while keeping the local Node runner working:

- Add app/common/storage with public, private, and archive areas keyed like
  the R2 objects. FilesystemStorage maps them onto dist/ and storage/ for
  Node; BucketStorage wraps R2 bindings. Storage is configured once per
  process; only image existence is cached, so JSON reads are always fresh.
- Move ValueCache, DataUpdater (data, calendars, archive snapshots),
  ImageProcessor, LocalizationProcessor, festival updaters, status
  generators, social translations, ImageWriter, and Buffer/Threads image
  hosting onto storage. Query hashes are a JSON import.
- Add app/splatnet/auth.mjs: the existing bullet-token code behind auth(),
  so the Worker can route token requests to per-region Durable Objects.
  warmCaches uses auth().refresh().
- Remove the S3/R2 sync, vfs, the dual upload, the sync commands, and
  s3-sync-client. Local runs no longer read from or write to remote buckets.
- Give every test fresh in-memory storage via app/test/setup.mjs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 16:03:33 -08:00

48 lines
1.6 KiB
JavaScript

import { storage } from '../../common/storage/index.mjs';
/**
* Hosts post images for Buffer, which fetches them by URL. Images go to public storage, so
* this needs storage served at a public HTTPS URL (the Worker's R2 bucket); the local Node
* runner writes to dist/ and cannot host them.
*/
export default class BufferMedia {
get publicUrl() {
return storage().public.publicUrl;
}
canUpload() {
return !!this.publicUrl;
}
async upload(media) {
if (!this.canUpload()) {
throw new Error('Buffer images require public storage with a public URL');
}
const extension = { 'image/png': 'png', 'image/jpeg': 'jpg' }[media.type];
if (!extension || !media.file?.byteLength) {
throw new Error('Buffer requires a nonempty PNG or JPEG image');
}
const publicUrl = new URL(this.publicUrl);
if (publicUrl.protocol !== 'https:' || publicUrl.username || publicUrl.password
|| publicUrl.search || publicUrl.hash) {
throw new Error('The public storage URL must be an HTTPS base URL without credentials, query, or fragment');
}
// Production R2 lifecycle rules delete all temporary images under this prefix
// after seven days, leaving time for Buffer to fetch queued posts' images.
const key = `buffer-twitter/${crypto.randomUUID()}.${extension}`;
publicUrl.pathname = `${publicUrl.pathname.replace(/\/+$/, '')}/${key}`;
await storage().public.writeBytes(key, media.file, {
contentType: media.type,
cacheControl: 'private, no-store',
});
return { key, url: publicUrl.href };
}
}