Class: Hydra::Derivatives::TempfileService

Inherits:
Object
  • Object
show all
Defined in:
lib/hydra/derivatives/services/tempfile_service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_file) ⇒ TempfileService

Returns a new instance of TempfileService.



12
13
14
# File 'lib/hydra/derivatives/services/tempfile_service.rb', line 12

def initialize(source_file)
  @source_file = source_file
end

Instance Attribute Details

#source_fileObject (readonly)

Returns the value of attribute source_file.



10
11
12
# File 'lib/hydra/derivatives/services/tempfile_service.rb', line 10

def source_file
  @source_file
end

Class Method Details

.create(file, &block) ⇒ Object



6
7
8
# File 'lib/hydra/derivatives/services/tempfile_service.rb', line 6

def self.create(file, &block)
  new(file).tempfile(&block)
end

Instance Method Details

#default_tempfile(&_block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hydra/derivatives/services/tempfile_service.rb', line 24

def default_tempfile(&_block)
  Tempfile.open(filename_for_characterization) do |f|
    f.binmode
    if source_file.content.respond_to? :read
      f.write(source_file.content.read)
    else
      f.write(source_file.content)
    end
    source_file.content.rewind if source_file.content.respond_to? :rewind
    f.rewind
    yield(f)
  end
end

#filename_for_characterizationObject



38
39
40
41
42
43
44
45
# File 'lib/hydra/derivatives/services/tempfile_service.rb', line 38

def filename_for_characterization
  registered_mime_type = MIME::Types[source_file.mime_type].first
  Logger.warn "Unable to find a registered mime type for #{source_file.mime_type.inspect} on #{source_file.uri}" unless registered_mime_type
  extension = registered_mime_type ? ".#{registered_mime_type.extensions.first}" : ''
  version_id = 1 # TODO: fixme
  m = %r{/([^/]*)$}.match(source_file.uri)
  ["#{m[1]}-#{version_id}", extension.to_s]
end

#tempfile(&block) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/hydra/derivatives/services/tempfile_service.rb', line 16

def tempfile(&block)
  if source_file.respond_to? :to_tempfile
    source_file.send(:to_tempfile, &block)
  elsif source_file.has_content?
    default_tempfile(&block)
  end
end