Commit 1aa717f2 by jyurek

Fixed up some issues regarding file names and deleting attachments.

git-svn-id: https://svn.thoughtbot.com/plugins/paperclip/trunk@169 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
parent 163c11de
...@@ -21,6 +21,7 @@ module Thoughtbot ...@@ -21,6 +21,7 @@ module Thoughtbot
attachments[attr] = (attachments[attr] || {:name => attr}).merge(options) attachments[attr] = (attachments[attr] || {:name => attr}).merge(options)
define_method "#{attr}=" do |uploaded_file| define_method "#{attr}=" do |uploaded_file|
return unless is_a_file? uploaded_file
attachments[attr].merge!({ attachments[attr].merge!({
:dirty => true, :dirty => true,
:files => {:original => uploaded_file}, :files => {:original => uploaded_file},
...@@ -39,6 +40,10 @@ module Thoughtbot ...@@ -39,6 +40,10 @@ module Thoughtbot
uploaded_file uploaded_file
end end
define_method attr do
self.send("#{attr}_file_name")
end
define_method "#{attr}_attachment" do define_method "#{attr}_attachment" do
attachments[attr] attachments[attr]
end end
...@@ -60,10 +65,16 @@ module Thoughtbot ...@@ -60,10 +65,16 @@ module Thoughtbot
File.file?( path_for(attachments[attr], style)) File.file?( path_for(attachments[attr], style))
end end
end end
define_method "destroy_#{attr}" do
if attachments[attr].keys.any?
delete_attachment attachments[attr]
end
end
define_method "#{attr}_after_save" do define_method "#{attr}_after_save" do
if attachments[attr].keys.any? if attachments[attr].keys.any?
write_attachment attachments[attr] write_attachment attachments[attr] if attachments[attr][:files]
attachments[attr][:dirty] = false attachments[attr][:dirty] = false
attachments[attr][:files] = nil attachments[attr][:files] = nil
end end
...@@ -84,6 +95,8 @@ module Thoughtbot ...@@ -84,6 +95,8 @@ module Thoughtbot
module InstanceMethods module InstanceMethods
private
def path_for attachment, style = :original def path_for attachment, style = :original
prefix = File.join(attachment[:path_prefix], attachment[:path]) prefix = File.join(attachment[:path_prefix], attachment[:path])
prefix.gsub!(/:rails_root/, RAILS_ROOT) prefix.gsub!(/:rails_root/, RAILS_ROOT)
...@@ -103,7 +116,7 @@ module Thoughtbot ...@@ -103,7 +116,7 @@ module Thoughtbot
end end
def ensure_directories_for attachment def ensure_directories_for attachment
attachment[:files].keys.each do |style| attachment[:files].each do |style, file|
dirname = File.dirname(path_for(attachment, style)) dirname = File.dirname(path_for(attachment, style))
FileUtils.mkdir_p dirname FileUtils.mkdir_p dirname
end end
...@@ -121,12 +134,15 @@ module Thoughtbot ...@@ -121,12 +134,15 @@ module Thoughtbot
def delete_attachment attachment def delete_attachment attachment
(attachment[:thumbnails].keys + [:original]).each do |style| (attachment[:thumbnails].keys + [:original]).each do |style|
FileUtils.rm path_for(attachment, style) file_path = path_for(attachment, style)
FileUtils.rm(file_path)
end end
self.update_attribute "#{attachment[:name]}_file_name", nil
self.update_attribute "#{attachment[:name]}_content_type", nil
end end
def make_thumbnail orig_io, geometry def make_thumbnail orig_io, geometry
thumb = IO.popen("convert - -scale '#{geometry}' - > /dev/stdout", "w+") do |io| thumb = IO.popen("convert - -scale '#{geometry}' -", "w+") do |io|
orig_io.rewind orig_io.rewind
io.write(orig_io.read) io.write(orig_io.read)
io.close_write io.close_write
...@@ -135,6 +151,12 @@ module Thoughtbot ...@@ -135,6 +151,12 @@ module Thoughtbot
raise "Convert returned with result code #{$?.exitstatus}." unless $?.success? raise "Convert returned with result code #{$?.exitstatus}." unless $?.success?
StringIO.new(thumb) StringIO.new(thumb)
end end
def is_a_file? data
[:size, :content_type, :original_filename].map do |meth|
data.respond_to? meth
end.all?
end
def sanitize_filename filename def sanitize_filename filename
File.basename(filename).gsub(/[^\w\.\_]/,'_') File.basename(filename).gsub(/[^\w\.\_]/,'_')
...@@ -155,6 +177,10 @@ module Thoughtbot ...@@ -155,6 +177,10 @@ module Thoughtbot
def original_filename def original_filename
self.path self.path
end end
def size
File.size(self)
end
end end
end end
end end
......
...@@ -17,9 +17,9 @@ class PaperclipImagesTest < Test::Unit::TestCase ...@@ -17,9 +17,9 @@ class PaperclipImagesTest < Test::Unit::TestCase
def test_should_save_the_file_and_its_thumbnails def test_should_save_the_file_and_its_thumbnails
assert @foo.save assert @foo.save
assert File.exists?( @foo.image_filename(:original) ) assert File.exists?( @foo.image_filename(:original) ), @foo.image_filename(:original)
assert File.exists?( @foo.image_filename(:medium) ) assert File.exists?( @foo.image_filename(:medium) ), @foo.image_filename(:medium)
assert File.exists?( @foo.image_filename(:thumb) ) assert File.exists?( @foo.image_filename(:thumb) ), @foo.image_filename(:thumb)
assert File.size?( @foo.image_filename(:original) ) assert File.size?( @foo.image_filename(:original) )
assert File.size?( @foo.image_filename(:medium) ) assert File.size?( @foo.image_filename(:medium) )
assert File.size?( @foo.image_filename(:thumb) ) assert File.size?( @foo.image_filename(:thumb) )
...@@ -36,10 +36,9 @@ class PaperclipImagesTest < Test::Unit::TestCase ...@@ -36,10 +36,9 @@ class PaperclipImagesTest < Test::Unit::TestCase
def test_should_delete_all_thumbnails_on_destroy def test_should_delete_all_thumbnails_on_destroy
assert @foo.save assert @foo.save
names = [:original, :medium, :thumb].map{|style| @foo.image_filename(style) }
assert @foo.destroy assert @foo.destroy
assert !File.exists?( @foo.image_filename(:original) ) names.each {|path| assert !File.exists?( path ), path }
assert !File.exists?( @foo.image_filename(:medium) )
assert !File.exists?( @foo.image_filename(:thumb) )
end end
end end
\ No newline at end of file
...@@ -33,10 +33,11 @@ class PaperclipTest < Test::Unit::TestCase ...@@ -33,10 +33,11 @@ class PaperclipTest < Test::Unit::TestCase
def test_should_delete_files_on_destroy def test_should_delete_files_on_destroy
assert @bar.save assert @bar.save
assert File.exists?( @bar.document_filename ) assert File.exists?( @bar.document_filename ), @bar.document_filename
document_filename = @bar.document_filename
assert @bar.destroy assert @bar.destroy
assert !File.exists?( @bar.document_filename ) assert !File.exists?( document_filename ), document_filename
end end
end end
\ No newline at end of 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