|
| 1 | +# == Schema Information |
| 2 | +# |
| 3 | +# Table name: archivematica_payloads |
| 4 | +# |
| 5 | +# id :integer not null, primary key |
| 6 | +# preservation_status :integer default("unpreserved"), not null |
| 7 | +# payload_json :text |
| 8 | +# preserved_at :datetime |
| 9 | +# thesis_id :integer not null |
| 10 | +# created_at :datetime not null |
| 11 | +# updated_at :datetime not null |
| 12 | +# |
| 13 | +# This class assembles a payload to send to the Archival Packaging Tool (APT), which then creates a bag for |
| 14 | +# preservation. It includes the thesis files, metadata, and checksums. The payload is then serialized to JSON |
| 15 | +# for transmission. |
| 16 | +# |
| 17 | +# Instances of this class are invalid without an associated thesis that has a DSpace handle, a copyright, and |
| 18 | +# at least one attached file with no duplicate filenames. |
| 19 | +# |
| 20 | +# There is some intentional duplication between this and the SubmissionInformationPackage model. The |
| 21 | +# SubmissionInformationPackage is the legacy model that was used to create the bag, but it is not |
| 22 | +# used in the current APT workflow. We are retaining it for historical purposes. |
| 23 | +class ArchivematicaPayload < ApplicationRecord |
| 24 | + include Checksums |
| 25 | + include Baggable |
| 26 | + |
| 27 | + has_paper_trail |
| 28 | + belongs_to :thesis |
| 29 | + has_one_attached :metadata_csv |
| 30 | + |
| 31 | + validates :baggable?, presence: true |
| 32 | + |
| 33 | + before_create :set_metadata_csv, :set_payload_json |
| 34 | + |
| 35 | + enum preservation_status: %i[unpreserved preserved] |
| 36 | + |
| 37 | + private |
| 38 | + |
| 39 | + # compress_zip is cast to a boolean to override the string value from ENV. APT strictly requires |
| 40 | + # a boolean for this field. |
| 41 | + def build_payload |
| 42 | + { |
| 43 | + action: 'create-bagit-zip', |
| 44 | + challenge_secret: ENV.fetch('APT_CHALLENGE_SECRET', nil), |
| 45 | + verbose: ActiveModel::Type::Boolean.new.cast(ENV.fetch('APT_VERBOSE', false)), |
| 46 | + input_files: build_input_files, |
| 47 | + checksums_to_generate: ENV.fetch('APT_CHECKSUMS_TO_GENERATE', ['md5']), |
| 48 | + output_zip_s3_uri: bag_output_uri, |
| 49 | + compress_zip: ActiveModel::Type::Boolean.new.cast(ENV.fetch('APT_COMPRESS_ZIP', true)) |
| 50 | + } |
| 51 | + end |
| 52 | + |
| 53 | + # Build input_files array from thesis files and attached metadata CSV |
| 54 | + def build_input_files |
| 55 | + files = thesis.files.map { |file| build_file_entry(file) } |
| 56 | + files << build_file_entry(metadata_csv) # Metadata CSV is the only file that is generated in this model |
| 57 | + files |
| 58 | + end |
| 59 | + |
| 60 | + # Build a file entry for each file, including the metadata CSV. |
| 61 | + def build_file_entry(file) |
| 62 | + { |
| 63 | + uri: ["s3://#{ENV.fetch('AWS_S3_BUCKET')}", file.blob.key].join('/'), |
| 64 | + filepath: set_filepath(file), |
| 65 | + checksums: { |
| 66 | + md5: base64_to_hex(file.blob.checksum) |
| 67 | + } |
| 68 | + } |
| 69 | + end |
| 70 | + |
| 71 | + def set_filepath(file) |
| 72 | + file == metadata_csv ? 'metadata/metadata.csv' : file.filename.to_s |
| 73 | + end |
| 74 | + |
| 75 | + # The bag_name has to be unique due to our using it as the basis of an ActiveStorage key. Using a UUID |
| 76 | + # was not preferred as the target system of these bags adds it's own UUID to the file when it arrives there |
| 77 | + # so the filename was unwieldy with two UUIDs embedded in it so we simply increment integers. |
| 78 | + def bag_name |
| 79 | + safe_handle = thesis.dspace_handle.gsub('/', '_') |
| 80 | + "#{safe_handle}-thesis-#{thesis.submission_information_packages.count + 1}" |
| 81 | + end |
| 82 | + |
| 83 | + # The bag_output_uri key is constructed to match the expected format for Archivematica. |
| 84 | + def bag_output_uri |
| 85 | + key = "etdsip/#{thesis.graduation_year}/#{thesis.graduation_month}-#{thesis.accession_number}/#{bag_name}.zip" |
| 86 | + [ENV.fetch('APT_S3_BUCKET'), key].join('/') |
| 87 | + end |
| 88 | + |
| 89 | + def baggable? |
| 90 | + baggable_thesis?(thesis) |
| 91 | + end |
| 92 | + |
| 93 | + def set_metadata_csv |
| 94 | + csv_data = ArchivematicaMetadata.new(thesis).to_csv |
| 95 | + metadata_csv.attach(io: StringIO.new(csv_data), filename: 'metadata.csv', content_type: 'text/csv') |
| 96 | + end |
| 97 | + |
| 98 | + def set_payload_json |
| 99 | + self.payload_json = build_payload.to_json |
| 100 | + end |
| 101 | +end |
0 commit comments