Class: Shrine::Storage::Cloudinary

Inherits:
Object
  • Object
show all
Defined in:
lib/shrine/storage/cloudinary.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix: nil, resource_type: "image", type: "upload", store_data: nil, upload_options: {}, large: nil) ⇒ Cloudinary

Returns a new instance of Cloudinary.



10
11
12
13
14
15
16
17
# File 'lib/shrine/storage/cloudinary.rb', line 10

def initialize(prefix: nil, resource_type: "image", type: "upload", store_data: nil, upload_options: {}, large: nil)
  @prefix = prefix
  @large = large
  @resource_type = resource_type
  @type = type
  @upload_options = upload_options
  @store_data = store_data
end

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix.



8
9
10
# File 'lib/shrine/storage/cloudinary.rb', line 8

def prefix
  @prefix
end

#resource_typeObject (readonly)

Returns the value of attribute resource_type.



8
9
10
# File 'lib/shrine/storage/cloudinary.rb', line 8

def resource_type
  @resource_type
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/shrine/storage/cloudinary.rb', line 8

def type
  @type
end

#upload_optionsObject (readonly)

Returns the value of attribute upload_options.



8
9
10
# File 'lib/shrine/storage/cloudinary.rb', line 8

def upload_options
  @upload_options
end

Instance Method Details

#clear!(**options) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/shrine/storage/cloudinary.rb', line 62

def clear!(**options)
  if prefix
    ::Cloudinary::Api.delete_resources_by_prefix(prefix, default_options.merge(options))
  else
    ::Cloudinary::Api.delete_all_resources(default_options.merge(options))
  end
end

#delete(id) ⇒ Object



54
55
56
# File 'lib/shrine/storage/cloudinary.rb', line 54

def delete(id)
  ::Cloudinary::Uploader.destroy(public_id(id), default_options)
end

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/shrine/storage/cloudinary.rb', line 49

def exists?(id)
  result = ::Cloudinary::Api.resources_by_ids([public_id(id)], default_options)
  result.fetch("resources").any?
end

#movable?(io, id) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/shrine/storage/cloudinary.rb', line 41

def movable?(io, id)
  io.is_a?(UploadedFile) && io.storage.is_a?(Storage::Cloudinary)
end

#move(io, id, shrine_metadata: {}, **upload_options) ⇒ Object



37
38
39
# File 'lib/shrine/storage/cloudinary.rb', line 37

def move(io, id, shrine_metadata: {}, **upload_options)
  ::Cloudinary::Uploader.rename(io.storage.public_id(io.id), public_id(id), default_options)
end

#open(id, **options) ⇒ Object



45
46
47
# File 'lib/shrine/storage/cloudinary.rb', line 45

def open(id, **options)
  Down::Http.open(url(id), **options)
end

#presign(id = nil, **options) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/shrine/storage/cloudinary.rb', line 70

def presign(id = nil, **options)
  upload_options = id ? { public_id: public_id(id) } : { folder: prefix }
  upload_options.update(@upload_options)

  fields = ::Cloudinary::Uploader.build_upload_params(upload_options.merge(options))
  fields.reject! { |key, value| value.nil? || value == "" }
  fields[:signature] = ::Cloudinary::Utils.api_sign_request(fields, ::Cloudinary.config.api_secret)
  fields[:api_key] = ::Cloudinary.config.api_key

  url = ::Cloudinary::Utils.cloudinary_api_url("upload", default_options)

  { method: :post, url: url, fields: fields }
end

#update(id, **options) ⇒ Object



33
34
35
# File 'lib/shrine/storage/cloudinary.rb', line 33

def update(id, **options)
  ::Cloudinary::Uploader.explicit(public_id(id), default_options.merge(options))
end

#upload(io, id, shrine_metadata: {}, **upload_options) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/shrine/storage/cloudinary.rb', line 19

def upload(io, id, shrine_metadata: {}, **upload_options)
  options = { public_id: public_id(id) }
  options.update(default_options)
  options.update(@upload_options)
  options.update(upload_options)

  result = store(io, **options)

  update_id!(result, id)
  update_metadata!(result, )

  result
end

#url(id, **options) ⇒ Object



58
59
60
# File 'lib/shrine/storage/cloudinary.rb', line 58

def url(id, **options)
  ::Cloudinary::Utils.cloudinary_url(path(id), default_options.merge(secure: true, **options))
end