class Ddr::Models::WithContentFile

Public Class Methods

new(obj, &block) click to toggle source
Calls superclass method
# File lib/ddr/models/with_content_file.rb, line 7
def initialize(obj, &block)
  super(obj)
  with_temp_file &block
end

Public Instance Methods

verify_checksum!(file) click to toggle source
# 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() { |path| ... } click to toggle source

Yields path of tempfile containing content to block @yield [String] the path to the tempfile containing content

# 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