Skip to content

Commit 95b57df

Browse files
authored
Merge pull request #425 from openstax/image-mime-type
Upload image files to S3 with proper mime type
2 parents 69cffe4 + 41111bd commit 95b57df

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

lib/openstax_kramdown.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,16 @@ def handle_html_start_tag(line, &block)
4242
next unless last_el.type == :html_element && last_el.value == 'img' && last_el.attr['src']
4343

4444
uri = URI.parse(ActionController::Base.helpers.strip_tags(last_el.attr['src'].strip))
45-
contents = Net::HTTP.get(uri)
45+
response = Net::HTTP.get_response(uri)
46+
body = response.body
4647

4748
bucket_name = s3_secrets[:uploads_bucket_name]
48-
key = "#{secrets.environment_name}/#{Digest::SHA2.new.update(contents).to_s}#{File.extname(uri.path)}"
49+
key = "#{secrets.environment_name}/#{Digest::SHA2.new.update(body).to_s}#{File.extname(uri.path)}"
4950

5051
s3_client.put_object(
51-
body: StringIO.new(contents),
52+
body: StringIO.new(body),
5253
bucket: bucket_name,
54+
content_type: response['content-type'] || 'application/octet-stream',
5355
key: key
5456
)
5557

0 commit comments

Comments
 (0)