Class: File

Inherits:
Object show all
Defined in:
lib/picolena/templates/lib/core_exts.rb

Class Method Summary collapse

Class Method Details

.encoding(source) ⇒ Object

Returns a probable encoding for a given plain text file If source is a html file, it parses for metadata to retrieve encoding, and uses file -i otherwise. Returns iso-8859-15 instead of iso-8859-1, to be sure € char can be encoded



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/picolena/templates/lib/core_exts.rb', line 80

def self.encoding(source)
  parse_for_charset="grep -io charset=[a-z0-9\\-]* | sed 's/charset=//i'"
  if File.extname(source)[0,4]==".htm" then
    enc=%x{head -n20 \"#{source}\" | #{parse_for_charset}}.chomp
  else
    enc=%x{file -i \"#{source}\"  | #{parse_for_charset}}.chomp
  end
  #iso-8859-15 should be used instead of iso-8859-1, for € char
  case enc
   when "iso-8859-1"
     "iso-8859-15"
   when "unknown"
     ""
   else
     enc
   end
end

.ext_as_sym(filename) ⇒ Object

Returns the filetype of filename as a symbol. Returns :no_extension unless an extension is found

>> File.ext_as_sym("test.pdf")
=> :pdf
>> File.ext_as_sym("test.tar.gz")
=> :gz
>> File.ext_as_sym("test")
=> :no_extension


71
72
73
# File 'lib/picolena/templates/lib/core_exts.rb', line 71

def self.ext_as_sym(filename)
  File.extname(filename).sub(/^\./,'').downcase.to_sym rescue :no_extension
end

.plain_text?(filename) ⇒ Boolean

Returns nil unless filename is a plain text file. It requires file command. NOTE: What to use for Win32?

Returns:

  • (Boolean)


109
110
111
# File 'lib/picolena/templates/lib/core_exts.rb', line 109

def self.plain_text?(filename)
  %x{file -i "#{filename}"} =~ /: text\//
end

.read_and_remove(filename) ⇒ Object

Returns the content of a file and removes it after. Could be used to read temporary output file written by a PlainTextExtractor.



100
101
102
103
104
# File 'lib/picolena/templates/lib/core_exts.rb', line 100

def self.read_and_remove(filename)
  content=read(filename)
  FileUtils.rm filename, :force=>true
  content
end

.thumbnail_path(filename, public_dir = false) ⇒ Object

For a given file, returns the path at which a thumbnail should be saved



114
115
116
117
# File 'lib/picolena/templates/lib/core_exts.rb', line 114

def self.thumbnail_path(filename, public_dir=false)
  thumb=expand_path(filename).base26_hash+'.jpg'
  public_dir ? File.join('thumbnails', thumb) : File.join(RAILS_ROOT,  'public/images/thumbnails', thumb)
end