Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update openimg to v0.6 #957

Merged

Conversation

andrelandgraf
Copy link
Contributor

@andrelandgraf andrelandgraf commented Mar 2, 2025

Update openimg to v0.6

Details

The new version of openimg now correctly includes the content-type and content-length headers in all cases. This ensures that saved images always have the correct file extension attached by the browser. The update also adds a metadata.json file to the cache if you want to check that out!

Background

I don't want to load the image into memory at once but stream it. However, that makes retrieving the image metadata tricky. The source of truth is sharp, but retrieving the metadata of an image from sharp while streaming it directly out to the client is tricky. HTTP headers must be set before the body starts streaming, but sharp needs to process parts of the image before the metadata is available. I was finally able to figure this out with a PassThrough stream that buffers chunks until the metadata is available and only then starts streaming it out to the client (relevant changes here). A new metadata.json file now stores image metadata for cached files. This is inspired by mjackson's local-file-storage! I tested it out on allthingsweb, and things seem to work. :)

Before

Added jpeg extension to image on download even though real content-type was png.

Screenshot 2025-03-01 at 11 41 09 PM

After

Screenshot 2025-03-01 at 11 42 25 PM

Test Plan

  • Download user profile data as JSON
  • Click on image link in JSON data
  • Download/save image from browser
  • Verify that the correct file extension is added

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@kentcdodds kentcdodds merged commit 468c5e5 into epicweb-dev:main Mar 2, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants