aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_storage/service
diff options
context:
space:
mode:
Diffstat (limited to 'lib/active_storage/service')
-rw-r--r--lib/active_storage/service/disk_service.rb71
-rw-r--r--lib/active_storage/service/gcs_service.rb53
-rw-r--r--lib/active_storage/service/mirror_service.rb51
-rw-r--r--lib/active_storage/service/s3_service.rb63
4 files changed, 238 insertions, 0 deletions
diff --git a/lib/active_storage/service/disk_service.rb b/lib/active_storage/service/disk_service.rb
new file mode 100644
index 0000000000..6977b5b82e
--- /dev/null
+++ b/lib/active_storage/service/disk_service.rb
@@ -0,0 +1,71 @@
+require "fileutils"
+require "pathname"
+
+class ActiveStorage::Service::DiskService < ActiveStorage::Service
+ attr_reader :root
+
+ def initialize(root:)
+ @root = root
+ end
+
+ def upload(key, io)
+ File.open(make_path_for(key), "wb") do |file|
+ while chunk = io.read(65536)
+ file.write(chunk)
+ end
+ end
+ end
+
+ def download(key)
+ if block_given?
+ File.open(path_for(key)) do |file|
+ while data = file.read(65536)
+ yield data
+ end
+ end
+ else
+ File.open path_for(key), &:read
+ end
+ end
+
+ def delete(key)
+ File.delete path_for(key) rescue Errno::ENOENT # Ignore files already deleted
+ end
+
+ def exist?(key)
+ File.exist? path_for(key)
+ end
+
+
+ def url(key, expires_in:, disposition:, filename:)
+ verified_key_with_expiration = ActiveStorage::VerifiedKeyWithExpiration.encode(key, expires_in: expires_in)
+
+ if defined?(Rails) && defined?(Rails.application)
+ Rails.application.routes.url_helpers.rails_disk_blob_path(verified_key_with_expiration, disposition: disposition)
+ else
+ "/rails/blobs/#{verified_key_with_expiration}?disposition=#{disposition}"
+ end
+ end
+
+ def byte_size(key)
+ File.size path_for(key)
+ end
+
+ def checksum(key)
+ Digest::MD5.file(path_for(key)).hexdigest
+ end
+
+
+ private
+ def path_for(key)
+ File.join root, folder_for(key), key
+ end
+
+ def folder_for(key)
+ [ key[0..1], key[2..3] ].join("/")
+ end
+
+ def make_path_for(key)
+ path_for(key).tap { |path| FileUtils.mkdir_p File.dirname(path) }
+ end
+end
diff --git a/lib/active_storage/service/gcs_service.rb b/lib/active_storage/service/gcs_service.rb
new file mode 100644
index 0000000000..18ec1de133
--- /dev/null
+++ b/lib/active_storage/service/gcs_service.rb
@@ -0,0 +1,53 @@
+require "google/cloud/storage"
+require "active_support/core_ext/object/to_query"
+
+class ActiveStorage::Service::GCSService < ActiveStorage::Service
+ attr_reader :client, :bucket
+
+ def initialize(project:, keyfile:, bucket:)
+ @client = Google::Cloud::Storage.new(project: project, keyfile: keyfile)
+ @bucket = @client.bucket(bucket)
+ end
+
+ def upload(key, io)
+ bucket.create_file(io, key)
+ end
+
+ def download(key)
+ io = file_for(key).download
+ io.rewind
+ io.read
+ end
+
+ def delete(key)
+ file_for(key).try(:delete)
+ end
+
+ def exist?(key)
+ file_for(key).present?
+ end
+
+
+ def url(key, expires_in:, disposition:, filename:)
+ file_for(key).signed_url(expires: expires_in) + "&" +
+ { "response-content-disposition" => "#{disposition}; filename=\"#{filename}\"" }.to_query
+ end
+
+ def byte_size(key)
+ file_for(key).size
+ end
+
+ def checksum(key)
+ convert_to_hex base64: file_for(key).md5
+ end
+
+
+ private
+ def file_for(key)
+ bucket.file(key)
+ end
+
+ def convert_to_hex(base64:)
+ base64.unpack("m0").first.unpack("H*").first
+ end
+end
diff --git a/lib/active_storage/service/mirror_service.rb b/lib/active_storage/service/mirror_service.rb
new file mode 100644
index 0000000000..2a3518e59e
--- /dev/null
+++ b/lib/active_storage/service/mirror_service.rb
@@ -0,0 +1,51 @@
+class ActiveStorage::Service::MirrorService < ActiveStorage::Service
+ attr_reader :services
+
+ def initialize(services:)
+ @services = services
+ end
+
+ def upload(key, io)
+ services.collect do |service|
+ service.upload key, io
+ io.rewind
+ end
+ end
+
+ def download(key)
+ services.detect { |service| service.exist?(key) }.download(key)
+ end
+
+ def delete(key)
+ perform_across_services :delete, key
+ end
+
+ def exist?(key)
+ perform_across_services(:exist?, key).any?
+ end
+
+
+ def url(key, **options)
+ primary_service.url(key, **options)
+ end
+
+ def byte_size(key)
+ primary_service.byte_size(key)
+ end
+
+ def checksum(key)
+ primary_service.checksum(key)
+ end
+
+ private
+ def primary_service
+ services.first
+ end
+
+ def perform_across_services(method, *args)
+ # FIXME: Convert to be threaded
+ services.collect do |service|
+ service.public_send method, *args
+ end
+ end
+end
diff --git a/lib/active_storage/service/s3_service.rb b/lib/active_storage/service/s3_service.rb
new file mode 100644
index 0000000000..811321a172
--- /dev/null
+++ b/lib/active_storage/service/s3_service.rb
@@ -0,0 +1,63 @@
+require "aws-sdk"
+
+class ActiveStorage::Service::S3Service < ActiveStorage::Service
+ attr_reader :client, :bucket
+
+ def initialize(access_key_id:, secret_access_key:, region:, bucket:)
+ @client = Aws::S3::Resource.new(access_key_id: access_key_id, secret_access_key: secret_access_key, region: region)
+ @bucket = @client.bucket(bucket)
+ end
+
+ def upload(key, io)
+ object_for(key).put(body: io)
+ end
+
+ def download(key)
+ if block_given?
+ stream(key, &block)
+ else
+ object_for(key).get.body.read
+ end
+ end
+
+ def delete(key)
+ object_for(key).delete
+ end
+
+ def exist?(key)
+ object_for(key).exists?
+ end
+
+
+ def url(key, expires_in:, disposition:, filename:)
+ object_for(key).presigned_url :get, expires_in: expires_in,
+ response_content_disposition: "#{disposition}; filename=\"#{filename}\""
+ end
+
+ def byte_size(key)
+ object_for(key).size
+ end
+
+ def checksum(key)
+ object_for(key).etag.remove(/"/)
+ end
+
+
+ private
+ def object_for(key)
+ bucket.object(key)
+ end
+
+ # Reads the object for the given key in chunks, yielding each to the block.
+ def stream(key, options = {}, &block)
+ object = object_for(key)
+
+ chunk_size = 5242880 # 5 megabytes
+ offset = 0
+
+ while offset < object.content_length
+ yield object.read(options.merge(:range => "bytes=#{offset}-#{offset + chunk_size - 1}"))
+ offset += chunk_size
+ end
+ end
+end