Module: CommandWrap

Defined in:
lib/command_wrap.rb,
lib/command_wrap/pdf.rb,
lib/command_wrap/xvfb.rb,
lib/command_wrap/image.rb,
lib/command_wrap/config.rb,
lib/command_wrap/open_office.rb

Defined Under Namespace

Modules: Config, Image, OpenOffice, Pdf, Xvfb

Class Method Summary collapse

Class Method Details

.capture(url, target) ⇒ Object

Creates a screenshot from the given url Uses CutyCapt (cutycapt.sourceforge.net)



7
8
9
10
# File 'lib/command_wrap.rb', line 7

def self.capture (url, target)
    command = CommandWrap::Config::Xvfb.command(File.dirname(__FILE__) + "/../bin/CutyCapt --min-width=1024 --min-height=768 --url=#{url} --out=#{target}")
    `#{command}`
end

.extension(filename) ⇒ Object



50
51
52
53
# File 'lib/command_wrap.rb', line 50

def self.extension (filename)
    return '' unless filename.include?('.')
    filename.split('.').last
end

.htmltopdf(source, target) ⇒ Object



12
13
14
15
# File 'lib/command_wrap.rb', line 12

def self.htmltopdf (source, target)
    command = CommandWrap::Config::Xvfb.command(File.dirname(__FILE__) + "/../bin/wkhtmltopdf --print-media-type #{source} #{target}")
    `#{command}`
end

.index(path) ⇒ Object

Tries to convert content of file to plaintext or html



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/command_wrap.rb', line 103

def self.index (path)
    extension = CommandWrap.extension(path).downcase

    if extension == 'txt'
        return IO.read(path)
    end

    if extension == 'html' || extension == 'htm'
        return IO.read(path)
    end

    tmp = self.temp('html')
    begin
        CommandWrap::OpenOffice.convert(path, tmp)
        return IO.read(tmp) if File.exists?(tmp)
    rescue
    ensure
        File.delete(tmp) if File.exists?(tmp) && File.writable?(tmp)
    end

    ''
end

.preview(source, target, width, height) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/command_wrap.rb', line 55

def self.preview (source, target, width, height)
    extension = self.extension(source).upcase

    # Image ?
    formats = Magick.formats
    if formats.key?(extension) && formats[extension].include?('r')
        begin
            CommandWrap::Image.scale(source, target, width, height)
            return true
        rescue
            return false
        end
    end

    tmppdf = self.temp('pdf')
    tmppng = self.temp('png')
    begin
        # Create a pdf of the document
        CommandWrap::OpenOffice.convert(source, tmppdf)
        # Create a screenshot of first page of the generated pdf
        CommandWrap::Pdf.preview(tmppdf, tmppng)
        # Scale it down to thumb
        CommandWrap::Image.scale(tmppng, target, width, height)
        return true
    rescue

    ensure
        # Cleanup
        File.delete(tmppdf) if File.exists?(tmppdf) && File.writable?(tmppdf)
        File.delete(tmppng) if File.exists?(tmppng) && File.writable?(tmppng)
    end

    nil
end

.temp(extension) ⇒ Object

Generates a temp filepath for the given extension



91
92
93
94
95
96
97
98
99
100
# File 'lib/command_wrap.rb', line 91

def self.temp (extension)
    path = "#{CommandWrap::Config.tmp_dir}/tmp.#{extension}"
    id = 1
    while File.exists?(path)
        path = "#{CommandWrap::Config.tmp_dir}/tmp.#{id}.#{extension}"
        id += 1
    end

    path
end

.zip(target, *sources) ⇒ Object

Sources consists of paths followed by the filename that must be used in the zip



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/command_wrap.rb', line 18

def self.zip (target, *sources)
    if sources.length == 1 && sources[0].is_a?(Array)
        sources = sources[0]
    end

    if sources.length % 2 == 1
        raise "sources must contain an even number of string (path to file, filename)"
    end

    targetdir = "#{CommandWrap::Config.tmp_dir}/zip"
    id = 1
    while File.exists?(targetdir)
        targetdir = "#{CommandWrap::Config.tmp_dir}/zip#{id}"
        id += 1
    end
    FileUtils.mkdir(targetdir)

    path = ''
    sources.each do |value|
        if path == ''
            path = value
        else
            FileUtils.copy(path, "#{targetdir}/#{value}")
            path = ''
        end
    end

    `#{CommandWrap::Config.zip} -j #{target} #{targetdir}/*`

    FileUtils.rm_rf(targetdir)
end