2024-07-16 20:28:05 +02:00
|
|
|
import { Size } from '@joplin/utils/types';
|
2024-08-02 15:51:49 +02:00
|
|
|
import { fileUriToPath } from '@joplin/utils/url';
|
|
|
|
import { Image as NativeImage, Platform } from 'react-native';
|
|
|
|
import fileToImage from './fileToImage.web';
|
|
|
|
|
2024-07-16 20:28:05 +02:00
|
|
|
|
|
|
|
const getImageDimensions = async (uri: string): Promise<Size> => {
|
2024-07-26 13:37:55 +02:00
|
|
|
if (uri.startsWith('/')) {
|
|
|
|
uri = `file://${uri}`;
|
|
|
|
}
|
|
|
|
|
2024-08-02 15:51:49 +02:00
|
|
|
// On web, image files are stored using the Origin Private File System and need special
|
|
|
|
// handling.
|
|
|
|
const isFileUrl = uri.startsWith('file://');
|
|
|
|
if (Platform.OS === 'web' && isFileUrl) {
|
|
|
|
const path = isFileUrl ? fileUriToPath(uri) : uri;
|
|
|
|
const image = await fileToImage(path);
|
|
|
|
const size = { width: image.image.width, height: image.image.height };
|
|
|
|
image.free();
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2024-07-16 20:28:05 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
NativeImage.getSize(
|
|
|
|
uri,
|
|
|
|
(width: number, height: number) => {
|
|
|
|
resolve({ width: width, height: height });
|
|
|
|
},
|
|
|
|
(error: unknown) => {
|
|
|
|
reject(error);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export default getImageDimensions;
|