actually upload this the right way, oops

lod-images
Mime Čuvalo 2024-05-09 17:00:05 +01:00
rodzic 0de2782c62
commit b5a08b7024
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: BA84499022AC984D
2 zmienionych plików z 55 dodań i 11 usunięć

Wyświetl plik

@ -4,6 +4,8 @@ import {
MediaHelpers,
TLAsset,
TLAssetId,
TLImageAsset,
downsizeImage,
getHashForString,
uniqueId,
} from 'tldraw'
@ -16,11 +18,6 @@ export async function createAssetFromFile({ file }: { type: 'file'; file: File }
const objectName = `${id}-${file.name}`.replaceAll(/[^a-zA-Z0-9.]/g, '-')
const url = `${UPLOAD_URL}/${objectName}`
await fetch(url, {
method: 'POST',
body: file,
})
const assetId: TLAssetId = AssetRecordType.createId(getHashForString(url))
const isImageType = DEFAULT_ACCEPTED_IMG_TYPE.includes(file.type)
@ -39,14 +36,60 @@ export async function createAssetFromFile({ file }: { type: 'file'; file: File }
} else {
isAnimated = false
}
const scaleOneImage = await downsizeImage(file, size.w, size.h, {
type: file.type,
quality: 0.92,
})
await fetch(url, {
method: 'POST',
body: scaleOneImage,
})
const sources: TLImageAsset['props']['sources'] = [
{
scale: 1,
src: url,
},
]
// Always rescale the image
if (file.type === 'image/jpeg' || file.type === 'image/png') {
const scaleHalfImage = await downsizeImage(file, size.w / 2, size.h / 2, {
type: file.type,
quality: 0.92,
})
const scaleHalfUrl = `${url}-scale0.5`
await fetch(scaleHalfUrl, {
method: 'POST',
body: scaleHalfImage,
})
const scaleQuarterUrl = `${url}-scale0.25`
const scaleQuarterImage = await downsizeImage(file, size.w / 4, size.h / 4, {
type: file.type,
quality: 0.92,
})
await fetch(scaleQuarterUrl, {
method: 'POST',
body: scaleQuarterImage,
})
sources.push(
{
scale: 1 / 2,
src: scaleHalfUrl,
},
{
scale: 1 / 4,
src: scaleQuarterUrl,
}
)
}
props = {
name: file.name,
sources: [
{
scale: 1,
src: url,
},
],
sources,
w: size.w,
h: size.h,
mimeType: file.type,

Wyświetl plik

@ -52,6 +52,7 @@ export default function HostedImagesExample() {
scale: 1,
src: url,
},
// More source sizes can be added here
],
w: size.w,
h: size.h,