Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ AllCops:
- spec/dummy??/**/*
- vendor/**/*
SuggestExtensions: false
TargetRubyVersion: 2.7
TargetRubyVersion: 3.0

Lint/MissingSuper:
Exclude:
Expand Down
6 changes: 3 additions & 3 deletions active_storage_db.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ Gem::Specification.new do |spec|
spec.description = "An ActiveStorage service plugin to store files in database."
spec.license = "MIT"

spec.required_ruby_version = ">= 2.7.0"
spec.required_ruby_version = ">= 3.0.0"

spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = spec.homepage
spec.metadata["rubygems_mfa_required"] = "true"

spec.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"]

spec.add_dependency "activestorage", ">= 6.0"
spec.add_dependency "rails", ">= 6.0"
spec.add_dependency "activestorage", ">= 6.1"
spec.add_dependency "rails", ">= 6.1"
end
12 changes: 3 additions & 9 deletions lib/active_storage/service/db_service.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require "active_storage/service/db_service_rails60"
require "active_storage/service/db_service_rails61"
require "active_storage/service/db_service_rails70"

Expand All @@ -11,10 +10,8 @@ class Service::DBService < Service
# :nocov:
if Rails::VERSION::MAJOR >= 7
include ActiveStorage::DBServiceRails70
elsif Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR == 1
include ActiveStorage::DBServiceRails61
else
include ActiveStorage::DBServiceRails60
include ActiveStorage::DBServiceRails61
end
# :nocov:

Expand Down Expand Up @@ -101,7 +98,7 @@ def url_for_direct_upload(key, expires_in:, content_type:, content_length:, chec
purpose: :blob_token
)

url_helpers.update_service_url(verified_token_with_expiration, url_options).tap do |generated_url|
url_helpers.update_service_url(verified_token_with_expiration, url_params).tap do |generated_url|
payload[:url] = generated_url
end
end
Expand Down Expand Up @@ -150,12 +147,9 @@ def generate_url(key, expires_in:, filename:, content_type:, disposition:)
purpose: :blob_key
)

current_uri = URI.parse(current_host)
url_helpers.service_url(
verified_key_with_expiration,
protocol: current_uri.scheme,
host: current_uri.host,
port: current_uri.port,
**url_params,
disposition: content_disposition,
content_type: content_type,
filename: filename
Expand Down
25 changes: 0 additions & 25 deletions lib/active_storage/service/db_service_rails60.rb

This file was deleted.

11 changes: 3 additions & 8 deletions lib/active_storage/service/db_service_rails61.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ module ActiveStorage
module DBServiceRails61
private

def current_host
url_options[:host]
end

def private_url(key, expires_in:, filename:, content_type:, disposition:, **)
generate_url(
key,
Expand All @@ -28,10 +24,9 @@ def public_url(key, filename:, content_type: nil, disposition: :attachment, **)
)
end

def url_options
{
host: ActiveStorage::Current.host
}
def url_params
uri = URI.parse(ActiveStorage::Current.host)
{ protocol: uri.scheme, host: uri.host, port: uri.port }
end
end
end
32 changes: 15 additions & 17 deletions lib/active_storage/service/db_service_rails70.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,25 @@
module ActiveStorage
module DBServiceRails70
def compose(source_keys, destination_key, **)
buffer = nil
comment = "DBService#compose"
source_keys.each do |source_key|
record = ::ActiveStorageDB::File.annotate(comment).find_by(ref: source_key)
raise ActiveStorage::FileNotFoundError unless record
instrument :compose, key: destination_key do
buffer = nil
comment = "DBService#compose"
source_keys.each do |source_key|
record = ::ActiveStorageDB::File.annotate(comment).find_by(ref: source_key)
raise ActiveStorage::FileNotFoundError unless record

if buffer
buffer << record.data
else
buffer = +record.data
if buffer
buffer << record.data
else
buffer = +record.data
end
end
::ActiveStorageDB::File.create!(ref: destination_key, data: buffer) if buffer
end
::ActiveStorageDB::File.create!(ref: destination_key, data: buffer) if buffer
end

private

def current_host
opts = url_options || {}
opts[:port] ? "#{opts[:protocol]}#{opts[:host]}:#{opts[:port]}" : "#{opts[:protocol]}#{opts[:host]}"
end

def private_url(key, expires_in:, filename:, content_type:, disposition:, **)
generate_url(
key,
Expand All @@ -45,8 +42,9 @@ def public_url(key, filename:, content_type: nil, disposition: :attachment, **)
)
end

def url_options
ActiveStorage::Current.url_options
def url_params
opts = ActiveStorage::Current.url_options || {}
{ protocol: opts[:protocol], host: opts[:host], port: opts[:port] }
end
end
end
2 changes: 1 addition & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
require 'factory_bot_rails'

support_files = File.expand_path('support/**/*.rb', __dir__)
Dir[support_files].sort.each { |f| require f }
Dir[support_files].each { |f| require f }

RSpec.configure do |config|
config.filter_rails_from_backtrace!
Expand Down
Loading