Class: Valkyrie::StorageAdapter::File

Inherits:
Dry::Struct
  • Object
show all
Defined in:
lib/valkyrie/storage_adapter.rb

Direct Known Subclasses

StreamFile

Instance Method Summary collapse

Instance Method Details

#checksum(digests:) ⇒ Array<Digest>

Parameters:

  • digests (Array<Digest>)

Returns:

  • (Array<Digest>)


89
90
91
92
93
94
95
96
# File 'lib/valkyrie/storage_adapter.rb', line 89

def checksum(digests:)
  io.rewind
  while (chunk = io.read(4096))
    digests.each { |digest| digest.update(chunk) }
  end

  digests.map(&:to_s)
end

#disk_pathObject



78
79
80
81
82
83
84
85
# File 'lib/valkyrie/storage_adapter.rb', line 78

def disk_path
  path = Pathname.new(io.path)
  if block_given?
    yield path
  else
    path
  end
end

#streamObject



74
75
76
# File 'lib/valkyrie/storage_adapter.rb', line 74

def stream
  io
end

#valid?(size: nil, digests:) ⇒ Boolean

Parameters:

  • size (Integer) (defaults to: nil)
  • digests (Hash<String, String>)

    map of digest algorithm names to digest values.

Returns:

  • (Boolean)


101
102
103
104
105
106
107
# File 'lib/valkyrie/storage_adapter.rb', line 101

def valid?(size: nil, digests:)
  return false if size && io.size.to_i != size.to_i
  calc_digests = checksum(digests: digests.keys.map { |alg| Digest(alg.upcase).new })
  return false unless digests.values == calc_digests

  true
end