Sha256: 39f8aff3e8b59201dd8475a9335fa0952a0db914ed6821812131f9cd4030e92a

Contents?: true

Size: 1.61 KB

Versions: 4

Compression:

Stored size: 1.61 KB

Contents

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} #{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.read(converted_file), 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

4 entries across 4 versions & 1 rubygems

Version Path
hydra-derivatives-3.2.2 lib/hydra/derivatives/processors/document.rb
hydra-derivatives-3.2.1 lib/hydra/derivatives/processors/document.rb
hydra-derivatives-3.2.0 lib/hydra/derivatives/processors/document.rb
hydra-derivatives-3.1.4 lib/hydra/derivatives/processors/document.rb