Module: CommandWrap

Defined in:
lib/command_wrap.rb,
lib/command_wrap/pdf.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

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



43
44
45
46
# File 'lib/command_wrap.rb', line 43

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

.htmltopdf(source, target) ⇒ Object



12
13
14
15
16
# 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}")
    puts command
    `#{command}`
end

.index(path) ⇒ Object

Tries to convert content of file to plaintext or html



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/command_wrap.rb', line 96

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



48
49
50
51
52
53
54
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
# File 'lib/command_wrap.rb', line 48

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



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

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



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

def self.zip (target, *sources)
    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