Created
January 10, 2011 20:34
-
-
Save cannikin/773408 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Image < ActiveRecord::Base | |
belongs_to :user | |
belongs_to :attachment, :polymorphic => true | |
# validates_presence_of :url | |
before_save :check_for_defaults | |
default_scope :order => 'id asc' | |
META = { :small => { :width => 150, :height => 150, :prefix => 's_', :watermark => File.join(Rails.root,'public','images',"watermark-small.png")}, | |
:large => { :width => 500, :height => 500, :prefix => 'l_', :watermark => File.join(Rails.root,'public','images',"watermark-large.png")}} | |
DEFAULTS = { :title => 'Title', :description => 'Description' } | |
OUTPUT_FORMAT = 'jpg' | |
PROFILE_PHOTO_NAME = 'profile.' + OUTPUT_FORMAT | |
PATH_PREFIX = '/images' | |
DEFAULT_IMAGES = {:workout => Image.new(:image_path => 'default_workout.png', :attachment_type => 'Workout'), | |
:plan => Image.new(:image_path => 'default_workout.png', :attachment_type => 'Plan')} | |
# for holding a file that's about to be uploaded (this isn't actually saved to the DB) | |
attr_accessor :file | |
def full_path_prefix | |
S3_CONFIG[:object_prefix].to_s + PATH_PREFIX + '/' + self.attachment_type.downcase | |
end | |
def url(size=:small) | |
S3_CONFIG[:url] + self.full_path_prefix + '/' + META[size][:prefix] + self.image_path | |
end | |
# the date to display for this photo | |
def display_date | |
if self.taken_on | |
"Taken on #{self.taken_on.to_s(:long)}" | |
else | |
"Uploaded on #{self.created_at.to_s(:long)}" | |
end | |
end | |
# takes the current instance and uploaded its attached file to S3 | |
def upload | |
if self.file | |
remote_filename = UUID.new.generate + '.' + OUTPUT_FORMAT # filename becomes the timestamp to avoid name conflicts | |
# upload the large size and then, assuming it works, upload the rest | |
#begin | |
if self.image_path = create_and_upload_image(:large, remote_filename) | |
create_and_upload_image(:small, remote_filename) | |
return true | |
else | |
return false | |
end | |
#rescue => e | |
# logger.error("** ERROR: Problem uploading to S3: #{e.message}\n\n#{e.backtrace}\n\n") | |
# HoptoadNotifier.notify(e) | |
#end | |
else | |
raise 'No file attached to photo' | |
end | |
end | |
private | |
# since we populate the title and description text boxes with default values, we need to set them to blank if they still contain the defaults | |
def check_for_defaults | |
DEFAULTS.each do |key,value| | |
self.send(key.to_s+'=','') if self.send(key.to_s) == value | |
end | |
end | |
# Creates and uploads a single image (the model should know everything it needs to about an image, including how to save itself wherever) | |
# Returns the name of the file it just uploaded (or throws an error if something goes wrong) | |
def create_and_upload_image(size,filename) | |
#begin | |
image = MiniMagick::Image.from_file(self.file.tempfile) # get the uploaded image | |
final_image_name = META[size][:prefix] + filename # figure out the final name | |
s3_path = File.join(self.full_path_prefix, final_image_name) # figure out the final S3 path | |
image.resize "#{META[size][:width]}x#{META[size][:height]}>" # 250x188> standard ImageMagick geometry | |
AWS::S3::S3Object.store(s3_path, image.tempfile.open, S3_CONFIG[:bucket_name], :access => :public_read) # reopen the tempfile and write directly to S3 | |
return filename | |
#rescue => e | |
# logger.error("** ERROR: Problem uploading to S3: #{e.message}\n\n#{e.backtrace}\n\n") | |
# HoptoadNotifier.notify(e) | |
#ensure | |
#File.delete(composite_output_path) # the final composited file | |
#[watermark.tempfile, image.tempfile, output.tempfile].each { |f| f.close! unless f.nil? } # any temp files that mini_magick was using | |
# image.tempfile.close! unless image.tempfile.nil? | |
#end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment