Class: CarrierWave::SanitizedFile
- Inherits:
-
Object
- Object
- CarrierWave::SanitizedFile
- Defined in:
- lib/carrierwave/sanitized_file.rb
Overview
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.
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#basename ⇒ String
Returns the part of the filename before the extension.
-
#content_type ⇒ String
Returns the content type of the file.
-
#copy_to(new_path) ⇒ CarrierWave::SanitizedFile
Creates a copy of this file and moves it to the given path.
-
#delete ⇒ Object
Removes the file from the filesystem.
-
#empty? ⇒ Boolean
Checks if the file is valid and has a non-zero size.
-
#exists? ⇒ Boolean
Checks if the file exists.
-
#extension ⇒ String
Returns the file extension.
-
#filename ⇒ String
(also: #identifier)
Returns the filename, sanitized to strip out any evil characters.
-
#initialize(file, options = {}) ⇒ SanitizedFile
constructor
A new instance of SanitizedFile.
-
#move_to(new_path) ⇒ Object
Moves the file to the given path.
-
#original_filename ⇒ String
Returns the filename as is, without sanizting it.
-
#path ⇒ String?
Returns the full path to the file.
-
#read ⇒ String
Returns the contents of the file.
-
#size ⇒ Integer
Returns the file’s size.
-
#string? ⇒ Boolean
Returns true if the file is supplied as a pathname or as a string.
Constructor Details
#initialize(file, options = {}) ⇒ SanitizedFile
Returns a new instance of SanitizedFile.
15 16 17 18 |
# File 'lib/carrierwave/sanitized_file.rb', line 15 def initialize(file, = {}) self.file = file self. = end |
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
13 14 15 |
# File 'lib/carrierwave/sanitized_file.rb', line 13 def file @file end |
#options ⇒ Object
Returns the value of attribute options.
13 14 15 |
# File 'lib/carrierwave/sanitized_file.rb', line 13 def @options end |
Instance Method Details
#basename ⇒ String
Returns the part of the filename before the extension. So if a file is called ‘test.jpeg’ this would return ‘test’
51 52 53 |
# File 'lib/carrierwave/sanitized_file.rb', line 51 def basename split_extension(filename)[0] if filename end |
#content_type ⇒ String
Returns the content type of the file.
189 190 191 192 |
# File 'lib/carrierwave/sanitized_file.rb', line 189 def content_type return @content_type if @content_type @file.content_type.chomp if @file.respond_to?(:content_type) and @file.content_type end |
#copy_to(new_path) ⇒ CarrierWave::SanitizedFile
Creates a copy of this file and moves it to the given path. Returns the copy.
163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/carrierwave/sanitized_file.rb', line 163 def copy_to(new_path) return if self.empty? new_path = File.(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) self.class.new(new_path) end |
#delete ⇒ Object
Removes the file from the filesystem.
180 181 182 |
# File 'lib/carrierwave/sanitized_file.rb', line 180 def delete FileUtils.rm(self.path) if exists? end |
#empty? ⇒ Boolean
Checks if the file is valid and has a non-zero size
110 111 112 |
# File 'lib/carrierwave/sanitized_file.rb', line 110 def empty? @file.nil? || self.size.nil? || self.size.zero? end |
#exists? ⇒ Boolean
Checks if the file exists
119 120 121 122 |
# File 'lib/carrierwave/sanitized_file.rb', line 119 def exists? return File.exists?(self.path) if self.path return false end |
#extension ⇒ String
Returns the file extension
60 61 62 |
# File 'lib/carrierwave/sanitized_file.rb', line 60 def extension split_extension(filename)[1] if filename end |
#filename ⇒ String Also known as: identifier
Returns the filename, sanitized to strip out any evil characters.
39 40 41 |
# File 'lib/carrierwave/sanitized_file.rb', line 39 def filename sanitize(original_filename) if original_filename end |
#move_to(new_path) ⇒ Object
Moves the file to the given path
143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/carrierwave/sanitized_file.rb', line 143 def move_to(new_path) return if self.empty? new_path = File.(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) self.file = new_path end |
#original_filename ⇒ String
Returns the filename as is, without sanizting it.
25 26 27 28 29 30 31 32 |
# File 'lib/carrierwave/sanitized_file.rb', line 25 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 |
#path ⇒ String?
Returns the full path to the file. If the file has no path, it will return nil.
86 87 88 89 90 91 92 93 94 |
# File 'lib/carrierwave/sanitized_file.rb', line 86 def path unless @file.blank? if string? File.(@file) elsif @file.respond_to?(:path) and not @file.path.blank? File.(@file.path) end end end |
#read ⇒ String
Returns the contents of the file.
129 130 131 132 133 134 135 136 |
# File 'lib/carrierwave/sanitized_file.rb', line 129 def read if string? File.read(@file) else @file.rewind if @file.respond_to?(:rewind) @file.read end end |
#size ⇒ Integer
Returns the file’s size.
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/carrierwave/sanitized_file.rb', line 69 def size if string? exists? ? File.size(path) : 0 elsif @file.respond_to?(:size) @file.size elsif path exists? ? File.size(path) : 0 else 0 end end |
#string? ⇒ Boolean
Returns true if the file is supplied as a pathname or as a string.
101 102 103 |
# File 'lib/carrierwave/sanitized_file.rb', line 101 def string? !!((@file.is_a?(String) || @file.is_a?(Pathname)) && !@file.blank?) end |