-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
The uri io adapter should use the content-disposition filename #2210
Conversation
- The uri io adapter now seeks for the content-disposition header if this is pressent the value filename is taken instead of the last path segment for the resource file name
@@ -83,9 +90,22 @@ | |||
end | |||
end | |||
|
|||
context "a url with content disposition headers" do | |||
let(:file_name) { 'test_document.pdf' } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
7c9d474
to
dbc2549
Compare
let(:file_name) { "test_document.pdf" } | ||
let(:meta) { | ||
{ | ||
"content-disposition" => "attachment; filename=\"#{file_name}\";" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.
Put a comma after the last item of a multiline hash.
@original_filename = @content.meta["content-disposition"] | ||
.match(/filename="([^"]*)"/)[1] | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we are here we can refactor some code. Suggestion:
def cache_current_values
self.content_type = content_type_from_content || "text/html"
self.original_filename =
filename_from_content_disposition ||
filename_from_path ||
default_filename
@size = @content.size
end
def content_type_from_content
if @content.respond_to?(:content_type)
@content.content_type
end
end
def filename_from_content_disposition
if @content.meta.has_key?("content-disposition")
@original_filename = @content.meta["content-disposition"]
.match(/filename="([^"]*)"/)[1]
end
end
def filename_from_path
@target.path.split("/").last
end
def default_filename
"index.html"
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks much better 👍
Looks good! Thank you.
Again, thanks for your contribution! 🎉 |
I think this should be applied also to v4.3 branch because is the latest stable version :) |
end | ||
|
||
def default_filename | ||
'index.html' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
- Applied the Tute Costa refactor to URI Adapter. - Added entry to the NEWS file.
|
||
private | ||
self.original_filename = filename_from_content_disposition || | ||
filename_from_path || default_filename |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Align the operands of an expression in an assignment spanning multiple lines.
But this changes behavior, and is thus backwards incompatible. |
We can consider it a bugfix, though, indeed. |
def default_filename | ||
"index.html" | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace detected.
Yes , we can considere it a bug, because the URI specifies the resource location, protocol but not the file name itself(?). |
@@ -0,0 +1 @@ | |||
[email protected] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like this is a runaway file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ups yea, its an emacs backup file :P sorry about that
Sounds good to me. Left a single comment, this looks almost ready. Thank you! 🎉 |
The uri io adapter now seeks for the content-disposition header if this is pressent the value filename is taken instead of the last path segment for the resource file name. [fixes #2210]
Merged into master. Thank you! :) |
I meant, merged into v4.3. Do you want to port this fix into master, too? Thank you! |
sure !! |
This is generally a better filename than extracting it from URL. See thoughtbot/paperclip#2210
The uri io adapter now seeks for the content-disposition header if this is pressent the value filename is taken instead of the last path segment for the resource file name. [fixes thoughtbot#2210]
When a URI is passed to the model attachment as the "attachment source"
example:
paperclip will download this resource and set the last element of the path in this case, "download_pdf" as the file name, This PR allows the URI adapter to use the "content-disposition" header if is available and set that value as the original filename which in most cases are the expected one.