Class: Inliner

Inherits:
Object
  • Object
show all
Defined in:
lib/hldr/inliners/inliner.rb

Direct Known Subclasses

ImageInliner, ScriptInliner, StyleInliner

Class Method Summary collapse

Class Method Details

.get_image_extension(local_file_path) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/hldr/inliners/inliner.rb', line 5

def Inliner.get_image_extension(local_file_path)
    # take from Alain Beauvois's post in
    # http://stackoverflow.com/questions/4600679/detect-mime-type-of-uploaded-file-in-ruby
    
    png = Regexp.new("\x89PNG".force_encoding("binary"))
    jpg = Regexp.new("\xff\xd8\xff\xe0\x00\x10JFIF".force_encoding("binary"))
    jpg2 = Regexp.new("\xff\xd8\xff\xe1(.*){2}Exif".force_encoding("binary"))

    case IO.read(local_file_path, 10)
    when /^GIF8/
        'image/gif'
    when /^#{png}/
        'image/png'
    when /^#{jpg}/
        'image/jpg'
    when /^#{jpg2}/
        'image/jpg'
    else
        mime_type = `file #{local_file_path} --mime-type`.gsub("\n", '') # Works on linux and mac
        raise UnprocessableEntity, "unknown file type" if !mime_type
        mime_type.split(':')[1].split('/')[1].gsub('x-', '').gsub(/jpeg/, 'jpg').gsub(/text/, 'txt').gsub(/x-/, '')
    end  

end

.getContent(location) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hldr/inliners/inliner.rb', line 30

def Inliner.getContent(location)

    content = Hash.new

    begin
        if location[0..3] == "http"
            content[:data] = open(location).read
            content[:type] = open(location).meta["content-type"]
        else
            content[:data] = File::open(location, "rb").read
            content[:type] = self.get_image_extension(location)
        end 
    rescue
    end

    return content

end