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'
3+ require " active_storage/service/db_service_rails60"
4+ require " active_storage/service/db_service_rails61"
5+ require " active_storage/service/db_service_rails70"
66
77module ActiveStorage
88 # Wraps a DB table as an Active Storage service. See ActiveStorage::Service
@@ -19,7 +19,7 @@ class Service::DBService < Service
1919 # :nocov:
2020
2121 def initialize ( public : false , **)
22- @chunk_size = ENV . fetch ( ' ASDB_CHUNK_SIZE' ) { 1 . megabytes }
22+ @chunk_size = ENV . fetch ( " ASDB_CHUNK_SIZE" ) { 1 . megabytes }
2323 @public = public
2424 end
2525
@@ -69,7 +69,7 @@ def delete_prefixed(prefix)
6969 instrument :delete_prefixed , prefix : prefix do
7070 comment = "DBService#delete_prefixed"
7171 sanitized_prefix = "#{ ApplicationRecord . sanitize_sql_like ( prefix ) } %"
72- ::ActiveStorageDB ::File . annotate ( comment ) . where ( ' ref LIKE ?' , sanitized_prefix ) . destroy_all
72+ ::ActiveStorageDB ::File . annotate ( comment ) . where ( " ref LIKE ?" , sanitized_prefix ) . destroy_all
7373 end
7474 end
7575
@@ -90,7 +90,7 @@ def url_for_direct_upload(key, expires_in:, content_type:, content_length:, chec
9090 content_type : content_type ,
9191 content_length : content_length ,
9292 checksum : checksum ,
93- service_name : respond_to? ( :name ) ? name : 'db'
93+ service_name : respond_to? ( :name ) ? name : "db"
9494 } ,
9595 expires_in : expires_in ,
9696 purpose : :blob_token
@@ -103,17 +103,25 @@ def url_for_direct_upload(key, expires_in:, content_type:, content_length:, chec
103103 end
104104
105105 def headers_for_direct_upload ( _key , content_type :, **)
106- { ' Content-Type' => content_type }
106+ { " Content-Type" => content_type }
107107 end
108108
109109 private
110110
111111 def adapter_sqlite?
112- @adapter_sqlite ||= ActiveStorageDB :: File . connection . adapter_name == ' SQLite'
112+ @adapter_sqlite ||= active_storage_db_adapter_name == " SQLite"
113113 end
114114
115115 def adapter_sqlserver?
116- @adapter_sqlserver ||= ActiveStorageDB ::File . connection . adapter_name == 'SQLServer'
116+ @adapter_sqlserver ||= active_storage_db_adapter_name == "SQLServer"
117+ end
118+
119+ def active_storage_db_adapter_name
120+ if ActiveStorageDB ::File . respond_to? ( :lease_connection )
121+ ActiveStorageDB ::File . lease_connection . adapter_name
122+ else
123+ ActiveStorageDB ::File . connection . adapter_name
124+ end
117125 end
118126
119127 def generate_url ( key , expires_in :, filename :, content_type :, disposition :)
@@ -123,7 +131,7 @@ def generate_url(key, expires_in:, filename:, content_type:, disposition:)
123131 key : key ,
124132 disposition : content_disposition ,
125133 content_type : content_type ,
126- service_name : respond_to? ( :name ) ? name : 'db'
134+ service_name : respond_to? ( :name ) ? name : "db"
127135 } ,
128136 expires_in : expires_in ,
129137 purpose : :blob_key
@@ -162,10 +170,10 @@ def object_for(key, fields: nil)
162170 end
163171
164172 def stream ( key )
165- data_size = adapter_sqlserver? ? ' DATALENGTH(data)' : ' OCTET_LENGTH(data)'
173+ data_size = adapter_sqlserver? ? " DATALENGTH(data)" : " OCTET_LENGTH(data)"
166174 size = object_for ( key , fields : "#{ data_size } AS size" ) &.size || raise ( ActiveStorage ::FileNotFoundError )
167175 ( size / @chunk_size . to_f ) . ceil . times . each do |i |
168- range = ( i * @chunk_size .. ( ( i + 1 ) * @chunk_size ) - 1 )
176+ range = ( i * @chunk_size ) .. ( ( ( i + 1 ) * @chunk_size ) - 1 )
169177 yield download_chunk ( key , range )
170178 end
171179 end
0 commit comments