Skip to content

Instantly share code, notes, and snippets.

@rodrigotassinari
Created August 4, 2009 20:23
Show Gist options
  • Save rodrigotassinari/161515 to your computer and use it in GitHub Desktop.
Save rodrigotassinari/161515 to your computer and use it in GitHub Desktop.
class TempImage < Image
has_attachment({
:storage => :file_system,
:path_prefix => "public/uploads/#{table_name}",
}.merge(COMMON_ATTACHMENT_OPTIONS)
)
after_create :queue_move_to_s3
def move_to_s3
local_image = self
pseudo_s3_image = Image.find(id)
AWS::S3::Base.establish_connection!(
:access_key_id => pseudo_s3_image.s3_config[:access_key_id],
:secret_access_key => pseudo_s3_image.s3_config[:secret_access_key]
)
logger.debug "TempImage ##{id}: Connected to S3"
logger.debug "TempImage ##{id}: Sending file..."
response = AWS::S3::S3Object.store(
pseudo_s3_image.full_filename,
File.open(local_image.full_filename),
pseudo_s3_image.bucket_name,
:content_type => local_image.content_type,
:access => pseudo_s3_image.attachment_options[:s3_access]
)
raise "Unable to store #{local_image.full_filename} in S3" unless response.success?
logger.debug "TempImage ##{id}: File sent!"
logger.debug "TempImage ##{id}: Toggling temporary status..."
pseudo_s3_image.toggle!(:temporary)
logger.debug "TempImage ##{id}: Erasing local file..."
FileUtils.rm local_image.full_filename
logger.debug "TempImage ##{id}: Erasing empty dirs..."
Utilities.delete_empty_directories("#{Rails.root}/#{local_image.attachment_options[:path_prefix]}")
logger.debug "TempImage ##{id}: Done!"
true
end
def destroy
raise "should call destroy on the permanent image instead"
end
protected
# after_create: calls move_to_s3 asynchronously (via delayed_job)
def queue_move_to_s3
# método send_later fornecido pelo DelayedJob
send_later(:move_to_s3)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment