Class: CarrierWave::SanitizedFile

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

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ SanitizedFile

Returns a new instance of SanitizedFile.



20
21
22
# File 'lib/carrierwave/sanitized_file.rb', line 20

def initialize(file)
  self.file = file
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

#basenameObject

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



61
62
63
# File 'lib/carrierwave/sanitized_file.rb', line 61

def basename
  split_extension(filename)[0] if filename
end

#content_typeObject

Returns the content type of the file.

Returns

String

the content type of the file



220
221
222
223
# File 'lib/carrierwave/sanitized_file.rb', line 220

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, 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

Returns:



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

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

#deleteObject

Removes the file from the filesystem.



209
210
211
# File 'lib/carrierwave/sanitized_file.rb', line 209

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

#empty?Boolean Also known as: blank?

Returns

Boolean

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

Returns:

  • (Boolean)


126
127
128
# File 'lib/carrierwave/sanitized_file.rb', line 126

def empty?
  @file.nil? || self.size.nil? || self.size.zero?
end

#exists?Boolean

Returns

Boolean

Whether the file exists

Returns:

  • (Boolean)


137
138
139
140
# File 'lib/carrierwave/sanitized_file.rb', line 137

def exists?
  return File.exists?(self.path) if self.path
  return false
end

#extensionObject

Returns the file extension

Returns

String

the extension



72
73
74
# File 'lib/carrierwave/sanitized_file.rb', line 72

def extension
  split_extension(filename)[1] if filename
end

#filenameObject Also known as: identifier

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

Returns

String

the sanitized filename



47
48
49
# File 'lib/carrierwave/sanitized_file.rb', line 47

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)


117
118
119
# File 'lib/carrierwave/sanitized_file.rb', line 117

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.



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/carrierwave/sanitized_file.rb', line 166

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

#original_filenameObject

Returns the filename as is, without sanizting it.

Returns

String

the unsanitized filename



31
32
33
34
35
36
37
38
# File 'lib/carrierwave/sanitized_file.rb', line 31

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

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



102
103
104
105
106
107
108
109
110
# File 'lib/carrierwave/sanitized_file.rb', line 102

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

#readObject

Returns the contents of the file.

Returns

String

contents of the file



149
150
151
152
153
154
155
156
# File 'lib/carrierwave/sanitized_file.rb', line 149

def read
  if is_path?
    File.open(@file, "rb").read
  else
    @file.rewind if @file.respond_to?(:rewind)
    @file.read
  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



232
233
234
# File 'lib/carrierwave/sanitized_file.rb', line 232

def sanitize_regexp
  /[^a-zA-Z0-9\.\-\+_]/
end

#sizeObject

Returns the file’s size.

Returns

Integer

the file’s size in bytes.



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/carrierwave/sanitized_file.rb', line 83

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