Skip to content

Commit

Permalink
Fix image metadata generation (#7510)
Browse files Browse the repository at this point in the history
* Fix problem where image metadata generation throwed error when provided url started with /@astroimage

* Remove unnecessary changes
  • Loading branch information
Mrowa96 authored Jun 30, 2023
1 parent 9e2426f commit 4256409
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-lions-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/image': patch
---

Fix problem where image metadata generation throwed error when provided url started with /@astroimage
8 changes: 7 additions & 1 deletion packages/integrations/image/src/loaders/squoosh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions packages/integrations/image/src/utils/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ 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) {
return undefined;
}

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,
Expand Down

0 comments on commit 4256409

Please sign in to comment.