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:
parent
9e2426f756
commit
4256409a94
3 changed files with 15 additions and 4 deletions
5
.changeset/stupid-lions-relax.md
Normal file
5
.changeset/stupid-lions-relax.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@astrojs/image': patch
|
||||
---
|
||||
|
||||
Fix problem where image metadata generation throwed error when provided url started with /@astroimage
|
|
@ -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) {
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue