• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / pdc_describe / 9091a1ae-29be-458c-984a-339d213919c4

12 Dec 2024 07:41PM UTC coverage: 26.434% (-69.7%) from 96.113%
9091a1ae-29be-458c-984a-339d213919c4

Pull #2000

circleci

jrgriffiniii
Removing integration with ActiveStorage
Pull Request #2000: Bump actionpack from 7.2.1.1 to 7.2.2.1

945 of 3575 relevant lines covered (26.43%)

0.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

31.03
/app/services/work_preservation_service.rb
1
# frozen_string_literal: true
2

3
# A service to create and store the preservation data for a given work.
4
# Currently it assumes this data will be stored in an AWS S3 bucket accessible with our AWS credentials.
5
# This preservation bucket is configured in our s3.yml file and we store it in a different availability region
6
# to make sure the data is properly distributed.
7
class WorkPreservationService
1✔
8
  PRESERVATION_DIRECTORY = "princeton_data_commons"
1✔
9

10
  # @param work_id [Integer] The ID of the work to preserve.
11
  # @param path [String] The path where the work will be preserved.
12
  # @param localhost [Bool] Set to true to preserve the files locally, i.e. not in AWS
13
  def initialize(work_id:, path:, localhost: false)
1✔
14
    @work = Work.find(work_id)
×
15
    @path = path
×
16
    @localhost = localhost
×
17
    @s3_query_service = nil
×
18
  end
19

20
  # Creates and stores the preservation files for the work.
21
  # @return [String] The AWS S3 path where the files were stored
22
  def preserve!
1✔
23
    create_preservation_directory
×
24
    preserve_file(io: metadata_io, filename: "metadata.json")
×
25
    preserve_file(io: datacite_io, filename: "datacite.xml")
×
26
    preserve_file(io: provenance_io, filename: "provenance.json")
×
27
    Rails.logger.info "Preservation files for work #{@work.id} saved to #{target_location}"
×
28
    target_location
×
29
  end
30

31
  def preservation_metadata
1✔
32
    # Always use the post-curation file list
33
    metadata = JSON.parse(@work.to_json(force_post_curation: true))
×
34
    # Make sure we don't include the preservation files as part of
35
    # the work's preservation metadata.
36
    metadata["files"] = metadata["files"].map do |file|
×
37
      if file["filename"].include?("/#{PRESERVATION_DIRECTORY}/")
×
38
        nil
×
39
      else
40
        file
×
41
      end
42
    end.compact
43
    metadata.to_json
×
44
  end
45

46
  private
1✔
47

48
    def local?
1✔
49
      @localhost
×
50
    end
51

52
    def target_location
1✔
53
      if local?
×
54
        "file://" + File.join(Dir.pwd, preservation_directory)
×
55
      else
56
        "s3://#{bucket_name}/#{preservation_directory}"
×
57
      end
58
    end
59

60
    def metadata_io
1✔
61
      StringIO.new(preservation_metadata)
×
62
    end
63

64
    def datacite_io
1✔
65
      StringIO.new(@work.to_xml)
×
66
    end
67

68
    def provenance_io
1✔
69
      StringIO.new(@work.work_activity.to_json)
×
70
    end
71

72
    def preservation_directory
1✔
73
      Pathname.new(@path).join("#{PRESERVATION_DIRECTORY}/")
×
74
    end
75

76
    def s3_query_service
1✔
77
      @s3_query_service ||= S3QueryService.new(@work, "preservation")
×
78
    end
79

80
    def bucket_name
1✔
81
      s3_query_service.bucket_name
×
82
    end
83

84
    def create_preservation_directory
1✔
85
      if local?
×
86
        FileUtils.mkdir_p preservation_directory.to_s
×
87
      else
88
        s3_query_service.client.put_object({ bucket: bucket_name, key: preservation_directory.to_s, content_length: 0 })
×
89
      end
90
    end
91

92
    def preserve_file(io:, filename:)
1✔
93
      if local?
×
94
        save_local_file(io:, filename:)
×
95
      else
96
        upload_file(io:, filename:)
×
97
      end
98
    end
99

100
    def upload_file(io:, filename:)
1✔
101
      md5_digest = @work.s3_query_service.md5(io:)
×
102
      key = preservation_directory.join(filename).to_s
×
103
      s3_query_service.client.put_object(bucket: bucket_name, key:, body: io, content_md5: md5_digest)
×
104
      key
×
105
    end
106

107
    def save_local_file(io:, filename:)
1✔
108
      full_filename = preservation_directory.join(filename).to_s
×
109
      File.open(full_filename, "w") do |file|
×
110
        file.puts(io.read)
×
111
      end
112
      full_filename
×
113
    end
114
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc