feat: support file and image attachments (#98)
* upload & view attachments to particles * allow download of attachments
This commit was merged in pull request #98.
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Downscale an image file to a thumbnail and return an object URL.
|
||||
* Returns undefined for non-image files.
|
||||
* Caller is responsible for revoking the URL via URL.revokeObjectURL().
|
||||
*/
|
||||
export async function createImageThumbnail(
|
||||
file: File,
|
||||
maxDim = 200,
|
||||
): Promise<string | undefined> {
|
||||
if (!file.type.startsWith("image/")) return undefined;
|
||||
|
||||
const bitmap = await createImageBitmap(file);
|
||||
const scale = Math.min(1, maxDim / Math.max(bitmap.width, bitmap.height));
|
||||
const w = Math.round(bitmap.width * scale);
|
||||
const h = Math.round(bitmap.height * scale);
|
||||
|
||||
const canvas = new OffscreenCanvas(w, h);
|
||||
const ctx = canvas.getContext("2d")!;
|
||||
ctx.drawImage(bitmap, 0, 0, w, h);
|
||||
bitmap.close();
|
||||
|
||||
const blob = await canvas.convertToBlob({ type: "image/jpeg", quality: 0.7 });
|
||||
return URL.createObjectURL(blob);
|
||||
}
|
||||
Reference in New Issue
Block a user