public
Rubygem
Description: A lightweight and opinionated but hackable library for attaching images to ActiveRecord models.
Homepage:
Clone URL: git://github.com/norman/has_image.git
Made storage work more correctly with tempfiles.
norman (author)
Tue Aug 19 12:05:04 -0700 2008
commit  f2b1cb0fd3a417d616682612346febcd3fc68654
tree    4f2e58e5dfca5d410bdac1a2264b9fa861ce3115
parent  bda419354f2cdd5ea331be2c45158fc502778e96
...
 
 
 
1
2
3
...
1
2
3
4
5
6
0
@@ -1,3 +1,6 @@
0
+2008-08-19 Norman Clarke <norman@randomba.org>
0
+  * Made storage work more correctly with tempfiles.
0
+  
0
 2008-08-18 Norman Clarke <norman@randomba.org>
0
   * Fixed ability to set the path for storing image files.
0
 
...
1
2
3
4
 
 
5
6
7
...
1
2
 
 
3
4
5
6
7
0
@@ -1,7 +1,7 @@
0
 Gem::Specification.new do |s|
0
   s.name = "has_image"
0
-  s.version = "0.1.8"
0
-  s.date = "2008-08-18"
0
+  s.version = "0.1.9"
0
+  s.date = "2008-08-19"
0
   s.add_dependency('mini_magick', '>= 1.2.3')
0
   s.rubyforge_project = 'has-image'  
0
   s.summary = "Lets you attach images with thumbnails to active record models."
...
31
32
33
34
 
35
36
37
...
51
52
53
54
 
55
56
57
...
77
78
79
80
81
82
83
 
 
 
 
84
85
86
...
137
138
139
140
 
 
 
 
141
142
143
...
148
149
150
151
 
 
 
 
152
153
154
...
31
32
33
 
34
35
36
37
...
51
52
53
 
54
55
56
57
...
77
78
79
 
 
 
 
80
81
82
83
84
85
86
...
137
138
139
 
140
141
142
143
144
145
146
...
151
152
153
 
154
155
156
157
158
159
160
0
@@ -31,7 +31,7 @@ module HasImage
0
       # names, and they end up being hard to manipulate on the command line.
0
       # This also helps prevent a possibly undesirable sitation where the
0
       # uploaded images have offensive names.
0
-      def random_file_name
0
+      def generated_file_name
0
         Zlib.crc32(Time.now.to_s + rand(10e10).to_s).to_s(36).downcase
0
       end
0
           
0
@@ -51,7 +51,7 @@ module HasImage
0
         @temp_file = image_data
0
       else
0
         image_data.rewind
0
-        @temp_file = Tempfile.new 'has_image_data_%s' % Storage.random_file_name
0
+        @temp_file = Tempfile.new 'has_image_data_%s' % Storage.generated_file_name
0
         @temp_file.write(image_data.read)        
0
       end
0
     end
0
@@ -77,10 +77,10 @@ module HasImage
0
     # Invokes the processor to resize the image(s) and the installs them to
0
     # the appropriate directory.
0
     def install_images(id)
0
-      random_name = Storage.random_file_name
0
-      install_main_image(id, random_name)
0
-      install_thumbnails(id, random_name) if !options[:thumbnails].empty?
0
-      return random_name
0
+      generated_name = Storage.generated_file_name
0
+      install_main_image(id, generated_name)
0
+      install_thumbnails(id, generated_name) if !options[:thumbnails].empty?
0
+      return generated_name
0
     ensure  
0
       @temp_file.close! if !@temp_file.closed?
0
       @temp_file = nil
0
@@ -137,7 +137,10 @@ module HasImage
0
     def install_main_image(id, name)
0
       FileUtils.mkdir_p path_for(id)
0
       main = processor.resize(@temp_file, @options[:resize_to])
0
-      main.write(File.join(path_for(id), file_name_for(name)))
0
+      main.tempfile.close
0
+      file = File.open(File.join(path_for(id), file_name_for(name)), "w")
0
+      file.write(IO.read(main.tempfile.path))
0
+      file.close
0
       main.tempfile.close!
0
     end
0
     
0
@@ -148,7 +151,10 @@ module HasImage
0
       path = File.join(path_for(id), file_name_for(name))
0
       options[:thumbnails].each do |thumb_name, size|
0
         thumb = processor.resize(path, size)
0
-        thumb.write(File.join(path_for(id), file_name_for(name, thumb_name)))
0
+        thumb.tempfile.close
0
+        file = File.open(File.join(path_for(id), file_name_for(name, thumb_name)), "w")
0
+        file.write(IO.read(thumb.tempfile.path))
0
+        file.close
0
         thumb.tempfile.close!
0
       end
0
     end
...
25
26
27
28
29
 
 
30
31
32
...
61
62
63
64
 
 
65
66
67
...
25
26
27
 
 
28
29
30
31
32
...
61
62
63
 
64
65
66
67
68
0
@@ -25,8 +25,8 @@ class StorageTest < Test::Unit::TestCase
0
       HasImage::Storage.partitioned_path(867792731)
0
   end
0
   
0
-  def test_random_file_name
0
-    assert_match(/[a-z0-9]{4,6}/i, HasImage::Storage.random_file_name)
0
+  def test_generated_file_name
0
+    assert_match(/[a-z0-9]{4,6}/i, HasImage::Storage.generated_file_name)
0
   end
0
   
0
   def test_path_for
0
@@ -61,7 +61,8 @@ class StorageTest < Test::Unit::TestCase
0
   end
0
   
0
   def test_install_and_remove_images
0
-    @storage = HasImage::Storage.new(default_options)
0
+    @storage = HasImage::Storage.new(default_options.merge(:thumbnails => {
0
+      :one => "100x100", :two => "200x200"}))
0
     @storage.image_data = temp_file("image.jpg")
0
     assert @storage.install_images(1)
0
     assert @storage.remove_images(1)    

Comments