has_image 0.1.6 → 0.1.7

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