Class: Shrine::Storage::Cloudinary

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

Defined Under Namespace

Classes: Delegator

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



55
56
57
58
59
60
61
# File 'lib/shrine/storage/cloudinary.rb', line 55

def clear!(**options)
  if prefix
    api.delete_resources_by_prefix(prefix, **options)
  else
    api.delete_all_resources(**options)
  end
end

#delete(id) ⇒ Object



47
48
49
# File 'lib/shrine/storage/cloudinary.rb', line 47

def delete(id)
  uploader.destroy(public_id(id))
end

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


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

def exists?(id)
  result = api.resources_by_ids([public_id(id)])
  result.fetch("resources").any?
end

#open(id, **options) ⇒ Object



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

def open(id, **options)
  Down::Http.open(url(id, sign_url: true), **options)
rescue Down::NotFound
  raise Shrine::FileNotFound, "file #{id.inspect} not found on storage"
end

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/shrine/storage/cloudinary.rb', line 63

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

  fields = ::Cloudinary::Uploader.build_upload_params(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 = utils.cloudinary_api_url("upload")

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

#update(id, **options) ⇒ Object



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

def update(id, **options)
  uploader.explicit(public_id(id), **options)
end

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



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

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

  result = store(io, **options)

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

  result
end

#url(id, **options) ⇒ Object



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

def url(id, **options)
  utils.cloudinary_url(path(id), secure: true, **options)
end