| 27 |
27 |
|
| 28 |
28 |
COMMAND = (Redmine::Configuration['pandoc_command'] || 'pandoc').freeze
|
| 29 |
29 |
MAX_PREVIEW_SIZE = 100.kilobytes
|
| 30 |
|
SUPPORTED_EXTENSIONS = %w(.doc .docx .xls .xlsx .ppt .pptx .odt .ods .odp).freeze
|
|
30 |
SUPPORTED_EXTENSIONS = (Redmine::Configuration['pandoc_extensions'] || %w(.docx .xlsx .pptx .odt)).freeze
|
| 31 |
31 |
|
| 32 |
32 |
def self.supports?(filename)
|
| 33 |
33 |
SUPPORTED_EXTENSIONS.include?(File.extname(filename.to_s).downcase)
|
| ... | ... | |
| 39 |
39 |
|
| 40 |
40 |
directory = File.dirname(target)
|
| 41 |
41 |
FileUtils.mkdir_p(directory)
|
| 42 |
|
cmd = "#{shell_quote COMMAND} #{shell_quote source} -t gfm"
|
|
42 |
args = [COMMAND, source, "-t", "gfm"]
|
| 43 |
43 |
pid = nil
|
| 44 |
44 |
output = Tempfile.new('markdownized-preview')
|
| 45 |
45 |
|
| 46 |
46 |
begin
|
| 47 |
47 |
Timeout.timeout(Redmine::Configuration['thumbnails_generation_timeout'].to_i) do
|
| 48 |
|
pid = Process.spawn(cmd, out: output.path)
|
|
48 |
pid = Process.spawn(*args, out: output.path)
|
| 49 |
49 |
_, status = Process.wait2(pid)
|
| 50 |
50 |
unless status.success?
|
| 51 |
|
logger.error("Markdown conversion failed (#{status.exitstatus}):\nCommand: #{cmd}")
|
|
51 |
logger.error("Markdown conversion failed (#{status.exitstatus}):\nCommand: #{args.shelljoin}")
|
| 52 |
52 |
return nil
|
| 53 |
53 |
end
|
| 54 |
54 |
end
|
| 55 |
55 |
rescue Timeout::Error
|
| 56 |
56 |
Process.kill('KILL', pid) if pid
|
| 57 |
|
logger.error("Markdown conversion timed out:\nCommand: #{cmd}")
|
|
57 |
logger.error("Markdown conversion timed out:\nCommand: #{args.shelljoin}")
|
| 58 |
58 |
return nil
|
| 59 |
59 |
rescue => e
|
| 60 |
|
logger.error("Markdown conversion failed:\nCommand: #{cmd}\nException was: #{e.message}")
|
|
60 |
logger.error("Markdown conversion failed:\nCommand: #{args.shelljoin}\nException was: #{e.message}")
|
| 61 |
61 |
return nil
|
| 62 |
62 |
ensure
|
| 63 |
63 |
output.close
|
|
64 |
Process.detach(pid)
|
| 64 |
65 |
end
|
| 65 |
66 |
|
| 66 |
67 |
preview = File.binread(output.path, MAX_PREVIEW_SIZE + 1) || +""
|
| ... | ... | |
| 74 |
75 |
return @available if defined?(@available)
|
| 75 |
76 |
|
| 76 |
77 |
begin
|
| 77 |
|
`#{shell_quote COMMAND} --version`
|
| 78 |
|
@available = $?.success?
|
|
78 |
version = `#{shell_quote COMMAND} --version`[/pandoc\s+([\d.]+)/, 1].split('.').map(&:to_i)
|
|
79 |
@available = ($?.success? && (version <=> [3, 9]) >= 0) # Pandoc.version >= 3.9
|
| 79 |
80 |
rescue
|
| 80 |
81 |
@available = false
|
| 81 |
82 |
end
|