mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-22 12:36:24 +08:00
adc003862b
In Edgeless, the image size should be read when converting attachment to image: * fix `size` * fix `xywh`
29 lines
664 B
TypeScript
29 lines
664 B
TypeScript
import DOMPurify from 'dompurify';
|
|
|
|
export function readImageSize(file: File | Blob) {
|
|
return new Promise<{ width: number; height: number }>(resolve => {
|
|
const size = { width: 0, height: 0 };
|
|
if (!file.type.startsWith('image/')) {
|
|
resolve(size);
|
|
return;
|
|
}
|
|
|
|
const img = new Image();
|
|
|
|
img.onload = () => {
|
|
size.width = img.width;
|
|
size.height = img.height;
|
|
URL.revokeObjectURL(img.src);
|
|
resolve(size);
|
|
};
|
|
|
|
img.onerror = () => {
|
|
URL.revokeObjectURL(img.src);
|
|
resolve(size);
|
|
};
|
|
|
|
const sanitizedURL = DOMPurify.sanitize(URL.createObjectURL(file));
|
|
img.src = sanitizedURL;
|
|
});
|
|
}
|