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

Add encoding options to Resolver::Static#read and Resolver::Dynamic#read #180

Merged
merged 1 commit into from
Sep 4, 2024

Conversation

denzelem
Copy link
Contributor

PR for #176.

Comment on lines +15 to +17
def read(logical_path, options = {})
if asset = load_path.find(logical_path)
asset.content
asset.content(**options)
Copy link
Collaborator

@brenogazzola brenogazzola Feb 23, 2024

Choose a reason for hiding this comment

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

Why is the parameter here named options instead of encoding like in assets.rb and static.rb?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used an option hash to avoid duplicating the default value to this top-level method.

Alternative 1:

def read(logical_path, encoding: 'ASCII-8BIT')
  # ...
  asset.content(encoding: encoding)
  # ...
end

Alternative 2:

def read(logical_path, encoding: nil)
  # ...
  options = {}
  options[:encoding] = encoding if encoding.present?
  asset.content(**options)
  # ...
end

@brenogazzola Wdyt?

@dhh
Copy link
Member

dhh commented May 15, 2024

Don't see how this works when the asset is a binary file? If we're assuming ASCII-8BIT, what happens when you try to read a PNG?

@denzelem
Copy link
Contributor Author

denzelem commented May 22, 2024

Don't see how this works when the asset is a binary file? If we're assuming ASCII-8BIT, what happens when you try to read a PNG?

@dhh Do you think that File.binread(path) is different to File.read(path, encoding: 'ASCII-8BIT')? My assumption was, that binread (rb_ascii8bit_encoding C-Method) reads the bytes with the encoding ASCII-8BIT.

So reading an PNG image should be no breaking change with this PR.

require 'open-uri'
require 'tmpdir'

image_url = 'https://picsum.photos/id/237/200/300'

Dir.mktmpdir do |dir|
  image_path = File.join(dir, 'example.jpg')

  URI.open(image_url) do |image|
    File.open(image_path, 'wb') do |file|
      file.write(image.read)
    end
  end

  puts 'Encoding binread ' + File.binread(image_path).encoding.to_s
  puts 'Encoding read with ASCII-8BIT ' + File.read(image_path, encoding: 'ASCII-8BIT').encoding.to_s
end

exit
Encoding binread ASCII-8BIT
Encoding read with ASCII-8BIT ASCII-8BIT

@theodorton
Copy link
Collaborator

@dhh @brenogazzola I don't see any blockers for merging this - what do you think?

@rafaelfranca rafaelfranca merged commit e7b4c17 into rails:main Sep 4, 2024
4 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.

5 participants