Permalink
Browse files

raise AttachmentFu::AssetMissing if the asset #full_path does not exist

  • Loading branch information...
1 parent 5a1ee82 commit eb1ac9fdc30ce6cd2dc0052aaf11e50346cb17c4 @technoweenie committed Aug 21, 2009
Showing with 24 additions and 6 deletions.
  1. +14 −6 lib/attachment_fu.rb
  2. +10 −0 spec/attachment_fu_spec.rb
View
@@ -13,6 +13,14 @@ def make_tmpname(basename, n)
end
module AttachmentFu
+ class AssetMissing < StandardError
+ def initialize(full_path)
+ name = File.basename(full_path)
+ path = File.dirname(full_path)
+ super "#{name.inspect} not found in #{path.inspect}"
+ end
+ end
+
@@image_content_types = Set.new [
'image/jpeg',
'image/pjpeg',
@@ -350,15 +358,13 @@ def rename_attachment
# Saves the attachment to the file system. It also processes
# or queues the attachment for processing.
def save_attachment
- return if @temp_path.nil?
- old_path = File.expand_path(full_path_for(@temp_path))
- return if old_path.nil?
- unless old_path == full_path
+ return if @temp_path.nil? || @old_asset_path.nil?
+ unless @old_asset_path == full_path
FileUtils.mkdir_p(File.dirname(full_path))
- FileUtils.mv(old_path, full_path)
+ FileUtils.mv(@old_asset_path, full_path)
end
File.chmod(0644, full_path)
- @temp_path = nil # if a task tries to re-save, we don't want to re-store the attachment
+ @temp_path = @old_asset_path = nil # if a task tries to re-save, we don't want to re-store the attachment
queued_attachment ? queue_processing : process
@new_attachment = nil
end
@@ -395,6 +401,8 @@ def strip_filename(value)
# Needed to tell the difference between an attachment that has just been saved,
# vs one saved in a previous request or object instantiation.
def set_new_attachment
+ @old_asset_path = File.expand_path(full_path_for(@temp_path))
+ if !File.exist?(@old_asset_path) then raise AssetMissing.new(@old_asset_path) end
@new_attachment = true
end
end
View
@@ -23,6 +23,16 @@ class QueuedAsset < ActiveRecord::Base
it "has nil #partitioned_path" do
@asset.partitioned_path.should == nil
end
+
+ it "fails to create asset if #full_path does not exist" do
+ @file = File.join(File.dirname(__FILE__), 'guinea_pig.rb')
+ FileUtils.cp __FILE__, @file
+
+ @asset = BasicAsset.new(:content_type => 'application/x-ruby')
+ @asset.set_temp_path @file
+ FileUtils.rm_rf @file
+ lambda { @asset.save! }.should raise_error(AttachmentFu::AssetMissing)
+ end
end
describe "being processed" do

0 comments on commit eb1ac9f

Please sign in to comment.