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

Let Carrierwave generate urls. Fixes #123 #151 #155

Merged
merged 2 commits into from
Feb 21, 2015
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
6 changes: 3 additions & 3 deletions lib/carrierwave_direct/uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def persisted?
def key
return @key if @key.present?
if present?
self.key = URI.parse(encoded_url).path[1 .. -1] # explicitly set key
self.key = decoded_key # explicitly set key
else
@key = "#{store_dir}/#{guid}/#{FILENAME_WILDCARD}"
end
Expand Down Expand Up @@ -107,8 +107,8 @@ def filename

private

def encoded_url
URI.encode(URI.decode(url), " []+()")
def decoded_key
URI.decode(URI.parse(url).path[1 .. -1])
end

def key_from_file(fname)
Expand Down
9 changes: 3 additions & 6 deletions lib/carrierwave_direct/uploader/direct_url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ module Uploader
module DirectUrl

def direct_fog_url(options = {})
fog_uri = CarrierWave::Storage::Fog::File.new(self, CarrierWave::Storage::Fog.new(self), nil).public_url
if options[:with_path]
uri = URI.parse(fog_uri.chomp('/'))
path = "/#{URI.decode(key)}"
uri.path += URI.escape(path)
fog_uri = uri.to_s
url
else
CarrierWave::Storage::Fog::File.new(self, CarrierWave::Storage::Fog.new(self), nil).public_url
end
fog_uri
end

end
Expand Down
35 changes: 3 additions & 32 deletions spec/uploader/direct_url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,9 @@
end

context ":with_path => true" do

context "#key is set to '#{sample(:path_with_special_chars)}'" do
before { subject.key = sample(:path_with_special_chars) }

it "should return the full url with '/#{URI.escape(sample(:path_with_special_chars))}' as the path" do
direct_fog_url = CarrierWave::Storage::Fog::File.new(
subject, nil, nil
).public_url
expect(subject.direct_fog_url(:with_path => true)).to eq direct_fog_url + "#{URI.escape(sample(:path_with_special_chars))}"
end
end

context "#key is set to '#{sample(:path_with_escaped_chars)}'" do
before { subject.key = sample(:path_with_escaped_chars) }

it "should return the full url with '/#{sample(:path_with_escaped_chars)}' as the path" do
direct_fog_url = CarrierWave::Storage::Fog::File.new(
subject, nil, nil
).public_url
expect(subject.direct_fog_url(:with_path => true)).to eq direct_fog_url + sample(:path_with_escaped_chars)
end
end

context "#key is set to '#{sample(:path)}'" do
before { subject.key = sample(:path) }

it "should return the full url with '/#{sample(:path)}' as the path" do
direct_fog_url = CarrierWave::Storage::Fog::File.new(
subject, nil, nil
).public_url
expect(subject.direct_fog_url(:with_path => true)).to eq direct_fog_url + "#{sample(:path)}"
end
it "should return the full url set by carrierwave" do
allow(subject).to receive(:url).and_return("url")
expect(subject.direct_fog_url(:with_path => true)).to eq "url"
end
end
end
Expand Down
22 changes: 5 additions & 17 deletions spec/uploader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,28 +240,16 @@
end
end

context "and the model's remote #{sample(:mounted_as)} url has whitespace in it" do
context "and the model's remote #{sample(:mounted_as)} url has special characters in it" do
before do
allow(mounted_model).to receive(
"remote_#{mounted_subject.mounted_as}_url"
).and_return("http://anyurl.com/any_path/video_dir/filename 2.avi")
).and_return("http://anyurl.com/any_path/video_dir/filename ()+[]2.avi")
end

it "should be sanitized (whitespace replaced with _)" do
it "should be sanitized (special characters replaced with _)" do
mounted_subject.filename
expect(mounted_subject.key).to match /filename_2.avi$/
end
end

context "and the model's remote url contains escape characters" do
before do
subject.key = nil
allow(subject).to receive(:present?).and_return(:true)
allow(subject).to receive(:url).and_return("http://anyurl.com/any_path/video_dir/filename ()+[]2.avi")
end

it "should be escaped and replaced with non whitespace characters" do
expect(subject.key).to match /filename%20%28%29%2B%5B%5D2.avi/
expect(mounted_subject.key).to match /filename___\+__2.avi$/
end
end

Expand All @@ -273,7 +261,7 @@
end

it "should not double escape already escaped characters" do
expect(subject.key).to match /filename%20%28%29%2B%5B%5D2.avi/
expect(subject.key).to match /filename \(\)\+\[\]2.avi/
end

end
Expand Down