Module: Shrine::Plugins::Transloadit::AttacherMethods
- Defined in:
- lib/shrine/plugins/transloadit.rb
Instance Method Summary collapse
-
#transloadit_process(cached_file = get) ⇒ Object
Triggers Transloadit processing defined by the user in ‘Shrine#transloadit_process`.
-
#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).
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.
It raises a ‘Shrine::Error` if Transloadit returned an error.
80 81 82 83 84 85 86 87 88 |
# File 'lib/shrine/plugins/transloadit.rb', line 80 def transloadit_process(cached_file = get) assembly = store.transloadit_process(cached_file, context) assembly.[:fields] ||= {} assembly.[:fields]["attacher"] = self.dump.merge("attachment" => cached_file.to_json) response = assembly.create! raise Error, "#{response["error"]}: #{response["message"]}" 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.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/shrine/plugins/transloadit.rb', line 97 def transloadit_save(response, valid: true) if versions = response["fields"]["versions"] stored_file = versions.inject({}) do |hash, (name, key)| result = response["results"].fetch(key)[0] uploaded_file = store.transloadit_uploaded_file(result) hash.update(name => uploaded_file) end else result = response["results"].values.last[0] stored_file = store.transloadit_uploaded_file(result) end if valid swap(stored_file) else _delete(stored_file, action: :abort) end end |