fix vite and asset upload

This commit is contained in:
Jeff Emmett 2024-11-27 11:14:52 +07:00
parent 8a2714662e
commit d89624b801
3 changed files with 5 additions and 11 deletions

View File

@ -1,17 +1,12 @@
import { TLAssetStore, uniqueId } from 'tldraw'
import { WORKER_URL } from '../components/Board'
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,
@ -21,12 +16,9 @@ export const multiplayerAssetStore: TLAssetStore = {
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
},

View File

@ -26,7 +26,7 @@ import { components, uiOverrides } from '@/ui-overrides'
import { useCameraControls } from '@/hooks/useCameraControls'
//const WORKER_URL = `https://jeffemmett-canvas.jeffemmett.workers.dev`
export const WORKER_URL = 'https://jeffemmett-canvas.jeffemmett.workers.dev';
export const WORKER_URL = import.meta.env.VITE_TLDRAW_WORKER_URL || 'https://jeffemmett-canvas.jeffemmett.workers.dev';
const shapeUtils = [ChatBoxShape, VideoChatShape, EmbedShape]
const tools = [ChatBoxTool, VideoChatTool, EmbedTool]; // Array of tools

View File

@ -7,7 +7,9 @@ import { viteStaticCopy } from 'vite-plugin-static-copy';
export default defineConfig({
define: {
'process.env.TLDRAW_WORKER_URL': JSON.stringify(process.env.TLDRAW_WORKER_URL || 'https://jeffemmett-canvas.jeffemmett.workers.dev')
'process.env.TLDRAW_WORKER_URL': JSON.stringify(
process.env.VITE_TLDRAW_WORKER_URL || 'https://jeffemmett-canvas.jeffemmett.workers.dev'
)
},
plugins: [
react(),