Skip to content

Instantly share code, notes, and snippets.

@pacarvalho
Created April 17, 2022 14:58
Show Gist options
  • Save pacarvalho/649d8d3e59962cd616ecaaf9836ce3e7 to your computer and use it in GitHub Desktop.
Save pacarvalho/649d8d3e59962cd616ecaaf9836ce3e7 to your computer and use it in GitHub Desktop.
Medium Article - Custom Backend Video Model
class Video < ApplicationRecord
UPLOAD_BUCKET = "medium-article-#{Rails.env}-videos"
belongs_to :user, optional: true
before_save :assign_random_key
def original_url
"https://#{ENV['CLOUDFRONT_DOMAIN']}/#{key}"
end
def low_res_url
return unless low_res_key
"https://#{ENV['CLOUDFRONT_DOMAIN']}/#{low_res_key}"
end
def upload_url
presigner = Aws::S3::Presigner.new
presigner.presigned_url(
:put_object,
bucket: UPLOAD_BUCKET,
key: key,
use_accelerate_endpoint: true,
expires_in: 900
)
end
private
def assign_random_key
return if key.present?
self.key = "uploads/#{user_id}/#{SecureRandom.uuid}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment