Module: Shrine::Plugins::Transloadit::InstanceMethods
- Defined in:
- lib/shrine/plugins/transloadit.rb
Instance Method Summary collapse
-
#transloadit ⇒ Object
An cached instance of a Tranloadit client.
-
#transloadit_assembly(value, context: {}, **options) ⇒ Object
Accepts a TransloaditFile, a hash of TransloaditFiles or a template, and converts it into a Transloadit::Assembly.
-
#transloadit_export_step(name, path: nil, **step_options) ⇒ Object
Generates an export step from the current (permanent) storage.
-
#transloadit_file(io = nil) ⇒ Object
Creates a new TransloaditFile for building steps, with an optional import step applied.
-
#transloadit_import_step(name, io, **step_options) ⇒ Object
Generates a Transloadit import step from the Shrine::UploadedFile.
-
#transloadit_uploaded_file(result) ⇒ Object
Converts Transloadit’s representation of an uploaded file into Shrine’s representation.
Instance Method Details
#transloadit ⇒ Object
An cached instance of a Tranloadit client.
282 283 284 |
# File 'lib/shrine/plugins/transloadit.rb', line 282 def transloadit @transloadit ||= self.class.transloadit end |
#transloadit_assembly(value, context: {}, **options) ⇒ Object
Accepts a TransloaditFile, a hash of TransloaditFiles or a template, and converts it into a Transloadit::Assembly.
If a hash of versions are given, the version information is saved in assembly’s payload, so that later in the webhook it can be used to construct a hash of versions.
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/shrine/plugins/transloadit.rb', line 263 def transloadit_assembly(value, context: {}, **) = { steps: [], fields: {} }.merge() case value when TransloaditFile then transloadit_assembly_update_single!(value, context, ) when Hash then transloadit_assembly_update_versions!(value, context, ) when String then transloadit_assembly_update_template!(value, context, ) else raise Error, "Assembly value has to be either a TransloaditFile, a hash of TransloaditFile objects, or a template" end if [:steps].uniq(&:name) != [:steps] raise Error, "There are different transloadit steps using the same name" end transloadit.assembly() end |
#transloadit_export_step(name, path: nil, **step_options) ⇒ Object
Generates an export step from the current (permanent) storage. At the moment only Amazon S3 is supported.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/shrine/plugins/transloadit.rb', line 229 def transloadit_export_step(name, path: nil, **) if defined?(Storage::S3) && storage.is_a?(Storage::S3) path ||= "${unique_prefix}/${file.url_name}" # Transloadit's default path step = transloadit.step(name, "/s3/store", key: storage.client.config.access_key_id, secret: storage.client.config.secret_access_key, bucket: storage.bucket.name, bucket_region: storage.client.config.region, path: [*storage.prefix, path].join("/"), ) else raise Error, "Cannot construct a transloadit export step from #{storage.inspect}" end step..update() step end |
#transloadit_file(io = nil) ⇒ Object
Creates a new TransloaditFile for building steps, with an optional import step applied.
251 252 253 254 255 |
# File 'lib/shrine/plugins/transloadit.rb', line 251 def transloadit_file(io = nil) file = TransloaditFile.new(transloadit: transloadit) file = file.add_step(transloadit_import_step("import", io)) if io file end |
#transloadit_import_step(name, io, **step_options) ⇒ Object
Generates a Transloadit import step from the Shrine::UploadedFile. If it’s from the S3 storage, an S3 import step will be generated. Otherwise either a generic HTTP(S) or an FTP import will be generated.
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/shrine/plugins/transloadit.rb', line 196 def transloadit_import_step(name, io, **) uri = URI.parse(io.url) if defined?(Storage::S3) && io.storage.is_a?(Storage::S3) step = transloadit.step(name, "/s3/import", key: io.storage.client.config.access_key_id, secret: io.storage.client.config.secret_access_key, bucket: io.storage.bucket.name, bucket_region: io.storage.client.config.region, path: [*io.storage.prefix, io.id].join("/"), ) elsif uri.scheme == "http" || uri.scheme == "https" step = transloadit.step(name, "/http/import", url: uri.to_s, ) elsif uri.scheme == "ftp" step = transloadit.step(name, "/ftp/import", host: uri.host, user: uri.user, password: uri.password, path: uri.path, ) else raise Error, "Cannot construct a transloadit import step from #{io.inspect}" end step..update() step end |
#transloadit_uploaded_file(result) ⇒ Object
Converts Transloadit’s representation of an uploaded file into Shrine’s representation. It currently only accepts files exported to S3. All Transloadit’s metadata is saved into a “transloadit” attribute.
When doing direct uploads to Transloadit you will only get a temporary URL, which will be saved in the “id” attribute and it’s expected that the URL storage is used.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/shrine/plugins/transloadit.rb', line 169 def transloadit_uploaded_file(result) case url = result.fetch("url") when /amazonaws\.com/ raise Error, "Cannot save a processed file which wasn't exported: #{url.inspect}" if url.include?("tmp.transloadit.com") path = URI(url).path id = path.match(%r{^(/#{storage.prefix})?/}).post_match else raise Error, "The transloadit Shrine plugin doesn't support storage identified by #{url.inspect}" end self.class::UploadedFile.new( "id" => id, "storage" => storage_key.to_s, "metadata" => { "filename" => result.fetch("name"), "size" => result.fetch("size"), "mime_type" => result.fetch("mime"), "width" => (result["meta"] && result["meta"]["width"]), "height" => (result["meta"] && result["meta"]["height"]), "transloadit" => result["meta"], } ) end |