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, **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.
272 273 274 |
# File 'lib/shrine/plugins/transloadit.rb', line 272 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.
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/shrine/plugins/transloadit.rb', line 233 def transloadit_assembly(value, context: {}, **) [:steps] ||= [] [:fields] ||= {} if (versions = value).is_a?(Hash) [:fields]["versions"] = {} raise Error, "The versions Shrine plugin isn't loaded" if !defined?(Shrine::Plugins::Versions) versions.each do |name, transloadit_file| raise Error, "The given TransloaditFile is missing an import step" if !transloadit_file.imported? unless transloadit_file.exported? path = generate_location(transloadit_file, context.merge(version: name)) + ".${file.ext}" export_step = transloadit_export_step("export_#{name}", path: path) transloadit_file = transloadit_file.add_step(export_step) end [:steps] |= transloadit_file.steps [:fields]["versions"][name] = transloadit_file.name end elsif (transloadit_file = value).is_a?(TransloaditFile) raise Error, "The given TransloaditFile is missing an import step" if !transloadit_file.imported? unless transloadit_file.exported? path = generate_location(transloadit_file, context) + ".${file.ext}" export_step = transloadit_export_step("export", path: path) transloadit_file = transloadit_file.add_step(export_step) end [:steps] += transloadit_file.steps elsif (template = value).is_a?(String) [:template_id] = template else raise Error, "First argument has to be a TransloaditFile, a hash of TransloaditFiles, 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, **step_options) ⇒ Object
Generates an export step from the current (permanent) storage. At the moment only Amazon S3 is supported.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/shrine/plugins/transloadit.rb', line 202 def transloadit_export_step(name, **) if defined?(Storage::S3) && storage.is_a?(Storage::S3) step = transloadit.step(name, "/s3/store", key: storage.s3.client.config.access_key_id, secret: storage.s3.client.config.secret_access_key, bucket: storage.bucket.name, bucket_region: storage.s3.client.config.region ) 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.
221 222 223 224 225 |
# File 'lib/shrine/plugins/transloadit.rb', line 221 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.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/shrine/plugins/transloadit.rb', line 169 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.s3.client.config.access_key_id, secret: io.storage.s3.client.config.secret_access_key, bucket: io.storage.bucket.name, bucket_region: io.storage.s3.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.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/shrine/plugins/transloadit.rb', line 142 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(/^\/#{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 |