Last active
December 17, 2023 21:04
-
-
Save timm-oh/10c4f06effa536ff32c5d038e0dd57e1 to your computer and use it in GitHub Desktop.
Revisions
-
timm-oh revised this gist
Jun 4, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 = {}) options.merge!(host: ENV['ASSETS_HOST']) if ENV['ASSETS_HOST'].present? # proxy: 'true' allows you to stil have the original functionality while -
timm-oh revised this gist
May 15, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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 -
timm-oh revised this gist
May 15, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 near perfect. Just gets the job done in a simple manner. Inspiration from `https://github.com/rails/rails/pull/34477` -
timm-oh revised this gist
Dec 8, 2019 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. Inspiration from `https://github.com/rails/rails/pull/34477` -
timm-oh revised this gist
Dec 8, 2019 . No changes.There are no files selected for viewing
-
timm-oh revised this gist
Dec 8, 2019 . No changes.There are no files selected for viewing
-
timm-oh revised this gist
Dec 8, 2019 . No changes.There are no files selected for viewing
-
timm-oh revised this gist
Dec 8, 2019 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,5 @@ ### Basic Usage ```ruby # ENV["ASSETS_HOST"] = 'thing.cloudfront.net' class Foo < ApplicationRecord -
timm-oh revised this gist
Dec 8, 2019 . 2 changed files with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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? This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. -
timm-oh revised this gist
Dec 8, 2019 . 3 changed files with 7 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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')) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
timm-oh revised this gist
Dec 6, 2019 . 1 changed file with 18 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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')) ``` -
timm-oh created this gist
Dec 6, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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