Class: Service::CloudinaryService
- Inherits:
-
Service
- Object
- Service
- Service::CloudinaryService
- Includes:
- DownloadUtils
- Defined in:
- lib/active_storage/service/cloudinary_service.rb
Overview
Wraps the Cloudinary as an Active Storage service. See ActiveStorage::Service for the generic API documentation that applies to all services.
Instance Method Summary collapse
-
#delete(key) ⇒ Object
Delete the file at the
key
. -
#delete_prefixed(prefix) ⇒ Object
Delete files at keys starting with the
prefix
. -
#download(key, &block) ⇒ Object
Return the content of the file at the
key
. -
#download_chunk(key, range) ⇒ Object
Return the partial content in the byte
range
of the file at thekey
. -
#exist?(key) ⇒ Boolean
Return
true
if a file exists at thekey
. -
#headers_for_direct_upload(key, filename:, content_type:, content_length:, checksum:) ⇒ Object
Returns a Hash of headers for
url_for_direct_upload
requests. -
#initialize(cloud_name:, api_key:, api_secret:, **options) ⇒ CloudinaryService
constructor
FIXME: implement setup for private resource type FIXME: allow configuration via cloudinary url.
- #upload(key, io, checksum: nil) ⇒ Object
-
#url(key, expires_in:, disposition:, filename:, content_type:) ⇒ Object
Returns a signed, temporary URL for the file at the
key
. -
#url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) ⇒ Object
Returns a signed, temporary URL that a direct upload file can be PUT to on the
key
.
Methods included from DownloadUtils
#download_range, #stream_download
Constructor Details
#initialize(cloud_name:, api_key:, api_secret:, **options) ⇒ CloudinaryService
FIXME: implement setup for private resource type FIXME: allow configuration via cloudinary url
12 13 14 15 16 17 18 19 20 |
# File 'lib/active_storage/service/cloudinary_service.rb', line 12 def initialize(cloud_name:, api_key:, api_secret:, **) .merge!( cloud_name: cloud_name, api_key: api_key, api_secret: api_secret ) Cloudinary.config() # Cloudinary.config_from_url(url) end |
Instance Method Details
#delete(key) ⇒ Object
Delete the file at the key
.
52 53 54 55 56 |
# File 'lib/active_storage/service/cloudinary_service.rb', line 52 def delete(key) instrument :delete, key: key do delete_resource_with_public_id(key) end end |
#delete_prefixed(prefix) ⇒ Object
Delete files at keys starting with the prefix
.
59 60 61 62 63 |
# File 'lib/active_storage/service/cloudinary_service.rb', line 59 def delete_prefixed(prefix) instrument :delete_prefixed, prefix: prefix do Cloudinary::Api.delete_resources_by_prefix(prefix) end end |
#download(key, &block) ⇒ Object
Return the content of the file at the key
.
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/active_storage/service/cloudinary_service.rb', line 29 def download(key, &block) source = cloudinary_url_for_key(key) if block_given? instrument :streaming_download, key: key do stream_download(source, &block) end else instrument :download, key: key do Cloudinary::Downloader.download(source) end end end |
#download_chunk(key, range) ⇒ Object
Return the partial content in the byte range
of the file at the key
.
44 45 46 47 48 49 |
# File 'lib/active_storage/service/cloudinary_service.rb', line 44 def download_chunk(key, range) instrument :download_chunk, key: key, range: range do source = cloudinary_url_for_key(key) download_range(source, range) end end |
#exist?(key) ⇒ Boolean
Return true
if a file exists at the key
.
66 67 68 69 70 |
# File 'lib/active_storage/service/cloudinary_service.rb', line 66 def exist?(key) instrument :exist?, key: key do resource_exists_with_public_id?(key) end end |
#headers_for_direct_upload(key, filename:, content_type:, content_length:, checksum:) ⇒ Object
Returns a Hash of headers for url_for_direct_upload
requests.
110 111 112 |
# File 'lib/active_storage/service/cloudinary_service.rb', line 110 def headers_for_direct_upload(key, filename:, content_type:, content_length:, checksum:) { 'Content-Type' => content_type, 'X-Unique-Upload-Id' => key } end |
#upload(key, io, checksum: nil) ⇒ Object
22 23 24 25 26 |
# File 'lib/active_storage/service/cloudinary_service.rb', line 22 def upload(key, io, checksum: nil, **) instrument :upload, key: key, checksum: checksum do Cloudinary::Uploader.upload(io, public_id: key, resource_type: 'auto') end end |
#url(key, expires_in:, disposition:, filename:, content_type:) ⇒ Object
Returns a signed, temporary URL for the file at the key
. The URL will be valid for the amount of seconds specified in expires_in
. You must also provide the disposition
(:inline
or :attachment
), filename
, and content_type
that you wish the file to be served with on request.
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/active_storage/service/cloudinary_service.rb', line 75 def url(key, expires_in:, disposition:, filename:, content_type:) instrument :url, key: key do = { expires_in: expires_in, content_type: content_type, disposition: disposition, filename: filename } signed_download_url_for_public_id(key, ) end end |
#url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) ⇒ Object
Returns a signed, temporary URL that a direct upload file can be PUT to on the key
. The URL will be valid for the amount of seconds specified in expires_in
. You must also provide the content_type
, content_length
, and checksum
of the file that will be uploaded. All these attributes will be validated by the service upon upload.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/active_storage/service/cloudinary_service.rb', line 91 def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) instrument :url_for_direct_upload, key: key do = { expires_in: expires_in, content_type: content_type, content_length: content_length, checksum: checksum, resource_type: 'auto' } # FIXME: Cloudinary Ruby SDK does't expose an api for signed upload url # The expected url is similar to the private_download_url # with download replaced with upload signed_download_url_for_public_id(key, ) .sub(/download/, 'upload') end end |