Class: UploadColumn::UploadedFile

Inherits:
SanitizedFile show all
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

Attributes inherited from SanitizedFile

#basename, #extension

Class Method Summary collapse

Instance Method Summary collapse

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, options={})
  # 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.
  @options = options.reverse_merge(UploadColumn.configuration)
  @instance = instance
  @attribute = attribute
  @suffix = options[: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, @options)
    
    unless empty?
      if options[:validate_integrity] 
        raise UploadError.new("No list of valid extensions supplied.") unless options[:extensions]
        raise IntegrityError.new("has an extension that is not allowed.") unless options[: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!(@options[:process]) if @options[: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, @options)
    initialize_versions
  end
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



34
35
36
# File 'lib/upload_column/uploaded_file.rb', line 34

def attribute
  @attribute
end

#instanceObject (readonly)

Returns the value of attribute instance.



34
35
36
# File 'lib/upload_column/uploaded_file.rb', line 34

def instance
  @instance
end

#optionsObject (readonly)

Returns the value of attribute options.



34
35
36
# File 'lib/upload_column/uploaded_file.rb', line 34

def options
  @options
end

#suffixObject

Returns the value of attribute suffix.



35
36
37
# File 'lib/upload_column/uploaded_file.rb', line 35

def suffix
  @suffix
end

#versionsObject (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, options = {}) #:nodoc:
  self.new(:retrieve, filename, instance, attribute, options)
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, options = {}) #:nodoc:
  self.new(:retrieve_temp, path, instance, attribute, options)        
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, options = {}) #:nodoc:
  uf = self.new(:upload, file, instance, attribute, options)
  return uf.empty? ? nil : uf
end

Instance Method Details

#actual_filenameObject



189
# File 'lib/upload_column/uploaded_file.rb', line 189

alias_method :actual_filename, :filename

#dirObject

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

#filenameObject



191
192
193
194
195
196
197
# File 'lib/upload_column/uploaded_file.rb', line 191

def filename
  unless bn = parse_dir_options(:filename)
    bn = [self.basename, self.suffix].compact.join('-')
    bn += ".#{self.extension}" unless self.extension.blank?
  end
  return bn
end

#inspectObject

: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.

Returns:

  • (Boolean)


157
158
159
# File 'lib/upload_column/uploaded_file.rb', line 157

def new_file?
  @new_file
end

#pathObject

returns the full path of the file.



149
# File 'lib/upload_column/uploaded_file.rb', line 149

def path; super; end

#public_pathObject 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
  options[:web_root].to_s + '/' + self.relative_path.gsub("\\", "/")
end

#relative_pathObject

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.expand_path(options[:root_dir]) + '/', '')
end

#relative_store_dirObject

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
  parse_dir_options(:store_dir)
end

#relative_tmp_dirObject

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
  parse_dir_options(:tmp_dir)
end

#store_dirObject

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.expand_path(self.relative_store_dir, @options[:root_dir])
end

#temp_valueObject

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

Returns:

  • (Boolean)


185
186
187
# File 'lib/upload_column/uploaded_file.rb', line 185

def tempfile?
  @temp_name
end

#tmp_dirObject

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.expand_path(self.relative_tmp_dir, @options[:root_dir])
end