Commit 10968ea3 by Jon Yurek

Added logging to paperclip.rb and attachment.rb

parent 2ff75d11
......@@ -43,6 +43,8 @@ module Paperclip
normalize_style_definition
initialize_storage
logger.info("[paperclip] Paperclip attachment #{name} on #{instance.class} initialized.")
end
# What gets called when you call instance.attachment = File. It clears errors,
......@@ -50,6 +52,7 @@ module Paperclip
# the previous file for deletion, to be flushed away on #save of its host.
def assign uploaded_file
return nil unless valid_assignment?(uploaded_file)
logger.info("[paperclip] Assigning #{uploaded_file} to #{name}")
queue_existing_for_delete
@errors = []
......@@ -57,6 +60,7 @@ module Paperclip
return nil if uploaded_file.nil?
logger.info("[paperclip] Writing attributes for #{name}")
@queued_for_write[:original] = uploaded_file.to_tempfile
@instance[:"#{@name}_file_name"] = uploaded_file.original_filename.strip.gsub /[^\w\d\.\-]+/, '_'
@instance[:"#{@name}_content_type"] = uploaded_file.content_type.strip
......@@ -111,11 +115,13 @@ module Paperclip
# the instance's errors and returns false, cancelling the save.
def save
if valid?
logger.info("[paperclip] Saving files for #{name}")
flush_deletes
flush_writes
@dirty = false
true
else
logger.info("[paperclip] Errors on #{name}. Not saving.")
flush_errors
false
end
......@@ -173,6 +179,10 @@ module Paperclip
private
def logger
instance.logger
end
def valid_assignment? file #:nodoc:
file.nil? || (file.respond_to?(:original_filename) && file.respond_to?(:content_type))
end
......@@ -202,6 +212,7 @@ module Paperclip
def post_process #:nodoc:
return if @queued_for_write[:original].nil?
logger.info("[paperclip] Post-processing #{name}")
@styles.each do |name, args|
begin
dimensions, format = args
......@@ -228,6 +239,7 @@ module Paperclip
def queue_existing_for_delete #:nodoc:
return if original_filename.blank?
logger.info("[paperclip] Queueing the existing files for #{name} for deletion.")
@queued_for_delete += [:original, *@styles.keys].uniq.map do |style|
path(style) if exists?(style)
end.compact
......
......@@ -36,8 +36,10 @@ module Paperclip
alias_method :to_io, :to_file
def flush_writes #:nodoc:
logger.info("[paperclip] Writing files for #{name}")
@queued_for_write.each do |style, file|
FileUtils.mkdir_p(File.dirname(path(style)))
logger.info("[paperclip] -> #{path(style)}")
result = file.stream_to(path(style))
file.close
result.close
......@@ -46,8 +48,10 @@ module Paperclip
end
def flush_deletes #:nodoc:
logger.info("[paperclip] Deleting files for #{name}")
@queued_for_delete.each do |path|
begin
logger.info("[paperclip] -> #{path}")
FileUtils.rm(path) if File.exist?(path)
rescue Errno::ENOENT => e
# ignore file-not-found, let everything else pass
......@@ -102,6 +106,7 @@ module Paperclip
base.class.interpolations[:s3_url] = lambda do |attachment, style|
"https://s3.amazonaws.com/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
end
ActiveRecord::Base.logger.info("[paperclip] S3 Storage Initalized.")
end
def s3
......@@ -135,8 +140,10 @@ module Paperclip
alias_method :to_io, :to_file
def flush_writes #:nodoc:
logger.info("[paperclip] Writing files for #{name}")
@queued_for_write.each do |style, file|
begin
logger.info("[paperclip] -> #{path(style)}")
key = s3_bucket.key(path(style))
key.data = file
key.put(nil, @s3_permissions)
......@@ -148,8 +155,10 @@ module Paperclip
end
def flush_deletes #:nodoc:
logger.info("[paperclip] Writing files for #{name}")
@queued_for_delete.each do |path|
begin
logger.info("[paperclip] -> #{path(style)}")
if file = s3_bucket.key(path)
file.delete
end
......
......@@ -154,6 +154,7 @@ class AttachmentTest < Test::Unit::TestCase
@instance.stubs(:[]).with(:test_file_name).returns(nil)
@instance.stubs(:[]).with(:test_content_type).returns(nil)
@instance.stubs(:[]).with(:test_file_size).returns(nil)
@instance.stubs(:logger).returns(ActiveRecord::Base.logger)
@attachment = Paperclip::Attachment.new(:test,
@instance)
@file = File.new(File.join(File.dirname(__FILE__),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment