Class: HasImage::Storage
- Inherits:
-
Object
- Object
- HasImage::Storage
- Defined in:
- lib/has_image/storage.rb
Overview
Filesystem storage for the HasImage gem. The methods that HasImage inserts into ActiveRecord models only depend on the public methods in this class, so it should be reasonably straightforward to implement a different storage mechanism for Amazon AWS, Photobucket, DBFile, SFTP, or whatever you want.
Instance Attribute Summary collapse
-
#image_data ⇒ Object
(also: #uploaded_data)
Returns the value of attribute image_data.
-
#options ⇒ Object
Returns the value of attribute options.
-
#temp_file ⇒ Object
Returns the value of attribute temp_file.
Class Method Summary collapse
-
.generated_file_name(*args) ⇒ Object
By default, simply accepts and returns the id of the object.
-
.partitioned_path(id, *args) ⇒ Object
Jamis Buck’s well known solution to this problem fails with high ids, such as those created by db:fixture:load.
Instance Method Summary collapse
-
#image_too_big? ⇒ Boolean
Is uploaded file larger than the allowed maximum?.
-
#image_too_small? ⇒ Boolean
Is uploaded file smaller than the allowed minimum?.
-
#initialize(options) ⇒ Storage
constructor
The constuctor should be invoked with the options set by has_image.
-
#install_images(object) ⇒ Object
Invokes the processor to resize the image(s) and the installs them to the appropriate directory.
-
#public_path_for(object, thumbnail = nil) ⇒ Object
Gets the “web” path for an image.
- #regenerate_thumbnails(id, name) ⇒ Object
-
#remove_images(object, name) ⇒ Object
Deletes the images and directory that contains them.
-
#valid? ⇒ Boolean
Is the uploaded file within the min and max allowed sizes?.
Constructor Details
#initialize(options) ⇒ Storage
The constuctor should be invoked with the options set by has_image.
38 39 40 |
# File 'lib/has_image/storage.rb', line 38 def initialize() # :nodoc: @options = end |
Instance Attribute Details
#image_data ⇒ Object Also known as: uploaded_data
Returns the value of attribute image_data.
15 16 17 |
# File 'lib/has_image/storage.rb', line 15 def image_data @image_data end |
#options ⇒ Object
Returns the value of attribute options.
15 16 17 |
# File 'lib/has_image/storage.rb', line 15 def @options end |
#temp_file ⇒ Object
Returns the value of attribute temp_file.
15 16 17 |
# File 'lib/has_image/storage.rb', line 15 def temp_file @temp_file end |
Class Method Details
.generated_file_name(*args) ⇒ Object
By default, simply accepts and returns the id of the object. This is here to allow you to monkey patch this method, for example, if you wish instead to generate and return a UUID.
31 32 33 |
# File 'lib/has_image/storage.rb', line 31 def generated_file_name(*args) return args.first.to_param.to_s end |
.partitioned_path(id, *args) ⇒ Object
Jamis Buck’s well known solution to this problem fails with high ids, such as those created by db:fixture:load. This version scales to large ids more gracefully. Thanks to Adrian Mugnolo for the fix.
24 25 26 |
# File 'lib/has_image/storage.rb', line 24 def partitioned_path(id, *args) ["%04d" % ((id.to_i / 1e4) % 1e4), "%04d" % (id.to_i % 1e4)].concat(args) end |
Instance Method Details
#image_too_big? ⇒ Boolean
Is uploaded file larger than the allowed maximum?
63 64 65 66 |
# File 'lib/has_image/storage.rb', line 63 def image_too_big? @temp_file.open if @temp_file.closed? @temp_file.size > [:max_size] end |
#image_too_small? ⇒ Boolean
Is uploaded file smaller than the allowed minimum?
57 58 59 60 |
# File 'lib/has_image/storage.rb', line 57 def image_too_small? @temp_file.open if @temp_file.closed? @temp_file.size < [:min_size] end |
#install_images(object) ⇒ Object
Invokes the processor to resize the image(s) and the installs them to the appropriate directory.
76 77 78 79 80 81 82 83 84 |
# File 'lib/has_image/storage.rb', line 76 def install_images(object) generated_name = Storage.generated_file_name(object) install_main_image(object.has_image_id, generated_name) install_thumbnails(object.has_image_id, generated_name) if ![:thumbnails].empty? return generated_name ensure @temp_file.close! if !@temp_file.closed? @temp_file = nil end |
#public_path_for(object, thumbnail = nil) ⇒ Object
Gets the “web” path for an image. For example:
/photos/0000/0001/3er0zs.jpg
89 90 91 |
# File 'lib/has_image/storage.rb', line 89 def public_path_for(object, thumbnail = nil) filesystem_path_for(object, thumbnail).gsub(/\A.*public/, '') end |
#regenerate_thumbnails(id, name) ⇒ Object
105 106 107 |
# File 'lib/has_image/storage.rb', line 105 def regenerate_thumbnails(id, name) install_thumbnails(id, name) end |
#remove_images(object, name) ⇒ Object
Deletes the images and directory that contains them.
94 95 96 97 98 |
# File 'lib/has_image/storage.rb', line 94 def remove_images(object, name) FileUtils.rm Dir.glob(File.join(path_for(object.has_image_id), name + '*')) Dir.rmdir path_for(object.has_image_id) rescue SystemCallError end |
#valid? ⇒ Boolean
Is the uploaded file within the min and max allowed sizes?
101 102 103 |
# File 'lib/has_image/storage.rb', line 101 def valid? !(image_too_small? || image_too_big?) end |