more stuff

This commit is contained in:
Jeff Emmett 2024-08-30 09:44:11 +02:00
parent 80cda32cba
commit 2c4b2f6c91
2 changed files with 21 additions and 15 deletions

View File

@ -10,22 +10,27 @@ function getAssetObjectName(uploadId: string) {
// when a user uploads an asset, we store it in the bucket. we only allow image and video assets. // when a user uploads an asset, we store it in the bucket. we only allow image and video assets.
export async function handleAssetUpload(request: IRequest, env: Environment) { export async function handleAssetUpload(request: IRequest, env: Environment) {
const objectName = getAssetObjectName(request.params.uploadId) try {
const objectName = getAssetObjectName(request.params.uploadId)
const contentType = request.headers.get('content-type') ?? '' const contentType = request.headers.get('content-type') ?? ''
if (!contentType.startsWith('image/') && !contentType.startsWith('video/')) { if (!contentType.startsWith('image/') && !contentType.startsWith('video/')) {
return error(400, 'Invalid content type') return error(400, 'Invalid content type')
}
if (await env.TLDRAW_BUCKET.head(objectName)) {
return error(409, 'Upload already exists')
}
await env.TLDRAW_BUCKET.put(objectName, request.body, {
httpMetadata: request.headers,
})
return { ok: true }
} catch (error) {
console.error('Asset upload failed:', error);
return new Response(`Upload failed: ${(error as Error).message}`, { status: 500 });
} }
if (await env.TLDRAW_BUCKET.head(objectName)) {
return error(409, 'Upload already exists')
}
await env.TLDRAW_BUCKET.put(objectName, request.body, {
httpMetadata: request.headers,
})
return { ok: true }
} }
// when a user downloads an asset, we retrieve it from the bucket. we also cache the response for performance. // when a user downloads an asset, we retrieve it from the bucket. we also cache the response for performance.

View File

@ -25,4 +25,5 @@ new_classes = ["TldrawDurableObject"]
binding = 'TLDRAW_BUCKET' binding = 'TLDRAW_BUCKET'
bucket_name = 'jeffemmett-canvas' bucket_name = 'jeffemmett-canvas'
preview_bucket_name = 'jeffemmett-canvas-preview' preview_bucket_name = 'jeffemmett-canvas-preview'
workers_dev = true workers_dev = true
logpush = true