Module: Shrine::Plugins::Transloadit::AttacherClassMethods

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

Instance Method Summary collapse

Instance Method Details

#check_transloadit_signature!(params) ⇒ Object

Checks if the webhook has indeed been triggered by Transloadit, by checking if sent signature matches the calculated signature, and raising a ‘Shrine::Error` if signatures don’t match.

Raises:

  • (Error)


59
60
61
62
63
64
65
66
# File 'lib/shrine/plugins/transloadit.rb', line 59

def check_transloadit_signature!(params)
  sent_signature = params["signature"]
  payload = params["transloadit"]
  algorithm = OpenSSL::Digest.new('sha1')
  secret = shrine_class.opts[:transloadit_auth_secret]
  calculated_signature = OpenSSL::HMAC.hexdigest(algorithm, secret, payload)
  raise Error, "Transloadit signature that was sent doesn't match the calculated signature" if calculated_signature != sent_signature
end

#transloadit_process(data) ⇒ Object

Loads the attacher from the data, and triggers Transloadit processing. Intended to be used in a background job.



35
36
37
38
39
40
# File 'lib/shrine/plugins/transloadit.rb', line 35

def transloadit_process(data)
  attacher = self.load(data)
  cached_file = attacher.uploaded_file(data["attachment"])
  attacher.transloadit_process(cached_file)
  attacher
end

#transloadit_save(params) ⇒ Object

This should be called when receiving a webhook, where arguments are params that Transloadit POSTed to the endpoint. It checks the signature, loads the attacher, saves processing results to the record.



45
46
47
48
49
50
51
52
53
54
# File 'lib/shrine/plugins/transloadit.rb', line 45

def transloadit_save(params)
  params["transloadit"] = params["transloadit"].to_json if params["transloadit"].is_a?(Hash)
  check_transloadit_signature!(params)
  response = JSON.parse(params["transloadit"])
  data = response["fields"]["attacher"]
  attacher = self.load(data)
  cached_file = attacher.uploaded_file(data["attachment"])
  attacher.transloadit_save(response, valid: attacher.get == cached_file)
  attacher
end