SanitizedFile is a base class which provides a common API around all the different quirky Ruby File libraries. It has support for Tempfile, File, StringIO, Merb-style upload Hashes, as well as paths given as Strings and Pathnames.
It’s probably needlessly comprehensive and complex. Help is appreciated.
Returns the part of the filename before the extension. So if a file is called ‘test.jpeg’ this would return ‘test’
the first part of the filename
# File lib/carrierwave/sanitized_file.rb, line 59 def basename split_extension(filename)[0] if filename end
Returns the content type of the file.
the content type of the file
# File lib/carrierwave/sanitized_file.rb, line 218 def content_type return @content_type if @content_type @file.content_type.chomp if @file.respond_to?(:content_type) and @file.content_type end
Creates a copy of this file and moves it to the given path. Returns the copy.
The path where the file should be copied to.
permissions to set on the copy
@return [CarrierWave::SanitizedFile] the location where the file will be stored.
# File lib/carrierwave/sanitized_file.rb, line 190 def copy_to(new_path, permissions=nil) return if self.empty? new_path = File.expand_path(new_path) mkdir!(new_path) if exists? FileUtils.cp(path, new_path) unless new_path == path else File.open(new_path, "wb") { |f| f.write(read) } end chmod!(new_path, permissions) self.class.new({:tempfile => new_path, :content_type => content_type}) end
Removes the file from the filesystem.
# File lib/carrierwave/sanitized_file.rb, line 207 def delete FileUtils.rm(self.path) if exists? end
whether the file is valid and has a non-zero size
# File lib/carrierwave/sanitized_file.rb, line 124 def empty? @file.nil? || self.size.nil? || self.size.zero? end
Whether the file exists
# File lib/carrierwave/sanitized_file.rb, line 135 def exists? return File.exists?(self.path) if self.path return false end
Returns the file extension
the extension
# File lib/carrierwave/sanitized_file.rb, line 70 def extension split_extension(filename)[1] if filename end
Returns the filename, sanitized to strip out any evil characters.
the sanitized filename
# File lib/carrierwave/sanitized_file.rb, line 45 def filename sanitize(original_filename) if original_filename end
whether the file is supplied as a pathname or string.
# File lib/carrierwave/sanitized_file.rb, line 115 def is_path? !!((@file.is_a?(String) || @file.is_a?(Pathname)) && !@file.blank?) end
Moves the file to the given path
The path where the file should be moved.
permissions to set on the file in its new location.
# File lib/carrierwave/sanitized_file.rb, line 164 def move_to(new_path, permissions=nil) return if self.empty? new_path = File.expand_path(new_path) mkdir!(new_path) if exists? FileUtils.mv(path, new_path) unless new_path == path else File.open(new_path, "wb") { |f| f.write(read) } end chmod!(new_path, permissions) self.file = new_path end
Returns the filename as is, without sanizting it.
the unsanitized filename
# File lib/carrierwave/sanitized_file.rb, line 29 def original_filename return @original_filename if @original_filename if @file and @file.respond_to?(:original_filename) @file.original_filename elsif path File.basename(path) end end
Returns the full path to the file. If the file has no path, it will return nil.
the path where the file is located.
# File lib/carrierwave/sanitized_file.rb, line 100 def path unless @file.blank? if is_path? File.expand_path(@file) elsif @file.respond_to?(:path) and not @file.path.blank? File.expand_path(@file.path) end end end
Returns the contents of the file.
contents of the file
# File lib/carrierwave/sanitized_file.rb, line 147 def read if is_path? File.open(@file, "rb").read else @file.rewind if @file.respond_to?(:rewind) @file.read end end
Returns the file’s size.
the file’s size in bytes.
# File lib/carrierwave/sanitized_file.rb, line 81 def size if is_path? exists? ? File.size(path) : 0 elsif @file.respond_to?(:size) @file.size elsif path exists? ? File.size(path) : 0 else 0 end end
# File lib/carrierwave/sanitized_file.rb, line 18 def initialize(file) self.file = file end