Shrine.rb
Install Shrine gem, config initializer, plus adds some handy uploaders you can use.
Used 29 times
D
Dale Zak
Usage
Define your environment variables:
- AWS_REGION
- AWS_BUCKET_NAME
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
Add image to your model:
- include ImageUploader::Attachment(:image)
- include ::Imageable
Add audio to your model:
- include AudioUploader::Attachment(:audio)
Add video to your model:
- include VideoUploader::Attachment(:video)
Add document to your model:
- include DocumentUploader::Attachment(:document)
Run this command in your Rails app directory in the terminal:
rails app:template LOCATION="https://railsbytes.com/script/xYasLK"
Template Source
Review the code before running this template on your machine.
def do_bundle
Bundler.with_unbundled_env { run "bundle install" }
end
run 'bundle add shrine'
run 'bundle add fastimage'
run 'bundle add aws-sdk-s3'
run 'bundle add image_processing'
do_bundle
file 'config/initializers/shrine.rb', <<-CODE
require "shrine"
Shrine.plugin :activerecord
Shrine.plugin :cached_attachment_data
Shrine.plugin :restore_cached_data
Shrine.plugin :validation
Shrine.plugin :validation_helpers
Shrine.plugin :pretty_location
Shrine.plugin :derivatives
Shrine.plugin :infer_extension
Shrine.plugin :determine_mime_type
Shrine.plugin :store_dimensions
Shrine.plugin :remote_url, max_size: 20*1024*1024
if Rails.env.production?
require "shrine/storage/s3"
s3_options = {
region: ENV['AWS_REGION'],
bucket: ENV['AWS_BUCKET_NAME'],
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
}
Shrine.storages = {
cache: Shrine::Storage::S3.new(public: true, prefix: "cache", upload_options: { acl: "public-read" }, **s3_options),
store: Shrine::Storage::S3.new(public: true, upload_options: { acl: "public-read" }, **s3_options)
}
elsif Rails.env.development?
require "shrine/storage/file_system"
Shrine.storages = {
cache: Shrine::Storage::FileSystem.new("public", prefix: "uploads/cache"),
store: Shrine::Storage::FileSystem.new("public", prefix: "uploads")
}
else
require "shrine/storage/memory"
Shrine.storages = {
cache: Shrine::Storage::Memory.new(),
store: Shrine::Storage::Memory.new()
}
end
CODE
file 'app/uploaders/image_uploader.rb', <<-CODE
require 'image_processing/mini_magick'
require 'fastimage'
class ImageUploader < Shrine
plugin :derivatives
plugin :pretty_location
plugin :store_dimensions
plugin :validation_helpers
plugin :determine_mime_type
Attacher.validate do
validate_mime_type %w[image/jpeg image/png]
validate_extension %w[jpg jpeg png]
validate_max_size 10*1024*1024
end
Attacher.derivatives do |original|
magick = ImageProcessing::MiniMagick.source(original)
{
thumb: magick.resize_to_limit!(100, 100),
small: magick.resize_to_limit!(300, 300),
medium: magick.resize_to_limit!(600, 600),
large: magick.resize_to_limit!(1200, 1200),
}
end
end
CODE
file 'app/uploaders/document_uploader.rb', <<-CODE
class DocumentUploader < Shrine
plugin :pretty_location
plugin :validation_helpers
plugin :determine_mime_type
Attacher.validate do
validate_mime_type %w[application/pdf application/msword application/vnd.openxmlformats-officedocument.wordprocessingml.document]
validate_extension %w[pdf doc docx]
validate_max_size 25*1024*1024
end
end
CODE
file 'app/uploaders/audio_uploader.rb', <<-CODE
class AudioUploader < Shrine
plugin :pretty_location
plugin :validation_helpers
plugin :determine_mime_type
Attacher.validate do
validate_mime_type %w[audio/mpeg audio/mp4 audio/ogg]
validate_extension %w[mp3 mp4 ogg]
validate_max_size 25*1024*1024
end
end
CODE
file 'app/uploaders/video_uploader.rb', <<-CODE
class VideoUploader < Shrine
plugin :pretty_location
plugin :validation_helpers
plugin :determine_mime_type
Attacher.validate do
validate_mime_type %w[video/x-msvideo video/quicktime video/mpeg video/mp4 video/x-ms-wmv video/ogg]
validate_extension %w[avi mov mpeg mp4 wmv ogv]
validate_max_size 25*1024*1024
end
end
CODE
file 'app/models/concerns/imageable.rb', <<-CODE
module Imageable
extend ActiveSupport::Concern
included do
before_save :image_resize
end
def image_resize
self.image_derivatives! if self.image_changed?
end
end
CODE