mirror of
https://github.com/Neetpone/foalfetch.git
synced 2025-02-14 12:04:23 +01:00
fix: send images with correct content types
This commit is contained in:
parent
b9872ba287
commit
95a65e9a6e
3 changed files with 17 additions and 7 deletions
3
Gemfile
3
Gemfile
|
@ -25,6 +25,9 @@ gem 'model-msearch'
|
||||||
gem 'puma', '~> 5.0'
|
gem 'puma', '~> 5.0'
|
||||||
gem 'sidekiq'
|
gem 'sidekiq'
|
||||||
|
|
||||||
|
# Other stuff
|
||||||
|
gem 'marcel'
|
||||||
|
|
||||||
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
|
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
|
||||||
gem 'tzinfo-data', platforms: %i[ mingw mswin x64_mingw jruby ]
|
gem 'tzinfo-data', platforms: %i[ mingw mswin x64_mingw jruby ]
|
||||||
|
|
||||||
|
|
|
@ -306,6 +306,7 @@ DEPENDENCIES
|
||||||
elasticsearch-model
|
elasticsearch-model
|
||||||
fancy_searchable!
|
fancy_searchable!
|
||||||
kaminari
|
kaminari
|
||||||
|
marcel
|
||||||
model-msearch
|
model-msearch
|
||||||
pg (~> 1.1)
|
pg (~> 1.1)
|
||||||
puma (~> 5.0)
|
puma (~> 5.0)
|
||||||
|
|
|
@ -17,17 +17,23 @@ class ImagesController < ApplicationController
|
||||||
hash = Digest::SHA256.hexdigest(url)
|
hash = Digest::SHA256.hexdigest(url)
|
||||||
path = Rails.root.join('public', 'cached-images', hash + ext)
|
path = Rails.root.join('public', 'cached-images', hash + ext)
|
||||||
our_url = "/cached-images/#{hash}#{ext}"
|
our_url = "/cached-images/#{hash}#{ext}"
|
||||||
|
content_type = nil
|
||||||
|
|
||||||
if File.exist? path
|
if File.exist? path
|
||||||
redirect_to our_url
|
content_type = Marcel::MimeType.for Pathname.new(path)
|
||||||
return
|
else
|
||||||
|
content_type, body = fetch_image uri
|
||||||
|
File.binwrite path, body
|
||||||
end
|
end
|
||||||
|
|
||||||
File.binwrite(
|
send_file path, disposition: :inline, content_type: content_type
|
||||||
path,
|
end
|
||||||
Net::HTTP.get(parsed)
|
|
||||||
)
|
|
||||||
|
|
||||||
redirect_to our_url
|
private
|
||||||
|
|
||||||
|
def fetch_image(uri)
|
||||||
|
response = Net::HTTP.get_response(uri)
|
||||||
|
|
||||||
|
[response['Content-Type'], response.body]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue