Skip to content

Instantly share code, notes, and snippets.

@mfilej
Created October 7, 2020 09:10
Show Gist options
  • Save mfilej/ec7afcb7227e83325cc9b56db136d659 to your computer and use it in GitHub Desktop.
Save mfilej/ec7afcb7227e83325cc9b56db136d659 to your computer and use it in GitHub Desktop.
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "shrine", "3.3.0"
gem "shrine-transloadit", "1.0"
gem "aws-sdk-s3", "1.16"
gem "activerecord", "6.0.3.3"
gem "pg"
end
require "shrine/storage/s3"
require "active_record"
ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "transloadit_repro", host: "postgres", username: "postgres", password: "postgres")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :media_items, if_not_exists: true do |t|
t.text :item_data
t.timestamps
end
end
options = {
access_key_id: ENV.fetch("AWS_ACCESS_KEY"),
secret_access_key: ENV.fetch("AWS_ACCESS_SECRET"),
region: ENV.fetch("AWS_S3_REGION"),
bucket: ENV.fetch("AWS_S3_BUCKET")
}
Shrine.storages = {
cache: Shrine::Storage::S3.new(prefix: "cache", **options),
store: Shrine::Storage::S3.new(**options),
}
Shrine.logger = Logger.new($stdout)
Shrine.plugin :instrumentation
class VideoUploader < Shrine
plugin :activerecord
plugin :derivatives
plugin :transloadit,
auth: {
key: ENV.fetch("TRANSLOADIT_KEY"),
secret: ENV.fetch("TRANSLOADIT_SECRET"),
},
credentials: {
cache: :repro,
store: :repro
}
Attacher.transloadit_processor do
import = file.transloadit_import_step
encode = transloadit_step "encode", "/video/encode", use: import
thumbs = transloadit_step "thumbs", "/video/thumbs", use: import
export = store.transloadit_export_step use: [import, encode, thumbs]
assembly = transloadit.assembly(steps: [import, encode, thumbs, export])
assembly.create!
end
Attacher.transloadit_saver do |results|
original = store.transloadit_file(results["import"])
transcoded = store.transloadit_file(results["encode"])
thumbnails = store.transloadit_files(results["thumbs"])
set(original)
merge_derivatives(transcoded: transcoded, thumbnails: thumbnails)
end
end
class MediaItem < ActiveRecord::Base
include VideoUploader::Attachment(:item)
end
File.open("/Users/miha/Screenshots/short.mp4", "r") do |f|
mi = MediaItem.create!(item: f)
attacher = mi.item_attacher
response = attacher.transloadit_process
response.reload_until_finished!
if response.error?
warn "vvv ERROR vvv"
p response
warn "^^^ ERROR ^^^"
else
attacher.transloadit_save(response["results"])
attacher.derivatives
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment