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.
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Instance Method Summary collapse
-
#basename ⇒ Object
Returns the part of the filename before the extension.
-
#content_type ⇒ Object
Returns the content type of the file.
-
#content_type=(type) ⇒ Object
Sets the content type of the file.
-
#copy_to(new_path, permissions = nil) ⇒ 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
Returns.
-
#exists? ⇒ Boolean
Returns.
-
#extension ⇒ Object
Returns the file extension.
-
#filename ⇒ Object
(also: #identifier)
Returns the filename, sanitized to strip out any evil characters.
-
#initialize(file) ⇒ SanitizedFile
constructor
A new instance of SanitizedFile.
-
#is_path? ⇒ Boolean
Returns.
-
#move_to(new_path, permissions = nil) ⇒ Object
Moves the file to the given path.
-
#original_filename ⇒ Object
Returns the filename as is, without sanizting it.
-
#path ⇒ Object
Returns the full path to the file.
-
#read ⇒ Object
Returns the contents of the file.
-
#sanitize_regexp ⇒ Object
Used to sanitize the file name.
-
#size ⇒ Object
Returns the file’s size.
Constructor Details
#initialize(file) ⇒ SanitizedFile
Returns a new instance of SanitizedFile.
28 29 30 |
# File 'lib/carrierwave/sanitized_file.rb', line 28 def initialize(file) self.file = file end |
Class Attribute Details
.sanitize_regexp ⇒ Object
23 24 25 |
# File 'lib/carrierwave/sanitized_file.rb', line 23 def sanitize_regexp @sanitize_regexp ||= /[^a-zA-Z0-9\.\-\+_]/ end |
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
18 19 20 |
# File 'lib/carrierwave/sanitized_file.rb', line 18 def file @file end |
Instance Method Details
#basename ⇒ Object
Returns the part of the filename before the extension. So if a file is called ‘test.jpeg’ this would return ‘test’
Returns
- String
-
the first part of the filename
69 70 71 |
# File 'lib/carrierwave/sanitized_file.rb', line 69 def basename split_extension(filename)[0] if filename end |
#content_type ⇒ Object
Returns the content type of the file.
Returns
- String
-
the content type of the file
226 227 228 229 |
# File 'lib/carrierwave/sanitized_file.rb', line 226 def content_type return @content_type if @content_type @file.content_type.chomp if @file.respond_to?(:content_type) and @file.content_type end |
#content_type=(type) ⇒ Object
Sets the content type of the file.
Returns
- String
-
the content type of the file
238 239 240 |
# File 'lib/carrierwave/sanitized_file.rb', line 238 def content_type=(type) @content_type = type end |
#copy_to(new_path, permissions = nil) ⇒ CarrierWave::SanitizedFile
Creates a copy of this file and moves it to the given path. Returns the copy.
Parameters
- new_path (String)
-
The path where the file should be copied to.
- permissions (Integer)
-
permissions to set on the copy
Returns
198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/carrierwave/sanitized_file.rb', line 198 def copy_to(new_path, =nil) 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({:tempfile => new_path, :content_type => content_type}) end |
#delete ⇒ Object
Removes the file from the filesystem.
215 216 217 |
# File 'lib/carrierwave/sanitized_file.rb', line 215 def delete FileUtils.rm(self.path) if exists? end |
#empty? ⇒ Boolean
Returns
- Boolean
-
whether the file is valid and has a non-zero size
134 135 136 |
# File 'lib/carrierwave/sanitized_file.rb', line 134 def empty? @file.nil? || self.size.nil? || self.size.zero? end |
#exists? ⇒ Boolean
Returns
- Boolean
-
Whether the file exists
143 144 145 146 |
# File 'lib/carrierwave/sanitized_file.rb', line 143 def exists? return File.exists?(self.path) if self.path return false end |
#extension ⇒ Object
Returns the file extension
Returns
- String
-
the extension
80 81 82 |
# File 'lib/carrierwave/sanitized_file.rb', line 80 def extension split_extension(filename)[1] if filename end |
#filename ⇒ Object Also known as: identifier
Returns the filename, sanitized to strip out any evil characters.
Returns
- String
-
the sanitized filename
55 56 57 |
# File 'lib/carrierwave/sanitized_file.rb', line 55 def filename sanitize(original_filename) if original_filename end |
#is_path? ⇒ Boolean
Returns
- Boolean
-
whether the file is supplied as a pathname or string.
125 126 127 |
# File 'lib/carrierwave/sanitized_file.rb', line 125 def is_path? !!((@file.is_a?(String) || @file.is_a?(Pathname)) && !@file.blank?) end |
#move_to(new_path, permissions = nil) ⇒ Object
Moves the file to the given path
Parameters
- new_path (String)
-
The path where the file should be moved.
- permissions (Integer)
-
permissions to set on the file in its new location.
172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/carrierwave/sanitized_file.rb', line 172 def move_to(new_path, =nil) 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 ⇒ Object
Returns the filename as is, without sanizting it.
Returns
- String
-
the unsanitized filename
39 40 41 42 43 44 45 46 |
# File 'lib/carrierwave/sanitized_file.rb', line 39 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 ⇒ Object
Returns the full path to the file. If the file has no path, it will return nil.
Returns
- String, nil
-
the path where the file is located.
110 111 112 113 114 115 116 117 118 |
# File 'lib/carrierwave/sanitized_file.rb', line 110 def path unless @file.blank? if is_path? File.(@file) elsif @file.respond_to?(:path) and not @file.path.blank? File.(@file.path) end end end |
#read ⇒ Object
Returns the contents of the file.
Returns
- String
-
contents of the file
155 156 157 158 159 160 161 162 |
# File 'lib/carrierwave/sanitized_file.rb', line 155 def read if is_path? File.open(@file, "rb") {|file| file.read} else @file.rewind if @file.respond_to?(:rewind) @file.read end end |
#sanitize_regexp ⇒ Object
Used to sanitize the file name. Public to allow overriding for non-latin characters.
Returns
- Regexp
-
the regexp for sanitizing the file name
249 250 251 |
# File 'lib/carrierwave/sanitized_file.rb', line 249 def sanitize_regexp CarrierWave::SanitizedFile.sanitize_regexp end |
#size ⇒ Object
Returns the file’s size.
Returns
- Integer
-
the file’s size in bytes.
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/carrierwave/sanitized_file.rb', line 91 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 |