Class: Cloudinary::CarrierWave::Storage

Inherits:
CarrierWave::Storage::Abstract
  • Object
show all
Defined in:
lib/cloudinary/carrier_wave/storage.rb

Instance Method Summary collapse

Instance Method Details

#identifierObject



57
58
59
# File 'lib/cloudinary/carrier_wave/storage.rb', line 57

def identifier
  uploader.file.respond_to?(:storage_identifier) ? uploader.file.storage_identifier : super
end

#store!(file) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cloudinary/carrier_wave/storage.rb', line 3

def store!(file)
  return unless uploader.enable_processing

  if uploader.is_main_uploader?
    case file
    when Cloudinary::CarrierWave::PreloadedCloudinaryFile
      storage_type = uploader.class.storage_type || "upload"
      raise CloudinaryException, "Uploader configured for type #{storage_type} but resource of type #{file.type} given." if storage_type.to_s != file.type
      if uploader.public_id && uploader.auto_rename_preloaded?
        @stored_version = file.version
        uploader.rename(nil, true)
      else
        store_cloudinary_identifier(file.version, file.filename, file.resource_type, file.type)
      end
      return # Nothing to do
    when Cloudinary::CarrierWave::CloudinaryFile, Cloudinary::CarrierWave::StoredFile
      return # Nothing to do
    when Cloudinary::CarrierWave::RemoteFile
      data = file.uri.to_s
    else
      data = file.file
      data.rewind if !file.is_path? && data.respond_to?(:rewind)
    end

    # This is the toplevel, need to upload the actual file.
    params = uploader.transformation.dup
    params[:return_error] = true
    params[:format] = uploader.requested_format
    params[:public_id] = uploader.my_public_id
    uploader.versions.values.each(&:tags) # Validate no tags in versions
    params[:tags] = uploader.tags if uploader.tags
    eager_versions = uploader.versions.values.select(&:eager)
    params[:eager] = eager_versions.map{|version| [version.transformation, version.format]} if eager_versions.length > 0
    params[:type]=uploader.class.storage_type

    params[:resource_type] ||= :auto
    upload_method = uploader.respond_to?(:upload_chunked?) && uploader.upload_chunked? ? "upload_large" : "upload"
    uploader. = Cloudinary::Uploader.send(upload_method, data, params)
    if uploader.["error"]
      raise Cloudinary::CarrierWave::UploadError.new(uploader.["error"]["message"], uploader.["error"]["http_code"])
    end

    if uploader.["version"]
      filename = [uploader.["public_id"], uploader.["format"]].reject(&:blank?).join(".")
      store_cloudinary_identifier(uploader.["version"], filename, uploader.["resource_type"], uploader.["type"])
    end
    # Will throw an exception on error
  else
    raise CloudinaryException, "nested versions are not allowed." if (uploader.class.version_names.length > 1)
    # Do nothing - versions are not handled locally.
  end
  nil
end

#store_cloudinary_identifier(version, filename, resource_type = nil, type = nil) ⇒ Object

Updates the model mounter identifier with version information.

Carrierwave uses hooks when integrating with ORMs so it’s important to update the identifier in a way that does not trigger hooks again or else you’ll get stuck in a loop.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/cloudinary/carrier_wave/storage.rb', line 66

def store_cloudinary_identifier(version, filename, resource_type=nil, type=nil)
  name = "v#{version}/#{filename}"
  if uploader.use_extended_identifier?
    resource_type ||= uploader.resource_type || "image"
    type ||= uploader.storage_type || "upload"
    name = "#{resource_type}/#{type}/#{name}"
  end
  model_class = uploader.model.class
  column = uploader.model.send(:_mounter, uploader.mounted_as).send(:serialization_column)
  original_value = uploader.model.read_attribute(column)
  return if original_value == name

  if original_value.is_a?(Array)
    if index = original_value.index(identifier)
      original_value[index] = name
    end
    name = original_value
  end

  if defined?(ActiveRecord::Base) && uploader.model.is_a?(ActiveRecord::Base)
    uploader.model.update_column(column, name)
  elsif defined?(Mongoid::Document) && uploader.model.is_a?(Mongoid::Document)
    # Mongoid support
    if Mongoid::VERSION.split(".").first.to_i >= 4
      column = column.to_sym
      uploader.model.write_attribute(column, name)
      uploader.model.set(column => name)
    else
      uploader.model.set(column, name)
    end
  elsif defined?(Neo4j::VERSION) && Neo4j::VERSION.split(".").first.to_i >= 5
      uploader.model.write_attribute(column, name)
  elsif defined?(Sequel::Model) && uploader.model.is_a?(Sequel::Model)
    # Sequel support
    uploader.model.this.update(column => name)
  elsif model_class.respond_to?(:update_all) && uploader.model.respond_to?(:_id)
    model_class.where(:_id=>uploader.model._id).update_all(column=>name)
    uploader.model.send :write_attribute, column, name
  else
    raise CloudinaryException, "Only ActiveRecord, Mongoid and Sequel are supported at the moment!"
  end
end