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

Adds fix for content-lenght value missing for files uploaded via ActionD... #622

Merged
merged 1 commit into from
Dec 2, 2014
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
7 changes: 7 additions & 0 deletions lib/active_fedora/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ def save(*)
payload = behaves_like_io?(content) ? content.read : content
headers = { 'Content-Type' => mime_type }
headers['Content-Disposition'] = "attachment; filename=\"#{@original_name}\"" if @original_name
# Setting the content-length is required until we figure out why Faraday
# is not doing this automatically for files uploaded via ActionDispatch.
headers['Content-Length'] = payload.size.to_s if uploaded_file?(payload)
ldp_source.content = payload
if new_record?
ldp_source.create do |req|
Expand Down Expand Up @@ -292,6 +295,10 @@ def stream(range = nil, &block)

private

def uploaded_file?(payload)
defined?(ActionDispatch::Http::UploadedFile) and payload.instance_of?(ActionDispatch::Http::UploadedFile)
Copy link
Member

Choose a reason for hiding this comment

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

Would it work to handle this based on behavior rather than on class? E.g., responds_to(:size) ? That way, if the object responds to the size method, use it. (Alternatively, that might lead to weird behavior if payload is an Array...)

Copy link
Member Author

Choose a reason for hiding this comment

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

@mjgiarlo So far we have only found that we need to set the Content-Length header for ActionDispatch::Http::UploadedFile objects.

The code works fine for plain Strings or File objects without us forcing the Content-Length header. String and File objects have a size property but we don't need to manually set it since Faraday picks up the correct value at runtime.

Copy link
Member

Choose a reason for hiding this comment

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

OK, then, sounds good!

Copy link
Member

Choose a reason for hiding this comment

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

Please omit the is_ from the method name, the question mark conveys that it is a binary returning method.

end

def local_or_remote_content(ensure_fetch = true)
return @content if new_record?

Expand Down
30 changes: 30 additions & 0 deletions spec/integration/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,36 @@
end
end

context "stand alone operation with UploadedFile" do
before(:all) do
module ActionDispatch
module Http
class UploadedFile

def initialize
@content = StringIO.new("hello world")
end

def read(a, b)
return @content.read(a, b)
end

def size
@content.length
end

end
end
end
end

it "should save" do
subject.content = ActionDispatch::Http::UploadedFile.new
subject.save
expect(subject).not_to be_new_record
end
end

context "when autocreate is true" do
before(:all) do
class MockAFBase < ActiveFedora::Base
Expand Down