Class: CarrierWave::SanitizedFile

Inherits:
Object
  • Object
show all
Includes:
Utilities::FileName
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

Instance Method Summary collapse

Methods included from Utilities::FileName

#basename, #extension

Constructor Details

#initialize(file) ⇒ SanitizedFile

Returns a new instance of SanitizedFile.



28
29
30
31
# File 'lib/carrierwave/sanitized_file.rb', line 28

def initialize(file)
  self.file = file
  @content = @content_type = nil
end

Class Attribute Details

.sanitize_regexpObject



23
24
25
# File 'lib/carrierwave/sanitized_file.rb', line 23

def sanitize_regexp
  @sanitize_regexp ||= /[^[:word:]\.\-\+]/
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



18
19
20
# File 'lib/carrierwave/sanitized_file.rb', line 18

def file
  @file
end

Instance Method Details

#content_typeObject

Returns the content type of the file.

Returns

String

the content type of the file



241
242
243
244
245
246
247
# File 'lib/carrierwave/sanitized_file.rb', line 241

def content_type
  @content_type ||=
    identified_content_type ||
    declared_content_type ||
    guessed_safe_content_type ||
    Marcel::MimeType::BINARY
end

#content_type=(type) ⇒ Object

Sets the content type of the file.

Returns

String

the content type of the file



256
257
258
# File 'lib/carrierwave/sanitized_file.rb', line 256

def content_type=(type)
  @content_type = type
end

#copy!(new_path) ⇒ Object

Helper to create copy of file in new path.



207
208
209
210
211
212
213
# File 'lib/carrierwave/sanitized_file.rb', line 207

def copy!(new_path)
  if exists?
    FileUtils.cp(path, new_path) unless new_path == path
  else
    File.open(new_path, "wb") { |f| f.write(read) }
  end
end

#copy_to(new_path, permissions = nil, directory_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

directory_permissions (Integer)

permissions to set on created directories.

Returns

Returns:



194
195
196
197
198
199
200
201
202
# File 'lib/carrierwave/sanitized_file.rb', line 194

def copy_to(new_path, permissions=nil, directory_permissions=nil)
  return if self.empty?
  new_path = File.expand_path(new_path)

  mkdir!(new_path, directory_permissions)
  copy!(new_path)
  chmod!(new_path, permissions)
  self.class.new({tempfile: new_path, content_type: declared_content_type})
end

#deleteObject

Removes the file from the filesystem.



218
219
220
# File 'lib/carrierwave/sanitized_file.rb', line 218

def delete
  FileUtils.rm(self.path) if exists?
end

#empty?Boolean

Returns

Boolean

whether the file is valid and has a non-zero size

Returns:

  • (Boolean)


111
112
113
# File 'lib/carrierwave/sanitized_file.rb', line 111

def empty?
  @file.nil? || self.size.nil? || (self.size.zero? && !self.exists?)
end

#exists?Boolean

Returns

Boolean

Whether the file exists

Returns:

  • (Boolean)


120
121
122
# File 'lib/carrierwave/sanitized_file.rb', line 120

def exists?
  self.path.present? && File.exist?(self.path)
end

#filenameObject Also known as: identifier

Returns the filename, sanitized to strip out any evil characters.

Returns

String

the sanitized filename



56
57
58
# File 'lib/carrierwave/sanitized_file.rb', line 56

def filename
  sanitize(original_filename) if original_filename
end

#is_path?Boolean

Returns

Boolean

whether the file is supplied as a pathname or string.

Returns:

  • (Boolean)


102
103
104
# File 'lib/carrierwave/sanitized_file.rb', line 102

def is_path?
  !!((@file.is_a?(String) || @file.is_a?(Pathname)) && !@file.blank?)
end

#move!(new_path) ⇒ Object

Helper to move file to new path.



173
174
175
176
177
178
179
# File 'lib/carrierwave/sanitized_file.rb', line 173

def move!(new_path)
  if exists?
    FileUtils.mv(path, new_path) unless File.identical?(new_path, path)
  else
    File.open(new_path, "wb") { |f| f.write(read) }
  end
end

#move_to(new_path, permissions = nil, directory_permissions = nil, keep_filename = false) ⇒ 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.

directory_permissions (Integer)

permissions to set on created directories.



159
160
161
162
163
164
165
166
167
168
# File 'lib/carrierwave/sanitized_file.rb', line 159

def move_to(new_path, permissions=nil, directory_permissions=nil, keep_filename=false)
  return if self.empty?
  new_path = File.expand_path(new_path)

  mkdir!(new_path, directory_permissions)
  move!(new_path)
  chmod!(new_path, permissions)
  self.file = {tempfile: new_path, filename: keep_filename ? original_filename : nil, content_type: declared_content_type}
  self
end

#original_filenameObject

Returns the filename as is, without sanitizing it.

Returns

String

the unsanitized filename



40
41
42
43
44
45
46
47
# File 'lib/carrierwave/sanitized_file.rb', line 40

def original_filename
  return @original_filename if @original_filename
  if @file && @file.respond_to?(:original_filename)
    @file.original_filename
  elsif path
    File.basename(path)
  end
end

#pathObject

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.



88
89
90
91
92
93
94
95
# File 'lib/carrierwave/sanitized_file.rb', line 88

def path
  return if @file.blank?
  if is_path?
    File.expand_path(@file)
  elsif @file.respond_to?(:path) && !@file.path.blank?
    File.expand_path(@file.path)
  end
end

#read(*args) ⇒ Object

Returns the contents of the file.

Returns

String

contents of the file



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/carrierwave/sanitized_file.rb', line 131

def read(*args)
  if @content
    if args.empty?
      @content
    else
      length, outbuf = args
      raise ArgumentError, "outbuf argument not supported since the content is already loaded" if outbuf
      @content[0, length]
    end
  elsif is_path?
    File.open(@file, "rb") {|file| file.read(*args)}
  else
    @file.try(:rewind)
    @content = @file.read(*args)
    @file.try(:close) unless @file.class.ancestors.include?(::StringIO) || @file.try(:closed?)
    @content
  end
end

#sanitize_regexpObject

Used to sanitize the file name. Public to allow overriding for non-latin characters.

Returns

Regexp

the regexp for sanitizing the file name



267
268
269
# File 'lib/carrierwave/sanitized_file.rb', line 267

def sanitize_regexp
  CarrierWave::SanitizedFile.sanitize_regexp
end

#sizeObject

Returns the file’s size.

Returns

Integer

the file’s size in bytes.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/carrierwave/sanitized_file.rb', line 69

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

#to_fileObject

Returns a File object, or nil if it does not exist.

Returns

File

a File object representing the SanitizedFile



229
230
231
232
# File 'lib/carrierwave/sanitized_file.rb', line 229

def to_file
  return @file if @file.is_a?(File)
  File.open(path, "rb") if exists?
end