Created
June 7, 2017 13:36
-
-
Save awead/57d3a56d5a997ed2b83a408b0c8e35db to your computer and use it in GitHub Desktop.
Version checksum report
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Checks the shasum of each version and current content in old production | |
# and compares it to those in new production. | |
# | |
# To run the report, go to the Rails console and copy-in the two classes in the file. | |
# Then run: | |
# | |
# > report = ChecksumReport.new | |
# | |
# The report is an array of hashes, each of which looks like: | |
# | |
# { id: "1234", valid: true } | |
# | |
class FileChecksum | |
attr_reader :file | |
# @param [GenericFile] gf object from Fedora | |
def initialize(gf) | |
@file = gf | |
end | |
# @return [String] sha1sum of the current content, i.e. what is at /content in Fedora | |
def current | |
return unless file.content.present? | |
file.content.digest.first.to_s | |
end | |
# @return [Hash] sha1sums of each of the file's versions | |
def versions | |
return {} unless file.content.present? | |
file.content.versions.all.each do |v| | |
version_hash[v.label] = shasum_for_version(v) | |
end | |
version_hash | |
end | |
# @return [Boolean] when the current checksum is present in the list of version checksums | |
def valid? | |
versions.values.include?(current) | |
end | |
private | |
def digest_predicate | |
::RDF::URI("http://fedora.info/definitions/v4/repository#digest") | |
end | |
def graph_from_response(response) | |
::RDF::Graph.new << ::RDF::Reader.for(:ttl).new(response.body) | |
end | |
def version_hash | |
@version_hash ||= {} | |
end | |
def shasum_for_version(version) | |
metadata_uri = version.uri + "/fcr:metadata" | |
graph = graph_from_response(ActiveFedora.fedora.connection.get(metadata_uri)) | |
graph.query(predicate: digest_predicate).first.object.to_s | |
end | |
end | |
class ChecksumReport | |
attr_reader :results | |
def initialize(ids = []) | |
@results = [] | |
file_ids = ids.present? ? ids : all_ids | |
file_ids.each do |id| | |
gf = GenericFile.find(id) | |
checksum = FileChecksum.new(gf) | |
@results << { id: id, valid: checksum.valid? } | |
end | |
end | |
private | |
def all_ids | |
ActiveFedora::SolrService.query("active_fedora_model_ssi:GenericFile", fl: [:id], rows: 10_000).map do |hit| | |
hit.fetch("id") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment