11# frozen_string_literal: true
22
3+ require 'active_storage/service/db_service_rails60'
4+ require 'active_storage/service/db_service_rails61'
5+ require 'active_storage/service/db_service_rails70'
6+
37module ActiveStorage
48 # Wraps a DB table as an Active Storage service. See ActiveStorage::Service
59 # for the generic API documentation that applies to all services.
610 class Service ::DBService < Service
7- def initialize ( **)
11+ if Rails ::VERSION ::MAJOR >= 7
12+ include ActiveStorage ::DBServiceRails70
13+ elsif Rails ::VERSION ::MAJOR == 6 && Rails ::VERSION ::MINOR == 1
14+ include ActiveStorage ::DBServiceRails61
15+ else
16+ include ActiveStorage ::DBServiceRails60
17+ end
18+
19+ def initialize ( public : false , **)
820 @chunk_size = ENV . fetch ( 'ASDB_CHUNK_SIZE' ) { 1 . megabytes }
21+ @public = public
922 end
1023
1124 def upload ( key , io , checksum : nil , **)
@@ -62,17 +75,6 @@ def exist?(key)
6275 end
6376 end
6477
65- if Rails ::VERSION ::MAJOR == 6 && Rails ::VERSION ::MINOR == 0
66- def url ( key , expires_in :, filename :, disposition :, content_type :)
67- instrument :url , key : key do |payload |
68- opts = { expires_in : expires_in , filename : filename , content_type : content_type , disposition : disposition }
69- generate_url ( key , opts ) . tap do |generated_url |
70- payload [ :url ] = generated_url
71- end
72- end
73- end
74- end
75-
7678 def url_for_direct_upload ( key , expires_in :, content_type :, content_length :, checksum :, custom_metadata : { } )
7779 instrument :url , key : key do |payload |
7880 verified_token_with_expiration = ActiveStorage . verifier . generate (
@@ -99,25 +101,6 @@ def headers_for_direct_upload(_key, content_type:, **)
99101
100102 private
101103
102- def current_host
103- if ActiveStorage ::Current . respond_to? :url_options
104- opts = ActiveStorage ::Current . url_options || { }
105- url = "#{ opts [ :protocol ] } #{ opts [ :host ] } "
106- url += ":#{ opts [ :port ] } " if opts [ :port ]
107- url || ''
108- else
109- ActiveStorage ::Current . host
110- end
111- end
112-
113- def private_url ( key , expires_in :, filename :, content_type :, disposition :, **)
114- generate_url ( key , expires_in : expires_in , filename : filename , content_type : content_type , disposition : disposition )
115- end
116-
117- def public_url ( key , filename :, content_type : nil , disposition : :attachment , **)
118- generate_url ( key , expires_in : nil , filename : filename , content_type : content_type , disposition : disposition )
119- end
120-
121104 def generate_url ( key , expires_in :, filename :, content_type :, disposition :)
122105 content_disposition = content_disposition_with ( type : disposition , filename : filename )
123106 verified_key_with_expiration = ActiveStorage . verifier . generate (
@@ -164,13 +147,5 @@ def stream(key)
164147 def url_helpers
165148 @url_helpers ||= ::ActiveStorageDB ::Engine . routes . url_helpers
166149 end
167-
168- def url_options
169- if ActiveStorage ::Current . respond_to? :url_options
170- ActiveStorage ::Current . url_options
171- else
172- { host : ActiveStorage ::Current . host }
173- end
174- end
175150 end
176151end
0 commit comments