Set a default Content Disposition inside an uploader class?

179 views
Skip to first unread message

kevin.so...@webascender.com

unread,
Aug 8, 2019, 12:35:05 PM8/8/19
to Shrine
Is it possible to set a default "content_disposition" header inside an uploader that will retain the original filename but use "attachment" instead of "inline"?

I'm using the S3 storage which defaults to "inline" for all uploads, but for a specific uploader class I want to use the "attachment" disposition header on everything uploaded to S3 via this uploader.

Right now my workaround is to do things like this inside every view where I want to link to an uploaded file:

photo.file_url(response_content_disposition: ContentDisposition.attachment(photo.file.original_filename))

But it's getting cumbersome when I do this in a lot of places for the same file / uploader type across lots of views...

Thanks for any help!

Janko Marohnić

unread,
Aug 8, 2019, 12:42:06 PM8/8/19
to Shrine, kevin.so...@webascender.com
You can use the default_url_options plugin in the uploader you want to have this content disposition header:

class MyUploader < Shrine
  plugin :default_url_options, store: -> (io, options) {
    { response_content_disposition: ContentDisposition.attachment(io.original_filename) }
  }
end

Kind regards,
Janko
--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-shrine/6c680e40-c947-4531-ada0-30a9be796012%40googlegroups.com.

kevin.so...@webascender.com

unread,
Aug 8, 2019, 1:26:38 PM8/8/19
to Shrine
Thanks for the reply, but I'm also using the dynamic_storage and default_storage plugins, which I forgot to mention before, so I don't have static storage names like :store.  Instead of have dynamic storages like:   :store_tenant_a, :store_tenant_b, :store_tenant_c, etc.

Is there a way to use :default_url_options and have it apply to ALL storages?


On Thursday, August 8, 2019 at 12:42:06 PM UTC-4, Janko Marohnić wrote:
You can use the default_url_options plugin in the uploader you want to have this content disposition header:

class MyUploader < Shrine
  plugin :default_url_options, store: -> (io, options) {
    { response_content_disposition: ContentDisposition.attachment(io.original_filename) }
  }
end

Kind regards,
Janko
On 8 Aug 2019, 18:35 +0200, kevin.s...@webascender.com, wrote:
Is it possible to set a default "content_disposition" header inside an uploader that will retain the original filename but use "attachment" instead of "inline"?

I'm using the S3 storage which defaults to "inline" for all uploads, but for a specific uploader class I want to use the "attachment" disposition header on everything uploaded to S3 via this uploader.

Right now my workaround is to do things like this inside every view where I want to link to an uploaded file:

photo.file_url(response_content_disposition: ContentDisposition.attachment(photo.file.original_filename))

But it's getting cumbersome when I do this in a lot of places for the same file / uploader type across lots of views...

Thanks for any help!

--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-...@googlegroups.com.

Janko Marohnić

unread,
Aug 8, 2019, 1:33:57 PM8/8/19
to Shrine, kevin.so...@webascender.com
That's a good point, I was also thinking that this should be possible. I added it to my TODO list for Shrine 3.0, which will be the next version, though it will take a while to get released.

In the meantime, you can override `Shrine::UploadedFile#url` in your uploader:

  class MyUploader < Shrine
    class UploadedFile
      def url(io, **options)
        content_disposition = ContentDisposition.attachment(original_filename)

        super(io, response_content_disposition: content_disposition, **options)
      end
    end
  end

Kind regards,
Janko
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-shrine/45e90401-d9fb-47e3-b4ee-7f9165ee5281%40googlegroups.com.

kevin.so...@webascender.com

unread,
Aug 8, 2019, 1:42:15 PM8/8/19
to Shrine
Thanks for the great support!
I'll give that a try.


On Thursday, August 8, 2019 at 1:33:57 PM UTC-4, Janko Marohnić wrote:
That's a good point, I was also thinking that this should be possible. I added it to my TODO list for Shrine 3.0, which will be the next version, though it will take a while to get released.

In the meantime, you can override `Shrine::UploadedFile#url` in your uploader:

  class MyUploader < Shrine
    class UploadedFile
      def url(io, **options)
        content_disposition = ContentDisposition.attachment(original_filename)

        super(io, response_content_disposition: content_disposition, **options)
      end
    end
  end

Kind regards,
Janko
Reply all
Reply to author
Forward
0 new messages