Module: OpenOffice::Server

Defined in:
lib/backends/open_office/server.rb

Constant Summary collapse

PYTHON_PATH =

Path to the Python executable TODO : Eww, use config instead

"/usr/bin/python"
CONVERT_FROM =

Server can convert from the following file formats

[:odt, :doc, :rtf, :docx, :txt, :html, :htm, :wps]
CONVERT_TO =

To the following formats

[:odt, :doc, :rtf, :pdf, :txt, :html, :htm, :wps]
PY_OD_CONVERTER =

Python conversion script path TODO : Wrong

File.join(File.dirname(__FILE__), "../DocumentConverter.py")
MAX_CPU =

Maximum allowed CPU usage for an OpenOffice process

80
SERVER_START_DELAY =

Server start grace time

4
LOG_FILE =

Log file TODO : Wrong

Object.const_defined?(:RAILS_ROOT) ? File.join(RAILS_ROOT, "log", "openoffice.log") : ""

Class Method Summary collapse

Class Method Details

.convert(origin, options = {:to => :txt}) ⇒ Object



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
# File 'lib/backends/open_office/server.rb', line 30

def self.convert(origin, options = {:to => :txt})
  if options and options[:to]
    raise "#{origin} does not exist !" unless File.exist?(origin)
    ensure_available

    if options[:to].is_a? Symbol
      destination = "#{origin.gsub(/[^\.]*$/, "")}#{options[:to].to_s}"
    elsif options[:to].is_a? String
      destination = options[:to]
    else
      raise "Can't convert #{origin} to #{options[:to]}"
    end

    Documentalist.timeout(10, :attempts => 2) do
      system("#{PYTHON_PATH} #{PY_OD_CONVERTER} #{origin} #{destination} > /dev/null 2>&1")

      # HACK : sometimes text files get saved in ISO-8859-1 instead of regular UTF-8, so we force
      # a conversion if it's the case
      if `file #{destination}` =~ /ISO/ and destination =~ /\.txt$/
        temp_file = File.join(Dir.tmpdir, "tmp_iconv_#{rand(10**9)}.txt")
        system("iconv --from-code ISO-8859-1 --to-code UTF-8 #{destination} >  && #{temp_file} mv #{temp_file} #{destination}")
      end
    end

    destination
  end
end