Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

The uri io adapter should use the content-disposition filename #2210

Closed
wants to merge 5 commits into from

Conversation

netmask
Copy link
Contributor

@netmask netmask commented May 11, 2016

When a URI is passed to the model attachment as the "attachment source"

example:

model.document = "http://examle.com/download_pdf"

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.

model.document = "http://examle.com/download_pdf"
# the response header contains the real name
# Content-disposition: attachment; filename=awesome_pdf_2016.pdf
# model.document.url => /storage/awesome_pdf_2016.pdf instead of download_pdf

- 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' }

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.

@netmask netmask force-pushed the v4.3 branch 2 times, most recently from 7c9d474 to dbc2549 Compare May 11, 2016 16:33
let(:file_name) { "test_document.pdf" }
let(:meta) {
{
"content-disposition" => "attachment; filename=\"#{file_name}\";"

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
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this looks much better 👍

@tute
Copy link
Contributor

tute commented May 12, 2016

Looks good! Thank you.

  1. Left a comment on a potential refactor. What do you think of it?
  2. Can you please add a note to the NEWS file?
  3. If this is backwards compatible, do you think we should only apply it to the master branch, and not the v4.3 one?

Again, thanks for your contribution! 🎉

@netmask
Copy link
Contributor Author

netmask commented May 12, 2016

I think this should be applied also to v4.3 branch because is the latest stable version :)

end

def default_filename
'index.html'

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

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.

@tute
Copy link
Contributor

tute commented May 12, 2016

I think this should be applied also to v4.3 branch because is the latest stable version :)

But this changes behavior, and is thus backwards incompatible.

@tute
Copy link
Contributor

tute commented May 12, 2016

We can consider it a bugfix, though, indeed.

def default_filename
"index.html"
end

Choose a reason for hiding this comment

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

Trailing whitespace detected.

@netmask
Copy link
Contributor Author

netmask commented May 13, 2016

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]
Copy link
Contributor

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?

Copy link
Contributor Author

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

@tute
Copy link
Contributor

tute commented May 13, 2016

Sounds good to me. Left a single comment, this looks almost ready. Thank you! 🎉

tute pushed a commit that referenced this pull request May 16, 2016
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]
@tute
Copy link
Contributor

tute commented May 16, 2016

Merged into master. Thank you! :)

@tute tute closed this May 16, 2016
@tute
Copy link
Contributor

tute commented Jul 1, 2016

Merged into master. Thank you! :)

I meant, merged into v4.3. Do you want to port this fix into master, too? Thank you!

@netmask
Copy link
Contributor Author

netmask commented Jul 1, 2016

sure !!

janko added a commit to janko/down that referenced this pull request Jul 26, 2016
This is generally a better filename than extracting it from URL.

See thoughtbot/paperclip#2210
jcracknell pushed a commit to jcracknell/paperclip that referenced this pull request Feb 10, 2022
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]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants