Class: Hydra::Derivatives::Document

Inherits:
Processor
  • Object
show all
Includes:
ShellBasedProcessor
Defined in:
lib/hydra/derivatives/document.rb

Instance Attribute Summary

Attributes inherited from Processor

#directives, #object, #source_name

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ShellBasedProcessor

#options_for, #process

Methods inherited from Processor

#initialize, #output_datastream, #output_datastream_id, #process, #source_datastream

Constructor Details

This class inherits a constructor from Hydra::Derivatives::Processor

Class Method Details

.encode(path, options, output_file) ⇒ Object



6
7
8
9
10
# File 'lib/hydra/derivatives/document.rb', line 6

def self.encode(path, options, output_file)
  format = File.extname(output_file).sub('.', '')
  outdir = File.dirname(output_file)
  execute "#{Hydra::Derivatives.libreoffice_path} --invisible --headless --convert-to #{format} --outdir #{outdir} #{path}"
end

Instance Method Details

#encode_datastream(dest_dsid, file_suffix, mime_type, options = '') ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hydra/derivatives/document.rb', line 12

def encode_datastream(dest_dsid, file_suffix, mime_type, options = '')
  output_file = Dir::Tmpname.create(["#{object.id}-content.", ".#{file_suffix}"], Hydra::Derivatives.temp_file_base){}
  new_output = ''
  source_datastream.to_tempfile do |f|
    if mime_type == 'image/jpeg'
      temp_file = File.join(Hydra::Derivatives.temp_file_base, [File.basename(f.path).sub(File.extname(f.path), ''), 'pdf'].join('.'))
      new_output = File.join(Hydra::Derivatives.temp_file_base, [File.basename(temp_file).sub(File.extname(temp_file), ''), file_suffix].join('.'))
      self.class.encode(f.path, options, temp_file)
      self.class.encode(temp_file, options, output_file)
      File.unlink(temp_file)
    else
      self.class.encode(f.path, options, output_file)
      new_output = File.join(Hydra::Derivatives.temp_file_base, [File.basename(f.path).sub(File.extname(f.path), ''), file_suffix].join('.'))
    end
  end
  out_file = File.open(new_output, "rb")
  object.add_file_datastream(out_file.read, dsid: dest_dsid, mimeType: mime_type)
  File.unlink(out_file)
end

#new_mime_type(format) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/hydra/derivatives/document.rb', line 32

def new_mime_type(format)
  case format
  when 'pdf'
    'application/pdf'
  when 'odf'
    'application/vnd.oasis.opendocument.text'
  when 'docx'
    'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
  when 'xslx'
    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  when 'pptx'
    'application/vnd.openxmlformats-officedocument.presentationml.presentation'
  when 'jpg'
    'image/jpeg'
  else
    raise "I don't know about the format '#{format}'"
  end
end