Class: Ddr::Models::WithContentFile

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/ddr/models/with_content_file.rb

Instance Method Summary collapse

Constructor Details

#initialize(obj, &block) ⇒ WithContentFile

Returns a new instance of WithContentFile.



7
8
9
10
# File 'lib/ddr/models/with_content_file.rb', line 7

def initialize(obj, &block)
  super(obj)
  with_temp_file &block
end

Instance Method Details

#verify_checksum!(file) ⇒ Object



29
30
31
32
33
34
# File 'lib/ddr/models/with_content_file.rb', line 29

def verify_checksum!(file)
  digest = Ddr::Utils.digest(::File.read(file), content.checksum.algorithm)
  if digest != content.checksum.value
    raise ChecksumInvalid, "The checksum of the downloaded file does not match the stored checksum of the content."
  end
end

#with_temp_file {|String| ... } ⇒ Object

Yields path of tempfile containing content to block

Yields:

  • (String)

    the path to the tempfile containing content



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ddr/models/with_content_file.rb', line 14

def with_temp_file
  filename = original_filename || content.default_file_name
  basename = [ ::File.basename(filename, ".*"), ::File.extname(filename) ]
  infile = Tempfile.open(basename, Ddr::Models.tempdir, encoding: 'ascii-8bit')
  begin
    infile.write(content.content)
    infile.close
    verify_checksum!(infile)
    yield infile.path
  ensure
    infile.close unless infile.closed?
    ::File.unlink(infile)
  end
end