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

pulibrary / pdc_describe / b1776731-62d7-41a7-882d-6a5b4760db75

14 May 2024 12:28PM UTC coverage: 79.223% (-16.7%) from 95.9%
b1776731-62d7-41a7-882d-6a5b4760db75

push

circleci

carolyncole
Adding a submission completion page

fixes #1791

9 of 9 new or added lines in 2 files covered. (100.0%)

567 existing lines in 42 files now uncovered.

2692 of 3398 relevant lines covered (79.22%)

60.03 hits per line

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

82.76
/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)
2✔
15
    @path = path
2✔
16
    @localhost = localhost
2✔
17
    @s3_query_service = nil
2✔
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
2✔
24
    preserve_file(io: metadata_io, filename: "metadata.json")
2✔
25
    preserve_file(io: datacite_io, filename: "datacite.xml")
2✔
26
    preserve_file(io: provenance_io, filename: "provenance.json")
2✔
27
    Rails.logger.info "Preservation files for work #{@work.id} saved to #{target_location}"
2✔
28
    target_location
2✔
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))
2✔
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|
2✔
UNCOV
37
      if file["filename"].include?("/#{PRESERVATION_DIRECTORY}/")
×
UNCOV
38
        nil
×
39
      else
UNCOV
40
        file
×
41
      end
42
    end.compact
43
    metadata.to_json
2✔
44
  end
45

46
  private
1✔
47

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

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

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

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

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

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

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

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

84
    def create_preservation_directory
1✔
85
      if local?
2✔
UNCOV
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 })
2✔
89
      end
90
    end
91

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

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

107
    def save_local_file(io:, filename:)
1✔
UNCOV
108
      full_filename = preservation_directory.join(filename).to_s
×
UNCOV
109
      File.open(full_filename, "w") do |file|
×
UNCOV
110
        file.puts(io.read)
×
111
      end
UNCOV
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