Class: Balloon::FileExtension

Inherits:
Object
  • Object
show all
Defined in:
lib/balloon/file_extension.rb

Overview

This class is file Extension for ruby class

@param[File, UploadFile, Hash, String, StringIO] file

file = FileExtension.new(file) file.path file.filename file.basename file.mime_type file.extension file.read

Constant Summary collapse

SANITIZE_REGEX =
/[^a-zA-Z0-9\.]|[\^]/
FILENAME_REGEX =
[ /\A(.+)\.(tar\.([glxb]?z|bz2))\z/, /\A(.+)\.([^\.]+)\z/ ]
IMAGE_REGEX =
[
  ["GIF8", "image/gif"],
  ["\x89PNG", "image/png"],
  ["\xff\xd8\xff\xe0\x00\x10JFIF", "image/jpeg"],
  ["\xff\xd8\xff\xe1(.*){2}Exif", "image/jpeg"]
]
IMAGE_TYPE_LIST =
{
  "GIF" => "image/gif",
  "JPEG" => "image/jpeg",
  "PNG" => "image/png",
  "WEBP" => "image/webp"
}
IMAGE_EXT_LIST =
{ 
  "image/gif" => "gif",
  "image/jpeg" => "jpg",
  "image/png" => "png",
  "image/webp" => "webp"
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, mime_type = nil) ⇒ FileExtension

Returns a new instance of FileExtension.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/balloon/file_extension.rb', line 46

def initialize(file, mime_type = nil)
  if file.is_a?(Hash)
    @file = file["tempfile"] || file[:tempfile]
    @original_filename = file["filename"] || file[:filename]
    @mime_type = file["content_type"] || file[:content_type]
  else
    @file = file
    @original_filename = nil
    @mime_type = mime_type
  end
end

Class Method Details

.get_extension(mime_type) ⇒ Object



149
150
151
# File 'lib/balloon/file_extension.rb', line 149

def self.get_extension(mime_type)
  IMAGE_EXT_LIST[mime_type]
end

.get_mime_type(type) ⇒ Object



144
145
146
147
# File 'lib/balloon/file_extension.rb', line 144

def self.get_mime_type(type)
  return type if type =~ /.+\/.+/
  IMAGE_TYPE_LIST[type]
end

Instance Method Details

#basenameObject



94
95
96
97
# File 'lib/balloon/file_extension.rb', line 94

def basename
  return "" if filename.blank?
  split_extension(filename)[0] 
end

#empty?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/balloon/file_extension.rb', line 124

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

#exists?Boolean

Returns:

  • (Boolean)


128
129
130
131
# File 'lib/balloon/file_extension.rb', line 128

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

#extensionObject



99
100
101
102
103
# File 'lib/balloon/file_extension.rb', line 99

def extension
  ext_name = split_extension(filename)[1] if !filename.blank?
  return ext_name if !ext_name.blank?
  return IMAGE_EXT_LIST[mime_type]
end

#filenamestring

Get filename with uploaded file

Returns:

  • (string)

    the real filename



89
90
91
92
# File 'lib/balloon/file_extension.rb', line 89

def filename
  return "" if original_filename.blank?
  sanitize(original_filename) 
end

#mime_typeObject



105
106
107
108
109
110
111
# File 'lib/balloon/file_extension.rb', line 105

def mime_type
  return get_mime_type(MIME::Types[@mime_type]) if @mime_type
  ext_name = split_extension(filename)[1] if !filename.blank?
  return get_mime_type(MIME::Types.type_for(ext_name)) if !ext_name.blank?
  if type = read_mime_type then return type end
  if type = command_mime_type then return type end
end

#original_filenameString

Get original filename with uploaded file

Returns:

  • (String)

    file original filename



75
76
77
78
79
80
81
82
83
84
# File 'lib/balloon/file_extension.rb', line 75

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

#pathString

Get real path with uploaded file

Returns:

  • (String)

    file path



61
62
63
64
65
66
67
68
69
70
# File 'lib/balloon/file_extension.rb', line 61

def path
  return "" if @file.blank?
  if @file.is_a?(String) || @file.is_a?(Pathname)
    File.expand_path(@file)
  elsif @file.respond_to?(:path)
    File.expand_path(@file.path)
  else
    ""
  end
end

#read(count = nil) ⇒ Object



133
134
135
136
137
138
139
140
141
142
# File 'lib/balloon/file_extension.rb', line 133

def read(count = nil)
  return "" if empty?
  if exists? && @file.is_a?(String)
    File.open(@file, "rb") {|file| file.read(count) }
  elsif @file.respond_to?(:read)
    @file.read(count)
  else
    ""
  end
end

#save_to(new_path, permissions = nil, directory_permission = nil) ⇒ Object

TODO:

What Change basename add extension



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/balloon/file_extension.rb', line 154

def save_to(new_path, permissions = nil, directory_permission = nil)
  new_path = File.expand_path new_path
  new_path = File.join new_path, basename + "." + extension 

  if exists?
    FileUtils.cp(path, new_path) unless new_path == path
  else
    File.open(new_path, "wb"){ |f| f.write(read) }
  end
  File.chmod(permissions, new_path) if permissions
  self.class.new(new_path)
end

#sizeObject



113
114
115
116
117
118
119
120
121
122
# File 'lib/balloon/file_extension.rb', line 113

def size
  return 0 if @file.blank?
  if @file.is_a?(String)
    exists? ? File.size(path) : 0
  elsif @file.respond_to?(:size)
    @file.size
  else
    0
  end
end