0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Fix image metadata generation (#7510)

* Fix problem where image metadata generation throwed error when provided url started with /@astroimage

* Remove unnecessary changes
This commit is contained in:
Paweł Mrowiec 2023-06-30 16:38:23 +02:00 committed by GitHub
parent 9e2426f756
commit 4256409a94
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 4 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/image': patch
---
Fix problem where image metadata generation throwed error when provided url started with /@astroimage

View file

@ -77,7 +77,13 @@ class SquooshService extends BaseSSRService {
case 8:
return { type: 'rotate', numRotations: 3 };
}
} catch {}
} catch {
error({
level: 'info',
prefix: false,
message: red(`Cannot read metadata for ${transform.src}`),
});
}
}
async transform(inputBuffer: Buffer, transform: TransformOptions) {

View file

@ -10,8 +10,7 @@ export interface Metadata extends ImageMetadata {
export async function metadata(src: URL | string, data?: Buffer): Promise<Metadata | undefined> {
const file = data || (await fs.readFile(src));
const { width, height, type, orientation } = await sizeOf(file);
const { width, height, type, orientation } = sizeOf(file);
const isPortrait = (orientation || 0) >= 5;
if (!width || !height || !type) {
@ -19,7 +18,8 @@ export async function metadata(src: URL | string, data?: Buffer): Promise<Metada
}
return {
src: fileURLToPath(src),
// We shouldn't call fileURLToPath function if it starts with /@astroimage/ because it will throw Invalid URL error
src: typeof src === 'string' && /^[\/\\]?@astroimage/.test(src) ? src : fileURLToPath(src),
width: isPortrait ? height : width,
height: isPortrait ? width : height,
format: type as InputFormat,