26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/libis/format/tool/spreadsheet_to_ods.rb', line 26
def run(source, target, options = {})
workdir = '/...'
workdir = Dir.tmpdir unless Dir.exist? workdir
workdir = File.join(workdir, rand(1_000_000).to_s)
FileUtils.mkpath(workdir)
src_file = File.join(workdir, File.basename(source))
FileUtils.symlink source, src_file
tgt_file = File.join(workdir, "#{File.basename(source, '.*')}.ods")
export_filter = options[:export_filter] || 'ods'
timeout = Libis::Format::Config[:timeouts][:spreadsheet_to_ods] ||
Libis::Format::Config[:timeouts][:office_to_pdf]
result = Libis::Tools::Command.run(
Libis::Format::Config[:soffice_cmd], '--headless',
"-env:UserInstallation=file://#{workdir}",
'--convert-to', export_filter,
'--outdir', workdir, src_file,
timeout:,
kill_after: timeout * 2
)
raise "#{self.class} took too long (> #{timeout} seconds) to complete" if result[:timeout]
warn "OfficeToPdf conversion messages: \n\t#{result[:err].join("\n\t")}" unless result[:err].empty?
raise "#{self.class} failed to generate target file #{tgt_file}" unless File.exist?(tgt_file)
FileUtils.copy tgt_file, target, preserve: true
{
command: result,
files: [target]
}
ensure
begin
FileUtils.rmtree workdir
rescue StandardError
nil
end
end
|