forked from tldraw/tldraw-sync-cloudflare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiplayerAssetStore.tsx
33 lines (27 loc) · 968 Bytes
/
multiplayerAssetStore.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { TLAssetStore, uniqueId } from 'tldraw'
const WORKER_URL = process.env.TLDRAW_WORKER_URL
// How does our server handle assets like images and videos?
export const multiplayerAssetStore: TLAssetStore = {
// to upload an asset, we...
async upload(_asset, file) {
// ...create a unique name & URL...
const id = uniqueId()
const objectName = `${id}-${file.name}`.replace(/[^a-zA-Z0-9.]/g, '-')
const url = `${WORKER_URL}/uploads/${objectName}`
// ...POST it to out worker to upload it...
const response = await fetch(url, {
method: 'POST',
body: file,
})
if (!response.ok) {
throw new Error(`Failed to upload asset: ${response.statusText}`)
}
// ...and return the URL to be stored with the asset record.
return url
},
// to retrieve an asset, we can just use the same URL. you could customize this to add extra
// auth, or to serve optimized versions / sizes of the asset.
resolve(asset) {
return asset.props.src
},
}