Skip to content

Instantly share code, notes, and snippets.

@timm-oh
Last active December 17, 2023 21:04
Show Gist options
  • Select an option

  • Save timm-oh/10c4f06effa536ff32c5d038e0dd57e1 to your computer and use it in GitHub Desktop.

Select an option

Save timm-oh/10c4f06effa536ff32c5d038e0dd57e1 to your computer and use it in GitHub Desktop.

Revisions

  1. timm-oh revised this gist Jun 4, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion application_helper.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # app/helpers/application_helper.rb
    module ApplicationHelper
    # active_storage_item could be a blob or variant object
    def proxy_url(active_storage_item, options)
    def proxy_url(active_storage_item, options = {})
    options.merge!(host: ENV['ASSETS_HOST']) if ENV['ASSETS_HOST'].present?

    # proxy: 'true' allows you to stil have the original functionality while
  2. timm-oh revised this gist May 15, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion application_helper.rb
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ def proxy_url(active_storage_item, options)
    # forwards this param otherwise active storage will always do the default
    # behavior which is a redirect to the service.
    # This is also meant to be intended for items that are always going to be
    # public. If you have files that might be private then theres no point in
    # public. If you have files that might be private then there's no point in
    # caching it with cloudfront.
    # See lib/core_extensions/active_storage/blob/downloader and
    # lib/core_extensions/active_storage/representation/downloader
  3. timm-oh revised this gist May 15, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -14,5 +14,5 @@ proxy_url(foo.image)
    # https://thing.cloudfront.net/rails/active_storage/representations/etc..etc
    proxy_url(foo.image.variant(resize: '100x100'))
    ```
    This is nowhere perfect. Just gets the job done in a simple manner.
    This is nowhere near perfect. Just gets the job done in a simple manner.
    Inspiration from `https://github.com/rails/rails/pull/34477`
  4. timm-oh revised this gist Dec 8, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -14,4 +14,5 @@ proxy_url(foo.image)
    # https://thing.cloudfront.net/rails/active_storage/representations/etc..etc
    proxy_url(foo.image.variant(resize: '100x100'))
    ```
    This is nowhere perfect. Just gets the job done in a simple manner.
    This is nowhere perfect. Just gets the job done in a simple manner.
    Inspiration from `https://github.com/rails/rails/pull/34477`
  5. timm-oh revised this gist Dec 8, 2019. No changes.
  6. timm-oh revised this gist Dec 8, 2019. No changes.
  7. timm-oh revised this gist Dec 8, 2019. No changes.
  8. timm-oh revised this gist Dec 8, 2019. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    ### Basic Usage

    Ensure that you have setup cloudfront similar to this `https://ricostacruz.com/til/rails-and-cloudfront`

    ```ruby
    # ENV["ASSETS_HOST"] = 'thing.cloudfront.net'
    class Foo < ApplicationRecord
  9. timm-oh revised this gist Dec 8, 2019. 2 changed files with 2 additions and 0 deletions.
    1 change: 1 addition & 0 deletions application_helper.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    # app/helpers/application_helper.rb
    module ApplicationHelper
    # active_storage_item could be a blob or variant object
    def proxy_url(active_storage_item, options)
    options.merge!(host: ENV['ASSETS_HOST']) if ENV['ASSETS_HOST'].present?

    1 change: 1 addition & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -16,3 +16,4 @@ proxy_url(foo.image)
    # https://thing.cloudfront.net/rails/active_storage/representations/etc..etc
    proxy_url(foo.image.variant(resize: '100x100'))
    ```
    This is nowhere perfect. Just gets the job done in a simple manner.
  10. timm-oh revised this gist Dec 8, 2019. 3 changed files with 7 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions application_helper.rb
    Original file line number Diff line number Diff line change
    @@ -5,8 +5,11 @@ def proxy_url(active_storage_item, options)

    # proxy: 'true' allows you to stil have the original functionality while
    # being able to proxy through a CDN. You've got to ensure that your CDN
    # forwards this param otherwise it will always do the default
    # behavior which is a redirect.
    # forwards this param otherwise active storage will always do the default
    # behavior which is a redirect to the service.
    # This is also meant to be intended for items that are always going to be
    # public. If you have files that might be private then theres no point in
    # caching it with cloudfront.
    # See lib/core_extensions/active_storage/blob/downloader and
    # lib/core_extensions/active_storage/representation/downloader
    polymorphic_url(active_storage_item, options.merge(proxy: 'true'))
    1 change: 1 addition & 0 deletions blob_downloader.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    # lib/core_extensions/active_storage/blob/downloader.rb
    # had to make the file a different name because reasons ¯\_(ツ)_/¯
    module CoreExtensions
    module ActiveStorage
    module Blob
    1 change: 1 addition & 0 deletions representation_downloader.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    # lib/core_extensions/active_storage/representation/downloader.rb
    # had to make the file a different name because reasons ¯\_(ツ)_/¯
    module CoreExtensions
    module ActiveStorage
    module Representation
  11. timm-oh revised this gist Dec 6, 2019. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    ### Basic Usage

    Ensure that you have setup cloudfront similar to this `https://ricostacruz.com/til/rails-and-cloudfront`

    ```ruby
    # ENV["ASSETS_HOST"] = 'thing.cloudfront.net'
    class Foo < ApplicationRecord
    has_one_attached :thing
    end

    foo = Foo.first

    # https://thing.cloudfront.net/rails/active_storage/blobs/etc..etc
    proxy_url(foo.image)

    # https://thing.cloudfront.net/rails/active_storage/representations/etc..etc
    proxy_url(foo.image.variant(resize: '100x100'))
    ```
  12. timm-oh created this gist Dec 6, 2019.
    14 changes: 14 additions & 0 deletions application_helper.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    # app/helpers/application_helper.rb
    module ApplicationHelper
    def proxy_url(active_storage_item, options)
    options.merge!(host: ENV['ASSETS_HOST']) if ENV['ASSETS_HOST'].present?

    # proxy: 'true' allows you to stil have the original functionality while
    # being able to proxy through a CDN. You've got to ensure that your CDN
    # forwards this param otherwise it will always do the default
    # behavior which is a redirect.
    # See lib/core_extensions/active_storage/blob/downloader and
    # lib/core_extensions/active_storage/representation/downloader
    polymorphic_url(active_storage_item, options.merge(proxy: 'true'))
    end
    end
    20 changes: 20 additions & 0 deletions blob_downloader.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    # lib/core_extensions/active_storage/blob/downloader.rb
    module CoreExtensions
    module ActiveStorage
    module Blob
    module Downloader
    include CoreExtensions::ActiveStorage::Headers

    def show
    return super unless params[:proxy] == 'true'
    set_headers(@blob)
    @blob.download do |chunk|
    response.stream.write(chunk)
    end
    ensure
    response.stream.close
    end
    end
    end
    end
    end
    6 changes: 6 additions & 0 deletions core_extentions.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    # config/initializers/core_extentions.rb
    Dir[File.expand_path(File.join(File.dirname(File.absolute_path(__FILE__)), 'lib/core_extensions')) + "/**/*.rb"].each do |file|
    require file
    end
    ActiveStorage::RepresentationsController.prepend CoreExtensions::ActiveStorage::Representation::Downloader
    ActiveStorage::BlobsController.prepend CoreExtensions::ActiveStorage::Blob::Downloader
    22 changes: 22 additions & 0 deletions headers.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    # lib/core_extensions/active_storage/headers.rb
    module CoreExtensions
    module ActiveStorage
    module Headers
    extend ActiveSupport::Concern

    private

    def set_headers(blob)
    # Hard coded this to 365, simply because thats what I want tbh
    expires_in 365.days, public: true

    response.headers["Content-Type"] = blob.content_type
    # Commented this out because in Rails 5.2.3 this isn't a thing
    # response.headers["Content-Disposition"] = ActionDispatch::Http::ContentDisposition.format(
    # disposition: params[:disposition] || "inline",
    # filename: blob.filename.sanitized
    # )
    end
    end
    end
    end
    21 changes: 21 additions & 0 deletions representation_downloader.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    # lib/core_extensions/active_storage/representation/downloader.rb
    module CoreExtensions
    module ActiveStorage
    module Representation
    module Downloader
    include CoreExtensions::ActiveStorage::Headers

    def show
    return super unless params[:proxy] == 'true'
    set_headers(@blob)
    variant = @blob.representation(params[:variation_key]).processed
    @blob.service.download(variant.key) do |chunk|
    response.stream.write(chunk)
    end
    ensure
    response.stream.close
    end
    end
    end
    end
    end