@@ -62,6 +62,7 @@ def upload(key, io, filename: nil, checksum: nil, **options)
62
62
end
63
63
64
64
def url ( key , filename : nil , content_type : '' , **options )
65
+ key = find_blob_or_use_key ( key )
65
66
instrument :url , key : key do |payload |
66
67
url = Cloudinary ::Utils . cloudinary_url (
67
68
full_public_id_internal ( key , options ) ,
@@ -105,6 +106,7 @@ def headers_for_direct_upload(key, content_type:, checksum:, **)
105
106
end
106
107
107
108
def delete ( key )
109
+ key = find_blob_or_use_key ( key )
108
110
instrument :delete , key : key do
109
111
options = {
110
112
resource_type : resource_type ( nil , key ) ,
@@ -121,6 +123,7 @@ def delete_prefixed(prefix)
121
123
end
122
124
123
125
def exist? ( key )
126
+ key = find_blob_or_use_key ( key )
124
127
instrument :exist , key : key do |payload |
125
128
begin
126
129
options = {
@@ -157,8 +160,7 @@ def download(key, &block)
157
160
158
161
# Return the partial content in the byte +range+ of the file at the +key+.
159
162
def download_chunk ( key , range )
160
- url = Cloudinary ::Utils . cloudinary_url ( public_id ( key ) , resource_type : resource_type ( nil , key ) )
161
- uri = URI ( url )
163
+ uri = URI ( url ( key ) )
162
164
instrument :download , key : key do
163
165
req = Net ::HTTP ::Get . new ( uri )
164
166
range_end = case
@@ -309,5 +311,19 @@ def resource_type(io, key = "", content_type = "")
309
311
end
310
312
content_type_to_resource_type ( content_type )
311
313
end
314
+
315
+ def find_blob_or_use_key ( key )
316
+ if key . is_a? ( ActiveStorage ::BlobKey )
317
+ key
318
+ else
319
+ begin
320
+ blob = ActiveStorage ::Blob . find_by ( key : key )
321
+ blob ? ActiveStorage ::BlobKey . new ( blob . attributes . as_json ) : key
322
+ rescue ActiveRecord ::StatementInvalid => e
323
+ # Return the original key if an error occurs
324
+ key
325
+ end
326
+ end
327
+ end
312
328
end
313
329
end
0 commit comments