Skip to content

Commit

Permalink
raise AttachmentFu::AssetMissing if the asset #full_path does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Aug 21, 2009
1 parent 5a1ee82 commit eb1ac9f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
20 changes: 14 additions & 6 deletions lib/attachment_fu.rb
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions spec/attachment_fu_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit eb1ac9f

Please sign in to comment.