diff options
Diffstat (limited to 'activestorage')
-rw-r--r-- | activestorage/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activestorage/app/models/active_storage/blob.rb | 4 | ||||
-rw-r--r-- | activestorage/config/routes.rb | 25 | ||||
-rw-r--r-- | activestorage/lib/active_storage.rb | 1 | ||||
-rw-r--r-- | activestorage/lib/active_storage/engine.rb | 1 | ||||
-rw-r--r-- | activestorage/test/models/filename_test.rb | 4 | ||||
-rw-r--r-- | activestorage/test/service/shared_service_tests.rb | 2 |
7 files changed, 26 insertions, 15 deletions
diff --git a/activestorage/CHANGELOG.md b/activestorage/CHANGELOG.md index 92e300a440..f4e2826dc6 100644 --- a/activestorage/CHANGELOG.md +++ b/activestorage/CHANGELOG.md @@ -1,3 +1,7 @@ +* Add `ActiveStorage.routes_prefix` for configuring generated routes. + + *Chris Bisnett* + * `ActiveStorage::Service::AzureStorageService` only handles specifically relevant types of `Azure::Core::Http::HTTPError`. It previously obscured other types of `HTTPError`, which is the azure-storage gem’s catch-all diff --git a/activestorage/app/models/active_storage/blob.rb b/activestorage/app/models/active_storage/blob.rb index e7f2615b0f..53aa9f0237 100644 --- a/activestorage/app/models/active_storage/blob.rb +++ b/activestorage/app/models/active_storage/blob.rb @@ -35,6 +35,10 @@ class ActiveStorage::Blob < ActiveRecord::Base scope :unattached, -> { left_joins(:attachments).where(ActiveStorage::Attachment.table_name => { blob_id: nil }) } + before_destroy(prepend: true) do + raise ActiveRecord::InvalidForeignKey if attachments.exists? + end + class << self # You can used the signed ID of a blob to refer to it on the client side without fear of tampering. # This is particularly helpful for direct uploads where the client-side needs to refer to the blob diff --git a/activestorage/config/routes.rb b/activestorage/config/routes.rb index 20d19f334a..3af7361cff 100644 --- a/activestorage/config/routes.rb +++ b/activestorage/config/routes.rb @@ -1,17 +1,15 @@ # frozen_string_literal: true Rails.application.routes.draw do - get "/rails/active_storage/blobs/:signed_id/*filename" => "active_storage/blobs#show", as: :rails_service_blob - - direct :rails_blob do |blob, options| - route_for(:rails_service_blob, blob.signed_id, blob.filename, options) - end - - resolve("ActiveStorage::Blob") { |blob, options| route_for(:rails_blob, blob, options) } - resolve("ActiveStorage::Attachment") { |attachment, options| route_for(:rails_blob, attachment.blob, options) } + scope ActiveStorage.routes_prefix do + get "/blobs/:signed_id/*filename" => "active_storage/blobs#show", as: :rails_service_blob + get "/representations/:signed_blob_id/:variation_key/*filename" => "active_storage/representations#show", as: :rails_blob_representation - get "/rails/active_storage/representations/:signed_blob_id/:variation_key/*filename" => "active_storage/representations#show", as: :rails_blob_representation + get "/disk/:encoded_key/*filename" => "active_storage/disk#show", as: :rails_disk_service + put "/disk/:encoded_token" => "active_storage/disk#update", as: :update_rails_disk_service + post "/direct_uploads" => "active_storage/direct_uploads#create", as: :rails_direct_uploads + end direct :rails_representation do |representation, options| signed_blob_id = representation.blob.signed_id @@ -25,7 +23,10 @@ Rails.application.routes.draw do resolve("ActiveStorage::Preview") { |preview, options| route_for(:rails_representation, preview, options) } - get "/rails/active_storage/disk/:encoded_key/*filename" => "active_storage/disk#show", as: :rails_disk_service - put "/rails/active_storage/disk/:encoded_token" => "active_storage/disk#update", as: :update_rails_disk_service - post "/rails/active_storage/direct_uploads" => "active_storage/direct_uploads#create", as: :rails_direct_uploads + direct :rails_blob do |blob, options| + route_for(:rails_service_blob, blob.signed_id, blob.filename, options) + end + + resolve("ActiveStorage::Blob") { |blob, options| route_for(:rails_blob, blob, options) } + resolve("ActiveStorage::Attachment") { |attachment, options| route_for(:rails_blob, attachment.blob, options) } end diff --git a/activestorage/lib/active_storage.rb b/activestorage/lib/active_storage.rb index d3e3a2f49b..a94ef626f2 100644 --- a/activestorage/lib/active_storage.rb +++ b/activestorage/lib/active_storage.rb @@ -50,6 +50,7 @@ module ActiveStorage mattr_accessor :variable_content_types, default: [] mattr_accessor :content_types_to_serve_as_binary, default: [] mattr_accessor :service_urls_expire_in, default: 5.minutes + mattr_accessor :routes_prefix, default: "/rails/active_storage" module Transformers extend ActiveSupport::Autoload diff --git a/activestorage/lib/active_storage/engine.rb b/activestorage/lib/active_storage/engine.rb index 9d6a27eabe..7eb93b5e16 100644 --- a/activestorage/lib/active_storage/engine.rb +++ b/activestorage/lib/active_storage/engine.rb @@ -51,6 +51,7 @@ module ActiveStorage ActiveStorage.previewers = app.config.active_storage.previewers || [] ActiveStorage.analyzers = app.config.active_storage.analyzers || [] ActiveStorage.paths = app.config.active_storage.paths || {} + ActiveStorage.routes_prefix = app.config.active_storage.routes_prefix || "/rails/active_storage" ActiveStorage.variable_content_types = app.config.active_storage.variable_content_types || [] ActiveStorage.content_types_to_serve_as_binary = app.config.active_storage.content_types_to_serve_as_binary || [] diff --git a/activestorage/test/models/filename_test.rb b/activestorage/test/models/filename_test.rb index 88405e41c0..715116309f 100644 --- a/activestorage/test/models/filename_test.rb +++ b/activestorage/test/models/filename_test.rb @@ -30,8 +30,8 @@ class ActiveStorage::FilenameTest < ActiveSupport::TestCase end test "sanitize transcodes to valid UTF-8" do - { "\xF6".dup.force_encoding(Encoding::ISO8859_1) => "ö", - "\xC3".dup.force_encoding(Encoding::ISO8859_1) => "Ã", + { (+"\xF6").force_encoding(Encoding::ISO8859_1) => "ö", + (+"\xC3").force_encoding(Encoding::ISO8859_1) => "Ã", "\xAD" => "�", "\xCF" => "�", "\x00" => "", diff --git a/activestorage/test/service/shared_service_tests.rb b/activestorage/test/service/shared_service_tests.rb index 58f189af2b..8a8d639af7 100644 --- a/activestorage/test/service/shared_service_tests.rb +++ b/activestorage/test/service/shared_service_tests.rb @@ -6,7 +6,7 @@ require "active_support/core_ext/securerandom" module ActiveStorage::Service::SharedServiceTests extend ActiveSupport::Concern - FIXTURE_DATA = "\211PNG\r\n\032\n\000\000\000\rIHDR\000\000\000\020\000\000\000\020\001\003\000\000\000%=m\"\000\000\000\006PLTE\000\000\000\377\377\377\245\331\237\335\000\000\0003IDATx\234c\370\377\237\341\377_\206\377\237\031\016\2603\334?\314p\1772\303\315\315\f7\215\031\356\024\203\320\275\317\f\367\201R\314\f\017\300\350\377\177\000Q\206\027(\316]\233P\000\000\000\000IEND\256B`\202".dup.force_encoding(Encoding::BINARY) + FIXTURE_DATA = (+"\211PNG\r\n\032\n\000\000\000\rIHDR\000\000\000\020\000\000\000\020\001\003\000\000\000%=m\"\000\000\000\006PLTE\000\000\000\377\377\377\245\331\237\335\000\000\0003IDATx\234c\370\377\237\341\377_\206\377\237\031\016\2603\334?\314p\1772\303\315\315\f7\215\031\356\024\203\320\275\317\f\367\201R\314\f\017\300\350\377\177\000Q\206\027(\316]\233P\000\000\000\000IEND\256B`\202").force_encoding(Encoding::BINARY) included do setup do |