Sha256: 7ee919b58acc60d0328a65c89a4727e74539dce7033c757783b7ffa99d72831a

Contents?: true

Size: 1.75 KB

Versions: 5

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

# An abstract class for asyncronous jobs that convert office documents using soffice
module Hydra::Derivatives::Processors
  class Document < Processor
    include ShellBasedProcessor

    def self.encode(path, format, outdir)
      execute "#{Hydra::Derivatives.libreoffice_path} --invisible --headless --convert-to #{format} --outdir #{outdir} #{Shellwords.escape(path)}"
    end

    # Converts the document to the format specified in the directives hash.
    # TODO: file_suffix and options are passed from ShellBasedProcessor.process but are not needed.
    #       A refactor could simplify this.
    def encode_file(_file_suffix, _options = {})
      convert_to_format
    ensure
      FileUtils.rm_f(converted_file)
    end

    private

      # For jpeg files, a pdf is created from the original source and then passed to the Image processor class
      # so we can get a better conversion with resizing options. Otherwise, the ::encode method is used.
      def convert_to_format
        if directives.fetch(:format) == "jpg"
          Hydra::Derivatives::Processors::Image.new(converted_file, directives).process
        else
          output_file_service.call(File.open(converted_file, 'rb'), directives)
        end
      end

      def converted_file
        @converted_file ||= if directives.fetch(:format) == "jpg"
                              convert_to("pdf")
                            else
                              convert_to(directives.fetch(:format))
                            end
      end

      def convert_to(format)
        self.class.encode(source_path, format, Hydra::Derivatives.temp_file_base)
        File.join(Hydra::Derivatives.temp_file_base, [File.basename(source_path, ".*"), format].join('.'))
      end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
hydra-derivatives-4.0.0 lib/hydra/derivatives/processors/document.rb
hydra-derivatives-3.8.0 lib/hydra/derivatives/processors/document.rb
hydra-derivatives-3.7.0 lib/hydra/derivatives/processors/document.rb
hydra-derivatives-3.6.1 lib/hydra/derivatives/processors/document.rb
hydra-derivatives-3.6.0 lib/hydra/derivatives/processors/document.rb