Module: Shrine::Plugins::Transloadit::AttacherMethods

Defined in:
lib/shrine/plugins/transloadit.rb

Instance Method Summary collapse

Instance Method Details

#transloadit_process(cached_file = get) ⇒ Object

Triggers Transloadit processing defined by the user in ‘Shrine#transloadit_process`. It dumps the attacher in the payload of the request, so that it’s included in the webhook and that we know which webhook belongs to which record/attachment.

After the Transloadit assembly was submitted, the response is saved into cached file’s metadata, which can then be reloaded at will for checking progress of the assembly.

Raises a ‘Shrine::Plugins::Transloadit::ResponseError` if Transloadit returned an error.

Raises:



78
79
80
81
82
83
84
85
86
# File 'lib/shrine/plugins/transloadit.rb', line 78

def transloadit_process(cached_file = get)
  assembly = store.transloadit_process(cached_file, context)
  assembly.options[:fields] ||= {}
  assembly.options[:fields]["attacher"] = self.dump.merge("attachment" => cached_file.to_json)
  response = assembly.create!
  raise ResponseError.new(response.body) if response["error"]
  cached_file.["transloadit_response"] = response.body.to_json
  swap(cached_file) or _set(cached_file)
end

#transloadit_save(response, valid: true) ⇒ Object

It receives the result of Transloadit processing, and converts it into Shrine’s representation(s) of an uploaded file (either as a single file or a hash of versions).

If attachment has changed in the meanwhile, meaning the result of this processing is no longer valid, it deletes the processed files from the main storage.

Raises a ‘Shrine::Plugins::Transloadit::ResponseError` if Transloadit returned an error.

Raises:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/shrine/plugins/transloadit.rb', line 97

def transloadit_save(response, valid: true)
  raise ResponseError.new(response) if response["error"]

  if versions = response["fields"]["versions"]
    stored_file = versions.inject({}) do |hash, (name, step_name)|
      results        = response["results"].fetch(step_name)
      uploaded_files = results.map { |result| store.transloadit_uploaded_file(result) }
      multiple       = response["fields"]["multiple"].to_h[name]

      if multiple == "list"
        hash.merge!(name => uploaded_files)
      elsif uploaded_files.one?
        hash.merge!(name => uploaded_files[0])
      else
        raise Error, "Step produced multiple files but wasn't marked as multiple"
      end
    end
  else
    results        = response["results"].values.last
    uploaded_files = results.map { |result| store.transloadit_uploaded_file(result) }
    multiple       = response["fields"]["multiple"]

    if multiple == "list"
      stored_file = uploaded_files
    elsif uploaded_files.one?
      stored_file = uploaded_files[0]
    else
      raise Error, "Step produced multiple files but wasn't marked as multiple"
    end
  end

  if valid
    swap(stored_file)
  else
    _delete(stored_file, action: :abort)
  end
end