Class: Hydra::Derivatives::Document
- Includes:
- ShellBasedProcessor
- Defined in:
- lib/hydra/derivatives/document.rb
Instance Attribute Summary
Attributes inherited from Processor
#directives, #object, #output_file_service, #source_file_service, #source_name
Class Method Summary collapse
Instance Method Summary collapse
- #encode_file(destination_name, file_suffix, mime_type, options = { }) ⇒ Object
- #new_mime_type(format) ⇒ Object
- #output_file(file_suffix) ⇒ Object
Methods included from ShellBasedProcessor
Methods inherited from Processor
#initialize, #output_file_id, #process, #source_file
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, , 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_file(destination_name, 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_file(destination_name, file_suffix, mime_type, = { }) new_output = '' Hydra::Derivatives::TempfileService.create(source_file) 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, , temp_file) self.class.encode(temp_file, , output_file(file_suffix)) File.unlink(temp_file) else self.class.encode(f.path, , output_file(file_suffix)) 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 = Hydra::Derivatives::IoDecorator.new(File.open(new_output, "rb")) out_file.mime_type = mime_type output_file_service.call(object, out_file, destination_name) File.unlink(out_file) end |
#new_mime_type(format) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/hydra/derivatives/document.rb', line 36 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 |
#output_file(file_suffix) ⇒ Object
32 33 34 |
# File 'lib/hydra/derivatives/document.rb', line 32 def output_file(file_suffix) Dir::Tmpname.create(["#{object.id.gsub('/', '_')}-content.", ".#{file_suffix}"], Hydra::Derivatives.temp_file_base){} end |