class ChupaText::Decomposers::LibreOffice

Constants

TARGET_EXTENSIONS
TARGET_MIME_TYPES

Public Class Methods

new(options) click to toggle source
Calls superclass method
# File lib/chupa-text/decomposers/libreoffice.rb, line 27
def initialize(options)
  super
  @command = find_command
  debug do
    if @command
      "#{log_tag}[command][found] #{@command.path}"
    else
      "#{log_tag}[command][not-found]"
    end
  end
end

Public Instance Methods

decompose(data) { |pdf_data| ... } click to toggle source
# File lib/chupa-text/decomposers/libreoffice.rb, line 67
def decompose(data)
  pdf_data = convert_to_pdf(data)
  return if pdf_data.nil?
  yield(pdf_data)
end
target?(data) click to toggle source
# File lib/chupa-text/decomposers/libreoffice.rb, line 61
def target?(data)
  return false if @command.nil?
  TARGET_EXTENSIONS.include?(data.extension) or
    TARGET_MIME_TYPES.include?(data.mime_type)
end

Private Instance Methods

convert_to_pdf(data) click to toggle source
# File lib/chupa-text/decomposers/libreoffice.rb, line 98
def convert_to_pdf(data)
  Dir.mktmpdir do |temporary_directory|
    output = Tempfile.new("chupa-text-decomposer-libreoffice-output")
    error_output = Tempfile.new("chupa-text-decomposer-libreoffice-error")
    succeeded = @command.run("--headless",
                             "--nologo",
                             "--convert-to", "pdf",
                             "--outdir", temporary_directory,
                             data.path.to_s,
                             {
                               :spawn_options => {
                                 :out => output.path,
                                 :err => error_output.path,
                               },
                             })
    unless succeeded
      error do
        tag = "#{log_tag}[convert][exited][abnormally]"
        [
          tag,
          "output: <#{output.read}>",
          "error: <#{error_output.read}>",
        ].join("\n")
      end
      return nil
    end
    pdf_path, = Dir.glob("#{temporary_directory}/*.pdf")
    if pdf_path.nil?
      error do
        tag = "#{log_tag}[convert][failed]"
        message = [
          "#{tag}: LibreOffice may be running",
          "output: <#{output.read}>",
          "error: <#{error_output.read}>",
        ].join("\n")
      end
      return nil
    end
    normalized_pdf_uri = data.uri.to_s.gsub(/\.[^.]+\z/, ".pdf")
    File.open(pdf_path, "rb") do |pdf|
      ChupaText::VirtualFileData.new(normalized_pdf_uri,
                                     pdf,
                                     :source_data => data)
    end
  end
end
expand_candidate(candidate) click to toggle source
# File lib/chupa-text/decomposers/libreoffice.rb, line 93
def expand_candidate(candidate)
  Dir.glob("/opt/libreoffice*/program/#{candidate}").first or
    Dir.glob("C:/Program Files*/libreoffice*/program/#{candidate}.exe").first
end
find_command() click to toggle source
# File lib/chupa-text/decomposers/libreoffice.rb, line 74
def find_command
  candidates = [
    @options[:libreoffice],
    ENV["LIBREOFFICE"],
    "libreoffice",
    "soffice",
  ]
  candidates.each do |candidate|
    next if candidate.nil?
    command = ExternalCommand.new(candidate)
    return command if command.exist?
    expanded_candidate = expand_candidate(candidate)
    next if expanded_candidate.nil?
    command = ExternalCommand.new(expanded_candidate)
    return command if command.exist?
  end
  nil
end
log_tag() click to toggle source
# File lib/chupa-text/decomposers/libreoffice.rb, line 145
def log_tag
  "[decomposer][libreoffice]"
end