Class: UploadColumn::UploadedFile
- Inherits:
-
SanitizedFile
- Object
- SanitizedFile
- UploadColumn::UploadedFile
- Defined in:
- lib/upload_column/uploaded_file.rb
Overview
When you call an upload_column field, an instance of this class will be returned.
Suppose a User model has a picture upload_column, like so:
class User < ActiveRecord::Base
upload_column :picture
end
Now in our controller we did:
@user = User.find(params[:id])
We could then access the file:
@user.picture.url
Which would output the url to the file (assuming it is stored in /public/)
Versions
If we had instead added different versions in our model
upload_column :picture, :versions => [:thumb, :large]
Then we could access them like so:
@user.picture.thumb.url
See the README for more detaills.
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
-
#instance ⇒ Object
readonly
Returns the value of attribute instance.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#suffix ⇒ Object
Returns the value of attribute suffix.
-
#versions ⇒ Object
readonly
Returns the value of attribute versions.
Attributes inherited from SanitizedFile
Class Method Summary collapse
-
.retrieve(filename, instance = nil, attribute = nil, options = {}) ⇒ Object
Retrieve a file from the filesystem, based on the calculated store_dir and the filename stored in the database.
-
.retrieve_temp(path, instance = nil, attribute = nil, options = {}) ⇒ Object
Retreieve a file that was stored as a temp file.
-
.upload(file, instance = nil, attribute = nil, options = {}) ⇒ Object
upload a file.
Instance Method Summary collapse
- #actual_filename ⇒ Object
-
#dir ⇒ Object
returns the directory where the file is currently stored.
- #filename ⇒ Object
-
#initialize(mode, file, instance, attribute, options = {}) ⇒ UploadedFile
constructor
A new instance of UploadedFile.
-
#inspect ⇒ Object
:nodoc:.
-
#move_to_directory(dir) ⇒ Object
TODO: this is a public method, should be specced.
-
#new_file? ⇒ Boolean
return true if the file has just been uploaded.
-
#path ⇒ Object
returns the full path of the file.
-
#public_path ⇒ Object
(also: #to_s, #url)
returns the url of the file, by merging the relative path with the web_root option.
-
#relative_path ⇒ Object
Returns the path of the file relative to :root_dir.
-
#relative_store_dir ⇒ Object
Returns the directory where files are stored for this UploadedFile, relative to :root_dir.
-
#relative_tmp_dir ⇒ Object
Returns the directory where tmp files are stored for this UploadedFile, relative to :root_dir.
-
#store_dir ⇒ Object
Returns the directory where files are stored for this UploadedFile.
-
#temp_value ⇒ Object
this is the value returned when avatar_temp is called, where avatar is an upload_column.
- #tempfile? ⇒ Boolean
-
#tmp_dir ⇒ Object
Returns the directory where tmp files are stored for this UploadedFile.
Methods inherited from SanitizedFile
#content_type, #copy_to, #empty?, #exists?, #move_to, #original_filename, #size
Constructor Details
#initialize(mode, file, instance, attribute, options = {}) ⇒ UploadedFile
Returns a new instance of UploadedFile.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/upload_column/uploaded_file.rb', line 59 def initialize(mode, file, instance, attribute, ={}) # TODO: the options are always reverse merged in here, in case UploadedFile has # been initialized outside UploadColumn proper, this is not a very elegant solution, imho. = .reverse_merge(UploadColumn.configuration) @instance = instance @attribute = attribute @suffix = [:suffix] load_manipulator case mode when :upload if file and file.is_a?(String) and not file.empty? raise UploadNotMultipartError.new("Do not know how to handle a string with value '#{file}' that was uploaded. Check if the form's encoding has been set to 'multipart/form-data'.") end super(file, ) unless empty? if [:validate_integrity] raise UploadError.new("No list of valid extensions supplied.") unless [:extensions] raise IntegrityError.new("has an extension that is not allowed.") unless [:extensions].include?(extension) end @temp_name = generate_tmpname @new_file = true move_to_directory(File.join(tmp_dir, @temp_name)) # The original is processed before versions are initialized. self.process!([:process]) if [:process] and self.respond_to?(:process!) initialize_versions do |version| copy_to_version(version) end apply_manipulations_to_versions # trigger the _after_upload callback self.instance.send("#{self.attribute}_after_upload", self) if self.instance.respond_to?("#{self.attribute}_after_upload") end when :retrieve @path = File.join(store_dir, file) @basename, @extension = split_extension(file) initialize_versions when :retrieve_temp if file and not file.empty? @temp_name, name, original_filename = file.scan( ::UploadColumn::TempValueRegexp ).first if @temp_name and name @path = File.join(tmp_dir, @temp_name, name) @basename, @extension = split_extension(name) @original_filename = original_filename initialize_versions else raise TemporaryPathMalformedError.new("#{file} is not a valid temporary path!") end end else super(file, ) initialize_versions end end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
34 35 36 |
# File 'lib/upload_column/uploaded_file.rb', line 34 def attribute @attribute end |
#instance ⇒ Object (readonly)
Returns the value of attribute instance.
34 35 36 |
# File 'lib/upload_column/uploaded_file.rb', line 34 def instance @instance end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
34 35 36 |
# File 'lib/upload_column/uploaded_file.rb', line 34 def end |
#suffix ⇒ Object
Returns the value of attribute suffix.
35 36 37 |
# File 'lib/upload_column/uploaded_file.rb', line 35 def suffix @suffix end |
#versions ⇒ Object (readonly)
Returns the value of attribute versions.
34 35 36 |
# File 'lib/upload_column/uploaded_file.rb', line 34 def versions @versions end |
Class Method Details
.retrieve(filename, instance = nil, attribute = nil, options = {}) ⇒ Object
Retrieve a file from the filesystem, based on the calculated store_dir and the filename stored in the database.
48 49 50 |
# File 'lib/upload_column/uploaded_file.rb', line 48 def retrieve(filename, instance = nil, attribute = nil, = {}) #:nodoc: self.new(:retrieve, filename, instance, attribute, ) end |
.retrieve_temp(path, instance = nil, attribute = nil, options = {}) ⇒ Object
Retreieve a file that was stored as a temp file
53 54 55 |
# File 'lib/upload_column/uploaded_file.rb', line 53 def retrieve_temp(path, instance = nil, attribute = nil, = {}) #:nodoc: self.new(:retrieve_temp, path, instance, attribute, ) end |
.upload(file, instance = nil, attribute = nil, options = {}) ⇒ Object
upload a file. In most cases you want to pass the ActiveRecord instance and the attribute name as well as the file. For a more bare-bones approach, check out SanitizedFile.
41 42 43 44 |
# File 'lib/upload_column/uploaded_file.rb', line 41 def upload(file, instance = nil, attribute = nil, = {}) #:nodoc: uf = self.new(:upload, file, instance, attribute, ) return uf.empty? ? nil : uf end |
Instance Method Details
#actual_filename ⇒ Object
189 |
# File 'lib/upload_column/uploaded_file.rb', line 189 alias_method :actual_filename, :filename |
#dir ⇒ Object
returns the directory where the file is currently stored.
152 153 154 |
# File 'lib/upload_column/uploaded_file.rb', line 152 def dir File.dirname(self.path) end |
#filename ⇒ Object
191 192 193 194 195 196 197 |
# File 'lib/upload_column/uploaded_file.rb', line 191 def filename unless bn = (:filename) bn = [self.basename, self.suffix].compact.join('-') bn += ".#{self.extension}" unless self.extension.blank? end return bn end |
#inspect ⇒ Object
:nodoc:
181 182 183 |
# File 'lib/upload_column/uploaded_file.rb', line 181 def inspect #:nodoc: "<UploadedFile: #{self.path}>" end |
#move_to_directory(dir) ⇒ Object
TODO: this is a public method, should be specced
200 201 202 203 204 205 |
# File 'lib/upload_column/uploaded_file.rb', line 200 def move_to_directory(dir) p = File.join(dir, self.filename) if copy_file(p) @path = p end end |
#new_file? ⇒ Boolean
return true if the file has just been uploaded.
157 158 159 |
# File 'lib/upload_column/uploaded_file.rb', line 157 def new_file? @new_file end |
#path ⇒ Object
returns the full path of the file.
149 |
# File 'lib/upload_column/uploaded_file.rb', line 149 def path; super; end |
#public_path ⇒ Object Also known as: to_s, url
returns the url of the file, by merging the relative path with the web_root option.
162 163 164 165 |
# File 'lib/upload_column/uploaded_file.rb', line 162 def public_path # TODO: this might present an attack vector if the file is outside the web_root [:web_root].to_s + '/' + self.relative_path.gsub("\\", "/") end |
#relative_path ⇒ Object
Returns the path of the file relative to :root_dir
144 145 146 |
# File 'lib/upload_column/uploaded_file.rb', line 144 def relative_path self.path.sub(File.([:root_dir]) + '/', '') end |
#relative_store_dir ⇒ Object
Returns the directory where files are stored for this UploadedFile, relative to :root_dir
134 135 136 |
# File 'lib/upload_column/uploaded_file.rb', line 134 def relative_store_dir (:store_dir) end |
#relative_tmp_dir ⇒ Object
Returns the directory where tmp files are stored for this UploadedFile, relative to :root_dir
124 125 126 |
# File 'lib/upload_column/uploaded_file.rb', line 124 def relative_tmp_dir (:tmp_dir) end |
#store_dir ⇒ Object
Returns the directory where files are stored for this UploadedFile
139 140 141 |
# File 'lib/upload_column/uploaded_file.rb', line 139 def store_dir File.(self.relative_store_dir, [:root_dir]) end |
#temp_value ⇒ Object
this is the value returned when avatar_temp is called, where avatar is an upload_column
171 172 173 174 175 176 177 178 179 |
# File 'lib/upload_column/uploaded_file.rb', line 171 def temp_value #:nodoc: if tempfile? if original_filename %(#{@temp_name}/#{filename};#{original_filename}) else %(#{@temp_name}/#{filename}) end end end |
#tempfile? ⇒ Boolean
185 186 187 |
# File 'lib/upload_column/uploaded_file.rb', line 185 def tempfile? @temp_name end |
#tmp_dir ⇒ Object
Returns the directory where tmp files are stored for this UploadedFile
129 130 131 |
# File 'lib/upload_column/uploaded_file.rb', line 129 def tmp_dir File.(self.relative_tmp_dir, [:root_dir]) end |