Module: Technoweenie::AttachmentFu::Backends::FileSystemBackend
- Defined in:
- lib/technoweenie/attachment_fu/backends/file_system_backend.rb
Overview
Methods for file system backed attachments
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#attachment_path_id ⇒ Object
The attachment ID used in the full path of a file.
-
#base_path ⇒ Object
Used as the base path that #public_filename strips off full_filename to create the public path.
-
#create_temp_file ⇒ Object
Creates a temp file from the currently saved file.
- #filename=(value) ⇒ Object
-
#full_filename(thumbnail = nil) ⇒ Object
Gets the full path to the filename in this format:.
-
#partitioned_path(*args) ⇒ Object
Partitions the given path into an array of path components.
-
#public_filename(thumbnail = nil) ⇒ Object
Gets the public path to the file The optional thumbnail argument will output the thumbnail’s filename.
Class Method Details
.included(base) ⇒ Object
:nodoc:
9 10 11 |
# File 'lib/technoweenie/attachment_fu/backends/file_system_backend.rb', line 9 def self.included(base) #:nodoc: base.before_update :rename_file end |
Instance Method Details
#attachment_path_id ⇒ Object
The attachment ID used in the full path of a file
32 33 34 |
# File 'lib/technoweenie/attachment_fu/backends/file_system_backend.rb', line 32 def ((respond_to?(:parent_id) && parent_id) || id) || 0 end |
#base_path ⇒ Object
Used as the base path that #public_filename strips off full_filename to create the public path
27 28 29 |
# File 'lib/technoweenie/attachment_fu/backends/file_system_backend.rb', line 27 def base_path @base_path ||= File.join(Rails.root, 'public') end |
#create_temp_file ⇒ Object
Creates a temp file from the currently saved file.
81 82 83 |
# File 'lib/technoweenie/attachment_fu/backends/file_system_backend.rb', line 81 def create_temp_file copy_to_temp_file full_filename end |
#filename=(value) ⇒ Object
75 76 77 78 |
# File 'lib/technoweenie/attachment_fu/backends/file_system_backend.rb', line 75 def filename=(value) @old_filename = full_filename unless filename.nil? || @old_filename write_attribute :filename, sanitize_filename(value) end |
#full_filename(thumbnail = nil) ⇒ Object
Gets the full path to the filename in this format:
# This assumes a model name like MyModel
# public/#{table_name} is the default filesystem path
#{Rails.root}/public/my_models/5/blah.jpg
Overwrite this method in your model to customize the filename. The optional thumbnail argument will output the thumbnail’s filename.
21 22 23 24 |
# File 'lib/technoweenie/attachment_fu/backends/file_system_backend.rb', line 21 def full_filename(thumbnail = nil) file_system_path = (thumbnail ? thumbnail_class : self).[:path_prefix].to_s File.join(Rails.root, file_system_path, *partitioned_path(thumbnail_name_for(thumbnail))) end |
#partitioned_path(*args) ⇒ Object
Partitions the given path into an array of path components.
For example, given an *args
of [“foo”, “bar”], it will return ["0000", "0001", "foo", "bar"]
(assuming that that id returns 1).
If the id is not an integer, then path partitioning will be performed by hashing the string value of the id with SHA-512, and splitting the result into 4 components. If the id a 128-bit UUID (as set by :uuid_primary_key => true) then it will be split into 2 components.
To turn this off entirely, set :partition => false.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/technoweenie/attachment_fu/backends/file_system_backend.rb', line 47 def partitioned_path(*args) if respond_to?(:attachment_options) && [:partition] == false args elsif [:uuid_primary_key] # Primary key is a 128-bit UUID in hex format. Split it into 2 components. path_id = .to_s component1 = path_id[0..15] || "-" component2 = path_id[16..-1] || "-" [component1, component2] + args else path_id = if path_id.is_a?(Integer) # Primary key is an integer. Split it after padding it with 0. ("%08d" % path_id).scan(/..../) + args else # Primary key is a String. Hash it, then split it into 4 components. hash = Digest::SHA512.hexdigest(path_id.to_s) [hash[0..31], hash[32..63], hash[64..95], hash[96..127]] + args end end end |
#public_filename(thumbnail = nil) ⇒ Object
Gets the public path to the file The optional thumbnail argument will output the thumbnail’s filename.
71 72 73 |
# File 'lib/technoweenie/attachment_fu/backends/file_system_backend.rb', line 71 def public_filename(thumbnail = nil) full_filename(thumbnail).gsub %r(^#{Regexp.escape(base_path)}), '' end |