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

Headers for download by url #2006

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/carrierwave/mount.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ def remote_#{column}_url=(url)
_mounter(:#{column}).remote_urls = [url]
end

def remote_#{column}_request_header=(header)
_mounter(:#{column}).remote_request_headers = [header]
end

def write_#{column}_identifier
return if frozen?
mounter = _mounter(:#{column})
Expand Down Expand Up @@ -316,6 +320,10 @@ def remote_#{column}_urls=(urls)
_mounter(:#{column}).remote_urls = urls
end

def remote_#{column}_request_headers=(headers)
_mounter(:#{column}).remote_request_headers = headers
end

def write_#{column}_identifier
return if frozen?
mounter = _mounter(:#{column})
Expand Down
9 changes: 5 additions & 4 deletions lib/carrierwave/mounter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ module CarrierWave
# this is an internal class, used by CarrierWave::Mount so that
# we don't pollute the model with a lot of methods.
class Mounter #:nodoc:
attr_reader :column, :record, :remote_urls, :integrity_error, :processing_error, :download_error
attr_accessor :remove
attr_reader :column, :record, :remote_urls, :integrity_error,
:processing_error, :download_error
attr_accessor :remove, :remote_request_headers

def initialize(record, column, options={})
@record = record
Expand Down Expand Up @@ -75,9 +76,9 @@ def remote_urls=(urls)
@download_error = nil
@integrity_error = nil

@uploaders = urls.map do |url|
@uploaders = urls.zip(remote_request_headers || []).map do |url, header|
uploader = blank_uploader
uploader.download!(url)
uploader.download!(url, header || {})
uploader
end

Expand Down
13 changes: 9 additions & 4 deletions lib/carrierwave/uploader/download.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ module Download
include CarrierWave::Uploader::Cache

class RemoteFile
def initialize(uri)
def initialize(uri, remote_headers = {})
@uri = uri
@remote_headers = remote_headers
end

def original_filename
Expand All @@ -35,7 +36,10 @@ def http?

def file
if @file.blank?
@file = Kernel.open(@uri.to_s, "User-Agent" => "CarrierWave/#{CarrierWave::VERSION}")
headers = @remote_headers.
reverse_merge('User-Agent' => "CarrierWave/#{CarrierWave::VERSION}")

@file = Kernel.open(@uri.to_s, headers)
@file = @file.is_a?(String) ? StringIO.new(@file) : @file
end
@file
Expand All @@ -62,10 +66,11 @@ def method_missing(*args, &block)
# === Parameters
#
# [url (String)] The URL where the remote file is stored
# [remote_headers (Hash)] Request headers
#
def download!(uri)
def download!(uri, remote_headers = {})
processed_uri = process_uri(uri)
file = RemoteFile.new(processed_uri)
file = RemoteFile.new(processed_uri, remote_headers)
raise CarrierWave::DownloadError, "trying to download a file which is not served over HTTP" unless file.http?
cache!(file)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/mount_multiple_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ def monkey

before do
uploader.class_eval do
def download! uri
def download! uri, headers = {}
raise CarrierWave::DownloadError
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/mount_single_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ def monkey

it "should raise an error if the image fails to be processed" do
@uploader.class_eval do
def download! uri
def download! uri, headers = {}
raise CarrierWave::DownloadError
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/orm/activerecord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ def monkey
context 'when validating download' do
before do
@uploader.class_eval do
def download! file
def download! file, headers = {}
raise CarrierWave::DownloadError
end
end
Expand Down Expand Up @@ -1209,7 +1209,7 @@ def monkey
context 'when validating download' do
before do
@uploader.class_eval do
def download! file
def download! file, headers = {}
raise CarrierWave::DownloadError
end
end
Expand Down
20 changes: 20 additions & 0 deletions spec/uploader/download_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
let(:url) { base_url + "/test.jpg" }
let(:test_file) { File.read(file_path(test_file_name)) }
let(:test_file_name) { "test.jpg" }
let(:authentication_headers) do
{
'Accept'=>'*/*',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'User-Agent'=>"CarrierWave/#{CarrierWave::VERSION}",
'Authorization'=>'Bearer QWE'
}
end

after { FileUtils.rm_rf(public_path) }

Expand All @@ -32,6 +40,10 @@

stub_request(:get, "www.example.com/missing.jpg").
to_return(status: 404)

stub_request(:get, "www.example.com/authorization_required.jpg").
with(:headers => authentication_headers).
to_return(body: test_file)
end

context "when a file was downloaded" do
Expand Down Expand Up @@ -83,6 +95,14 @@
end
end

context 'with request headers' do
it 'pass custom headers to request' do
auth_required_url = 'http://www.example.com/authorization_required.jpg'
uploader.download!(auth_required_url, { 'Authorization' => 'Bearer QWE' })
expect(uploader.url).to eq("/uploads/tmp/#{cache_id}/authorization_required.jpg")
end
end

it "raises an error when trying to download a local file" do
expect { uploader.download!('/etc/passwd') }.to raise_error(CarrierWave::DownloadError)
end
Expand Down