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
Improved partitioned path handling to avoid collisions when the id is very
high, which can happen if you use db:fixtures:load.
norman (author)
Fri Aug 01 17:04:40 -0700 2008
commit  bc10e72c80c78d3ff240a2eb7b5018594e74c5d0
tree    ef9e53d8af7ecaa493283bdedb9ad3b0f915f962
parent  52bfffe2cc0947400efe31f342689835967a19f5
...
 
 
 
 
1
2
3
4
5
...
1
2
3
4
5
 
6
7
8
0
@@ -1,5 +1,8 @@
0
+2008-08-1 Adrian Mugnolo <adrian@randomba.org>
0
+  * Improved partitioned path handling to avoid collisions when the id is
0
+    very high, which can happen if you use db:fixtures:load.
0
+
0
 2008-08-1 Norman Clarke <norman@randomba.org>
0
-  
0
   * Fixed a bug where overwriting a previous image triggered callbacks more
0
     than once, causing errors.
0
 
...
1
2
3
 
4
5
6
...
1
2
 
3
4
5
6
0
@@ -1,6 +1,6 @@
0
 Gem::Specification.new do |s|
0
   s.name = "has_image"
0
-  s.version = "0.1.6"
0
+  s.version = "0.1.7"
0
   s.date = "2008-08-01"
0
   s.add_dependency('mini_magick', '>= 1.2.3')
0
   s.rubyforge_project = 'has-image'  
...
6
7
8
9
10
11
 
 
 
 
12
13
14
15
16
17
18
 
 
 
 
 
19
20
 
21
22
23
24
25
26
27
28
 
 
 
 
 
 
29
30
31
...
6
7
8
 
 
 
9
10
11
12
13
14
15
16
17
18
 
19
20
21
22
23
24
 
25
26
27
 
 
 
 
 
 
28
29
30
31
32
33
34
35
36
0
@@ -6,26 +6,31 @@ require 'zlib'
0
 module HasImage  
0
   
0
   # Filesystem storage for the HasImage gem. The methods that HasImage inserts
0
-  # into ActiveRecord models only depend on the public methods in this class, so
0
-  # it should be reasonably straightforward to implement a different storage
0
-  # mechanism for Amazon AWS, Photobucket, DBFile, SFTP, or whatever you want.  
0
+  # into ActiveRecord models only depend on the public methods in this class,
0
+  # so it should be reasonably straightforward to implement a different
0
+  # storage mechanism for Amazon AWS, Photobucket, DBFile, SFTP, or whatever
0
+  # you want.  
0
   class Storage
0
     
0
     attr_accessor :image_data, :options, :temp_file
0
 
0
     class << self
0
       
0
-      # Stolen from {Jamis Buck}[http://www.37signals.com/svn/archives2/id_partitioning.php].
0
+      # {Jamis Buck's well known
0
+      # solution}[http://www.37signals.com/svn/archives2/id_partitioning.php]
0
+      # to this problem fails with high ids, such as those created by
0
+      # db:fixture:load. This version scales to large ids more gracefully.
0
+      # Thanks to Adrian Mugnolo for the fix.
0
       def partitioned_path(id, *args)
0
-        ("%08d" % id).scan(/..../) + args
0
+        ["%04d" % ((id.to_i / 1e4) % 1e4), "%04d" % (id.to_i % 1e4)].concat(args)
0
       end
0
 
0
-      # Generates a 4-6 character random file name to use for the image and its
0
-      # thumbnails. This is done to avoid having files with unfortunate names.
0
-      # On one of my sites users frequently upload images with Arabic names, and
0
-      # they end up being hard to manipulate on the command line. This also
0
-      # helps prevent a possibly undesirable sitation where the uploaded images
0
-      # have offensive names.
0
+      # Generates a 4-6 character random file name to use for the image and
0
+      # its thumbnails. This is done to avoid having files with unfortunate
0
+      # names. On one of my sites users frequently upload images with Arabic
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
         Zlib.crc32(Time.now.to_s + rand(10e10).to_s).to_s(36).downcase
0
       end
...
20
21
22
 
 
 
 
 
23
24
25
...
20
21
22
23
24
25
26
27
28
29
30
0
@@ -20,6 +20,11 @@ class StorageTest < Test::Unit::TestCase
0
     assert_equal(["0001", "2345"], HasImage::Storage.partitioned_path("12345"))
0
   end
0
   
0
+  def test_partitioned_path_doesnt_collide_with_high_ids
0
+    assert_not_equal HasImage::Storage.partitioned_path(867792732),
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
   end

Comments