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:
|
case 8:
|
||||||
return { type: 'rotate', numRotations: 3 };
|
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) {
|
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> {
|
export async function metadata(src: URL | string, data?: Buffer): Promise<Metadata | undefined> {
|
||||||
const file = data || (await fs.readFile(src));
|
const file = data || (await fs.readFile(src));
|
||||||
|
const { width, height, type, orientation } = sizeOf(file);
|
||||||
const { width, height, type, orientation } = await sizeOf(file);
|
|
||||||
const isPortrait = (orientation || 0) >= 5;
|
const isPortrait = (orientation || 0) >= 5;
|
||||||
|
|
||||||
if (!width || !height || !type) {
|
if (!width || !height || !type) {
|
||||||
|
@ -19,7 +18,8 @@ export async function metadata(src: URL | string, data?: Buffer): Promise<Metada
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
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,
|
width: isPortrait ? height : width,
|
||||||
height: isPortrait ? width : height,
|
height: isPortrait ? width : height,
|
||||||
format: type as InputFormat,
|
format: type as InputFormat,
|
||||||
|
|
Loading…
Reference in a new issue