Skip to content

Instantly share code, notes, and snippets.

@dhoss
Created September 24, 2013 17:18
Show Gist options
  • Save dhoss/6688100 to your computer and use it in GitHub Desktop.
Save dhoss/6688100 to your computer and use it in GitHub Desktop.
carrierwave
CarrierWave.configure do |config|
#if Rails.env.test? or Rails.env.rspec?
# CarrierWave.configure do |config|
# config.storage = :file
# end
#end
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => ENV['AWS_ACCESS_ID'], # required
:aws_secret_access_key => ENV['AWS_SECRET_KEY'], # required
:host => 'http://s3.amazonaws.com',
:endpoint => 'https://s3.amazonaws.com' #ENV['RAILS_ENV'] == "production" ? "https://s3.****" : "http://localhost/"
}
config.fog_directory = ENV['RAILS_ENV'] == "production" ? "lumos-prod-gallery-images" : "lumos-dev-gallery-images"
config.fog_public = false
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}
config.enable_processing = true
end
require 'carrierwave/orm/activerecord'
class Photo < ActiveRecord::Base
belongs_to :gallery
mount_uploader :image, PhotoUploader
alias_attribute :gallery, :gallery_id
attr_accessible :description, :name, :image, :remote_image_url, :gallery_id
end
# encoding: utf-8
require 'carrierwave/processing/mime_types'
require 'digest/md5'
class PhotoUploader < CarrierWave::Uploader::Base
include CarrierWave::MimeTypes
include CarrierWave::RMagick
# Choose what kind of storage to use for this uploader:
storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
nil
end
def cache_dir
"#{Rails.root}/tmp/lumos_photos"
end
def extension_white_list
%w(jpg jpeg gif png)
end
def filename(uploaded_file=file)
if uploaded_file.present?
md5 = Digest::MD5.hexdigest(uploaded_file.filename)
@name ||= md5.scan(/^(..)(..)(..)(..)/).take(4).join('/') + "/#{md5}.#{uploaded_file.extension}"
end
end
version :thumbnail do
process :resized_to_fit #:resize_to_fit => [100, 10000]
end
def resized_to_fit
puts "RESIZING"
resize_to_fit(100, 10000)
puts "RESIZED"
pp file
end
process :set_content_type
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
# version :thumb do
# process :scale => [50, 50]
# end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_white_list
# %w(jpg jpeg gif png)
# end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end
class PhotosController < ApplicationController
before_filter :authenticate_user!, :only => [:edit, :update, :delete, :create, :new]
# GET /photos
# GET /photos.json
def index
@photos = Photo.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @photos }
end
end
# GET /photos/1
# GET /photos/1.json
def show
@photo = Photo.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @photo }
end
end
# GET /photos/new
# GET /photos/new.json
def new
@photo = Photo.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @photo }
end
end
# GET /photos/1/edit
def edit
@photo = Photo.find(params[:id])
end
# POST /photos
# POST /photos.json
def create
@photo = Photo.new(params[:photo])
respond_to do |format|
if @photo.save
format.html { redirect_to @photo, notice: 'Photo was successfully created.' }
format.json { render json: @photo, status: :created, location: @photo }
else
format.html { render action: "new" }
format.json { render json: @photo.errors, status: :unprocessable_entity }
end
end
end
# PUT /photos/1
# PUT /photos/1.json
def update
@photo = Photo.find(params[:id])
respond_to do |format|
if @photo.update_attributes(params[:photo])
format.html { redirect_to @photo, notice: 'Photo was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @photo.errors, status: :unprocessable_entity }
end
end
end
# DELETE /photos/1
# DELETE /photos/1.json
def destroy
@photo = Photo.find(params[:id])
@photo.destroy
respond_to do |format|
format.html { redirect_to photos_url }
format.json { head :no_content }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment