Files
upstream-tldraw/templates/image-pipeline/worker/routes/images.ts
upstream-mirror a774ac673a
Some checks failed
Deploy analytics worker / Deploy analytics worker to ${{ (github.ref == 'refs/heads/production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Failing after 1s
i18n upload strings / Upload latest strings (push) Failing after 1s
Deploy bemo / Deploy bemo to ${{ (github.ref == 'refs/heads/bemo-production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Failing after 1s
Deploy grafana resources / ${{ github.event_name == 'push' && 'Deploy grafana resources' || 'Validate grafana resources (dry run)' }} (push) Failing after 0s
End to end tests (dotcom) / Check for relevant changes (push) Failing after 0s
End to end tests (examples) / Check for relevant changes (push) Failing after 0s
End to end tests (mermaid readability) / Check for relevant changes (push) Failing after 1s
End to end tests (perf) / Check for relevant changes (push) Failing after 0s
Publish editor extensions / Publish editor extensions (push) Failing after 1s
Publish SDK packages / Remove the publish-packages label (push) Has been skipped
Checks / Tests & checks (push) Has been cancelled
Checks / Build all projects (push) Has been cancelled
Deploy templates / Deploy templates (push) Has been cancelled
End to end tests (dotcom) / End to end tests (dotcom) (push) Has been cancelled
End to end tests (mermaid readability) / End to end tests (mermaid readability) (push) Has been cancelled
End to end tests (perf) / End to end tests (perf) (push) Has been cancelled
Deploy .com / Deploy dotcom to ${{ (github.ref == 'refs/heads/production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Has been cancelled
End to end tests (examples) / End to end tests (examples) (push) Has been cancelled
Publish SDK packages / Publish SDK packages (push) Has been cancelled
Publish SDK packages / Tell user released version (push) Has been cancelled
Publish SDK packages / Publishes code templates to separate repositories (push) Has been cancelled
Publish SDK packages / Deploys the template demos with the new SDK version (push) Has been cancelled
snapshot of https://github.com/tldraw/tldraw.git (main, 2026-09-23; blobs > 131072B omitted)
2026-09-23 16:12:52 +05:00

69 lines
1.9 KiB
TypeScript

import { IRequest } from 'itty-router'
/**
* POST /api/images/:imageId
*
* Upload a generated image to R2 for persistence.
*/
export async function handleImageUpload(request: IRequest, env: Env) {
const { imageId } = request.params
const contentType = request.headers.get('content-type') ?? 'image/png'
if (!contentType.startsWith('image/')) {
return new Response(JSON.stringify({ error: 'Invalid content type' }), {
status: 400,
headers: { 'Content-Type': 'application/json' },
})
}
// Don't overwrite existing images.
if (await env.IMAGE_BUCKET.head(imageId)) {
return new Response(JSON.stringify({ ok: true }), {
headers: { 'Content-Type': 'application/json' },
})
}
await env.IMAGE_BUCKET.put(imageId, request.body, {
httpMetadata: { contentType },
})
return new Response(JSON.stringify({ ok: true }), {
headers: { 'Content-Type': 'application/json' },
})
}
/**
* GET /api/images/:imageId
*
* Download a generated image from R2 with caching.
*/
export async function handleImageDownload(request: IRequest, env: Env, ctx: ExecutionContext) {
const { imageId } = request.params
// Check edge cache first.
// Use caches.open() to avoid DOM/Workers CacheStorage type conflict.
const cache = await caches.open('images')
const cacheKey = new Request(request.url, { headers: request.headers })
const cachedResponse = await cache.match(cacheKey)
if (cachedResponse) {
return cachedResponse
}
const object = await env.IMAGE_BUCKET.get(imageId)
if (!object) {
return new Response('Not found', { status: 404 })
}
const headers = new Headers()
headers.set('content-type', object.httpMetadata?.contentType ?? 'image/png')
// Generated images are immutable — cache them aggressively.
headers.set('cache-control', 'public, max-age=31536000, immutable')
const response = new Response(object.body, { headers })
// Cache in the edge for future requests.
ctx.waitUntil(cache.put(cacheKey, response.clone()))
return response
}