Skip to content

Read asset content in binary mode to suppress CRLF conversion on Windows#222

Merged
brenogazzola merged 1 commit into
rails:mainfrom
thloe2:asset-content-binary
Nov 28, 2024
Merged

Read asset content in binary mode to suppress CRLF conversion on Windows#222
brenogazzola merged 1 commit into
rails:mainfrom
thloe2:asset-content-binary

Conversation

@thloe2
Copy link
Copy Markdown
Contributor

@thloe2 thloe2 commented Nov 22, 2024

The first eight bytes of a PNG file always contain the following hex values: 89 50 4e 47 0d 0a 1a 0a.

However, Asset#content reads the file in text mode, and when using Propshaft on Windows, the first eight bytes become 89 50 4e 47 0a 1a 0a 00.

As a result, the browser shows: The image "..." cannot be displayed because it contains errors.

This PR adds the mode: "rb" option to the File.read call to suppress the CRLF -> LF conversion on Windows.

The external encoding is not affected, because it is explicitly specified.

@brenogazzola brenogazzola merged commit b6dab44 into rails:main Nov 28, 2024
@brenogazzola
Copy link
Copy Markdown
Collaborator

It seems reading in binary mode won't interfere with SVG and writing those inline into the HTML.
From a few quick checks, binary seems to be a better choice for image files.

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