From 90a8e0698db59d2f4ba137ea11c21783d6f9c66a Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 31 Jul 2017 15:57:37 -0500 Subject: Use config/storage.yml as part of the skeleton and loading --- activestorage/lib/active_storage/engine.rb | 2 +- activestorage/lib/active_storage/service.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'activestorage/lib') diff --git a/activestorage/lib/active_storage/engine.rb b/activestorage/lib/active_storage/engine.rb index 71861b84ae..d1f05986ba 100644 --- a/activestorage/lib/active_storage/engine.rb +++ b/activestorage/lib/active_storage/engine.rb @@ -31,7 +31,7 @@ module ActiveStorage initializer "active_storage.services" do config.after_initialize do |app| if config_choice = app.config.active_storage.service - config_file = Pathname.new(Rails.root.join("config/storage_services.yml")) + config_file = Pathname.new(Rails.root.join("config/storage.yml")) raise("Couldn't find Active Storage configuration in #{config_file}") unless config_file.exist? require "yaml" diff --git a/activestorage/lib/active_storage/service.rb b/activestorage/lib/active_storage/service.rb index b648c51823..7559fd0e2b 100644 --- a/activestorage/lib/active_storage/service.rb +++ b/activestorage/lib/active_storage/service.rb @@ -10,7 +10,7 @@ require "active_storage/log_subscriber" # * +Mirror+, to be able to use several services to manage attachments. # # Inside a Rails application, you can set-up your services through the -# generated config/storage_services.yml file and reference one +# generated config/storage.yml file and reference one # of the aforementioned constant under the +service+ key. For example: # # local: -- cgit v1.2.3 From a1a363a8aa0f0717ef329eba4a5fe57705fb6b2f Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 31 Jul 2017 15:57:56 -0500 Subject: Follow form from actioncable --- activestorage/lib/active_storage/engine.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'activestorage/lib') diff --git a/activestorage/lib/active_storage/engine.rb b/activestorage/lib/active_storage/engine.rb index d1f05986ba..89e61163f5 100644 --- a/activestorage/lib/active_storage/engine.rb +++ b/activestorage/lib/active_storage/engine.rb @@ -1,4 +1,7 @@ -require "rails/engine" +# frozen_string_literal: true + +require "rails" +require "active_storage" module ActiveStorage class Engine < Rails::Engine # :nodoc: -- cgit v1.2.3 From bb69b3e047539cc1b6c10cdf1d3bad7479a6b0d8 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 31 Jul 2017 16:12:39 -0500 Subject: Directory and config installation now done by app_generator --- activestorage/lib/tasks/activestorage.rake | 7 ------- 1 file changed, 7 deletions(-) (limited to 'activestorage/lib') diff --git a/activestorage/lib/tasks/activestorage.rake b/activestorage/lib/tasks/activestorage.rake index 2fba4eaa8d..1d386e67df 100644 --- a/activestorage/lib/tasks/activestorage.rake +++ b/activestorage/lib/tasks/activestorage.rake @@ -3,13 +3,6 @@ require "fileutils" namespace :activestorage do desc "Copy over the migration needed to the application" task :install do - FileUtils.mkdir_p Rails.root.join("storage") - FileUtils.mkdir_p Rails.root.join("tmp/storage") - puts "Made storage and tmp/storage directories for development and testing" - - FileUtils.cp File.expand_path("../../../config/storage_services.yml", __FILE__), Rails.root.join("config") - puts "Copied default configuration to config/storage_services.yml" - migration_file_path = "db/migrate/#{Time.now.utc.strftime("%Y%m%d%H%M%S")}_active_storage_create_tables.rb" FileUtils.mkdir_p Rails.root.join("db/migrate") FileUtils.cp File.expand_path("../../active_storage/migration.rb", __FILE__), Rails.root.join(migration_file_path) -- cgit v1.2.3 From 1ea49d68ff50b39aaadad58b5d4dde1b53f79006 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 1 Aug 2017 16:50:53 -0500 Subject: Follow same format as action_cable --- activestorage/lib/active_storage.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'activestorage/lib') diff --git a/activestorage/lib/active_storage.rb b/activestorage/lib/active_storage.rb index 4466aea1ee..412f08e8f5 100644 --- a/activestorage/lib/active_storage.rb +++ b/activestorage/lib/active_storage.rb @@ -22,7 +22,9 @@ #++ require "active_record" -require "active_storage/engine" +require "active_support" +require "active_support/rails" +require_relative "active_storage/version" module ActiveStorage extend ActiveSupport::Autoload -- cgit v1.2.3 From b334ac80cf07d4ce6ba20b3ee45c3e58711f9a91 Mon Sep 17 00:00:00 2001 From: Rolandas Barysas Date: Wed, 2 Aug 2017 10:28:51 +0300 Subject: Add Azure to the available services list --- activestorage/lib/active_storage/service.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activestorage/lib') diff --git a/activestorage/lib/active_storage/service.rb b/activestorage/lib/active_storage/service.rb index 7559fd0e2b..f6b4877759 100644 --- a/activestorage/lib/active_storage/service.rb +++ b/activestorage/lib/active_storage/service.rb @@ -7,6 +7,7 @@ require "active_storage/log_subscriber" # * +Disk+, to manage attachments saved directly on the hard drive. # * +GCS+, to manage attachments through Google Cloud Storage. # * +S3+, to manage attachments through Amazon S3. +# * +Azure+, to manage attachments through Microsoft Azure Storage. # * +Mirror+, to be able to use several services to manage attachments. # # Inside a Rails application, you can set-up your services through the -- cgit v1.2.3 From 624b6c210fee56fb8ef98a7a96f588d1bea8d124 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 3 Aug 2017 10:01:35 -0500 Subject: Can't initialize a message verifier without a key base --- activestorage/lib/active_storage/engine.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'activestorage/lib') diff --git a/activestorage/lib/active_storage/engine.rb b/activestorage/lib/active_storage/engine.rb index 89e61163f5..1d345920fa 100644 --- a/activestorage/lib/active_storage/engine.rb +++ b/activestorage/lib/active_storage/engine.rb @@ -27,7 +27,9 @@ module ActiveStorage initializer "active_storage.verifier" do config.after_initialize do |app| - ActiveStorage.verifier = app.message_verifier("ActiveStorage") + if app.config.secret_key_base.present? + ActiveStorage.verifier = app.message_verifier("ActiveStorage") + end end end -- cgit v1.2.3 From f601a01b2cb4c6c0b9f6397874c2998810993bed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 3 Aug 2017 16:40:37 -0400 Subject: Do not eager load ActiveRecord::Base Everything inside the app directory of a engine is autoload/eager loaded automatically so we don't need to require them. --- activestorage/lib/active_storage/attached.rb | 3 --- 1 file changed, 3 deletions(-) (limited to 'activestorage/lib') diff --git a/activestorage/lib/active_storage/attached.rb b/activestorage/lib/active_storage/attached.rb index 4644d74bcc..f9c8929c5b 100644 --- a/activestorage/lib/active_storage/attached.rb +++ b/activestorage/lib/active_storage/attached.rb @@ -1,6 +1,3 @@ -require "active_storage/blob" -require "active_storage/attachment" - require "action_dispatch/http/upload" require "active_support/core_ext/module/delegation" -- cgit v1.2.3 From 5bfff1de54de363d0982e63d15e70f1568744238 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 3 Aug 2017 17:02:43 -0400 Subject: We need to require the top file before individual one --- activestorage/lib/active_storage/attached.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activestorage/lib') diff --git a/activestorage/lib/active_storage/attached.rb b/activestorage/lib/active_storage/attached.rb index f9c8929c5b..2dbf841864 100644 --- a/activestorage/lib/active_storage/attached.rb +++ b/activestorage/lib/active_storage/attached.rb @@ -1,3 +1,4 @@ +require "action_dispatch" require "action_dispatch/http/upload" require "active_support/core_ext/module/delegation" -- cgit v1.2.3 From b76f4e47e842d4f6e4519046461e1a3da99ccf82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 3 Aug 2017 17:32:59 -0400 Subject: Fix all rubocop violations --- activestorage/lib/active_storage/service/disk_service.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'activestorage/lib') diff --git a/activestorage/lib/active_storage/service/disk_service.rb b/activestorage/lib/active_storage/service/disk_service.rb index 35b0909297..c75c1caabe 100644 --- a/activestorage/lib/active_storage/service/disk_service.rb +++ b/activestorage/lib/active_storage/service/disk_service.rb @@ -81,8 +81,10 @@ class ActiveStorage::Service::DiskService < ActiveStorage::Service content_length: content_length, checksum: checksum }, - expires_in: expires_in, - purpose: :blob_token + { + expires_in: expires_in, + purpose: :blob_token + } ) generated_url = -- cgit v1.2.3 From 77be872819d6e9fd63f4253398dbfd6990166a7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 3 Aug 2017 17:42:35 -0400 Subject: Do not change the entire codebase style only because of active_storage We are already removing the braces around hash parameters in the last argument in other places so we should not change the entire codebase because of two places. --- activestorage/lib/active_storage/service/disk_service.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'activestorage/lib') diff --git a/activestorage/lib/active_storage/service/disk_service.rb b/activestorage/lib/active_storage/service/disk_service.rb index c75c1caabe..35b0909297 100644 --- a/activestorage/lib/active_storage/service/disk_service.rb +++ b/activestorage/lib/active_storage/service/disk_service.rb @@ -81,10 +81,8 @@ class ActiveStorage::Service::DiskService < ActiveStorage::Service content_length: content_length, checksum: checksum }, - { - expires_in: expires_in, - purpose: :blob_token - } + expires_in: expires_in, + purpose: :blob_token ) generated_url = -- cgit v1.2.3 From 3179f089be4f631b9c0f8b431567992164f2bdb4 Mon Sep 17 00:00:00 2001 From: Dino Maric Date: Sat, 5 Aug 2017 00:57:02 +0200 Subject: Rename Azure to AzureStorage (#30057) --- activestorage/lib/active_storage/service.rb | 2 +- .../lib/active_storage/service/azure_service.rb | 115 --------------------- .../service/azure_storage_service.rb | 115 +++++++++++++++++++++ .../lib/active_storage/service/configurator.rb | 2 +- 4 files changed, 117 insertions(+), 117 deletions(-) delete mode 100644 activestorage/lib/active_storage/service/azure_service.rb create mode 100644 activestorage/lib/active_storage/service/azure_storage_service.rb (limited to 'activestorage/lib') diff --git a/activestorage/lib/active_storage/service.rb b/activestorage/lib/active_storage/service.rb index f6b4877759..4223295ed8 100644 --- a/activestorage/lib/active_storage/service.rb +++ b/activestorage/lib/active_storage/service.rb @@ -7,7 +7,7 @@ require "active_storage/log_subscriber" # * +Disk+, to manage attachments saved directly on the hard drive. # * +GCS+, to manage attachments through Google Cloud Storage. # * +S3+, to manage attachments through Amazon S3. -# * +Azure+, to manage attachments through Microsoft Azure Storage. +# * +AzureStorage+, to manage attachments through Microsoft Azure Storage. # * +Mirror+, to be able to use several services to manage attachments. # # Inside a Rails application, you can set-up your services through the diff --git a/activestorage/lib/active_storage/service/azure_service.rb b/activestorage/lib/active_storage/service/azure_service.rb deleted file mode 100644 index a505b9a0ee..0000000000 --- a/activestorage/lib/active_storage/service/azure_service.rb +++ /dev/null @@ -1,115 +0,0 @@ -require "active_support/core_ext/numeric/bytes" -require "azure/storage" -require "azure/storage/core/auth/shared_access_signature" - -# Wraps the Microsoft Azure Storage Blob Service as a Active Storage service. -# See `ActiveStorage::Service` for the generic API documentation that applies to all services. -class ActiveStorage::Service::AzureService < ActiveStorage::Service - attr_reader :client, :path, :blobs, :container, :signer - - def initialize(path:, storage_account_name:, storage_access_key:, container:) - @client = Azure::Storage::Client.create(storage_account_name: storage_account_name, storage_access_key: storage_access_key) - @signer = Azure::Storage::Core::Auth::SharedAccessSignature.new(storage_account_name, storage_access_key) - @blobs = client.blob_client - @container = container - @path = path - end - - def upload(key, io, checksum: nil) - instrument :upload, key, checksum: checksum do - begin - blobs.create_block_blob(container, key, io, content_md5: checksum) - rescue Azure::Core::Http::HTTPError => e - raise ActiveStorage::IntegrityError - end - end - end - - def download(key) - if block_given? - instrument :streaming_download, key do - stream(key, &block) - end - else - instrument :download, key do - _, io = blobs.get_blob(container, key) - io.force_encoding(Encoding::BINARY) - end - end - end - - def delete(key) - instrument :delete, key do - begin - blobs.delete_blob(container, key) - rescue Azure::Core::Http::HTTPError - false - end - end - end - - def exist?(key) - instrument :exist, key do |payload| - answer = blob_for(key).present? - payload[:exist] = answer - answer - end - end - - def url(key, expires_in:, disposition:, filename:) - instrument :url, key do |payload| - base_url = url_for(key) - generated_url = signer.signed_uri(URI(base_url), false, permissions: "r", - expiry: format_expiry(expires_in), content_disposition: "#{disposition}; filename=\"#{filename}\"").to_s - - payload[:url] = generated_url - - generated_url - end - end - - def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) - instrument :url, key do |payload| - base_url = url_for(key) - generated_url = signer.signed_uri(URI(base_url), false, permissions: "rw", - expiry: format_expiry(expires_in)).to_s - - payload[:url] = generated_url - - generated_url - end - end - - def headers_for_direct_upload(key, content_type:, checksum:, **) - { "Content-Type" => content_type, "Content-MD5" => checksum, "x-ms-blob-type" => "BlockBlob" } - end - - private - def url_for(key) - "#{path}/#{container}/#{key}" - end - - def blob_for(key) - blobs.get_blob_properties(container, key) - rescue Azure::Core::Http::HTTPError - false - end - - def format_expiry(expires_in) - expires_in ? Time.now.utc.advance(seconds: expires_in).iso8601 : nil - end - - # Reads the object for the given key in chunks, yielding each to the block. - def stream(key, options = {}, &block) - blob = blob_for(key) - - chunk_size = 5.megabytes - offset = 0 - - while offset < blob.properties[:content_length] - _, io = blobs.get_blob(container, key, start_range: offset, end_range: offset + chunk_size - 1) - yield io - offset += chunk_size - end - end -end diff --git a/activestorage/lib/active_storage/service/azure_storage_service.rb b/activestorage/lib/active_storage/service/azure_storage_service.rb new file mode 100644 index 0000000000..527dc57eeb --- /dev/null +++ b/activestorage/lib/active_storage/service/azure_storage_service.rb @@ -0,0 +1,115 @@ +require "active_support/core_ext/numeric/bytes" +require "azure/storage" +require "azure/storage/core/auth/shared_access_signature" + +# Wraps the Microsoft Azure Storage Blob Service as a Active Storage service. +# See `ActiveStorage::Service` for the generic API documentation that applies to all services. +class ActiveStorage::Service::AzureStorageService < ActiveStorage::Service + attr_reader :client, :path, :blobs, :container, :signer + + def initialize(path:, storage_account_name:, storage_access_key:, container:) + @client = Azure::Storage::Client.create(storage_account_name: storage_account_name, storage_access_key: storage_access_key) + @signer = Azure::Storage::Core::Auth::SharedAccessSignature.new(storage_account_name, storage_access_key) + @blobs = client.blob_client + @container = container + @path = path + end + + def upload(key, io, checksum: nil) + instrument :upload, key, checksum: checksum do + begin + blobs.create_block_blob(container, key, io, content_md5: checksum) + rescue Azure::Core::Http::HTTPError => e + raise ActiveStorage::IntegrityError + end + end + end + + def download(key) + if block_given? + instrument :streaming_download, key do + stream(key, &block) + end + else + instrument :download, key do + _, io = blobs.get_blob(container, key) + io.force_encoding(Encoding::BINARY) + end + end + end + + def delete(key) + instrument :delete, key do + begin + blobs.delete_blob(container, key) + rescue Azure::Core::Http::HTTPError + false + end + end + end + + def exist?(key) + instrument :exist, key do |payload| + answer = blob_for(key).present? + payload[:exist] = answer + answer + end + end + + def url(key, expires_in:, disposition:, filename:) + instrument :url, key do |payload| + base_url = url_for(key) + generated_url = signer.signed_uri(URI(base_url), false, permissions: "r", + expiry: format_expiry(expires_in), content_disposition: "#{disposition}; filename=\"#{filename}\"").to_s + + payload[:url] = generated_url + + generated_url + end + end + + def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) + instrument :url, key do |payload| + base_url = url_for(key) + generated_url = signer.signed_uri(URI(base_url), false, permissions: "rw", + expiry: format_expiry(expires_in)).to_s + + payload[:url] = generated_url + + generated_url + end + end + + def headers_for_direct_upload(key, content_type:, checksum:, **) + { "Content-Type" => content_type, "Content-MD5" => checksum, "x-ms-blob-type" => "BlockBlob" } + end + + private + def url_for(key) + "#{path}/#{container}/#{key}" + end + + def blob_for(key) + blobs.get_blob_properties(container, key) + rescue Azure::Core::Http::HTTPError + false + end + + def format_expiry(expires_in) + expires_in ? Time.now.utc.advance(seconds: expires_in).iso8601 : nil + end + + # Reads the object for the given key in chunks, yielding each to the block. + def stream(key, options = {}, &block) + blob = blob_for(key) + + chunk_size = 5.megabytes + offset = 0 + + while offset < blob.properties[:content_length] + _, io = blobs.get_blob(container, key, start_range: offset, end_range: offset + chunk_size - 1) + yield io + offset += chunk_size + end + end +end diff --git a/activestorage/lib/active_storage/service/configurator.rb b/activestorage/lib/active_storage/service/configurator.rb index 00ae24d251..a0afdaa912 100644 --- a/activestorage/lib/active_storage/service/configurator.rb +++ b/activestorage/lib/active_storage/service/configurator.rb @@ -22,7 +22,7 @@ class ActiveStorage::Service::Configurator #:nodoc: end def resolve(class_name) - require "active_storage/service/#{class_name.to_s.downcase}_service" + require "active_storage/service/#{class_name.to_s.underscore}_service" ActiveStorage::Service.const_get(:"#{class_name}Service") end end -- cgit v1.2.3