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

pulibrary / pdc_describe / 4e4e59fc-9df4-4838-9fd4-6c7ea33cdb7c

07 Apr 2025 06:36PM UTC coverage: 1.283% (-94.6%) from 95.862%
4e4e59fc-9df4-4838-9fd4-6c7ea33cdb7c

Pull #1994

circleci

hectorcorrea
Switched to use the autocomplete that we aleady use for ROR. Integrated it with the existing logic for creators
Pull Request #1994: Started adding auto complete to contributors

0 of 46 new or added lines in 2 files covered. (0.0%)

4806 existing lines in 74 files now uncovered.

65 of 5065 relevant lines covered (1.28%)

0.01 hits per line

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

0.0
/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.
UNCOV
7
class WorkPreservationService
×
UNCOV
8
  PRESERVATION_DIRECTORY = "princeton_data_commons"
×
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
UNCOV
13
  def initialize(work_id:, path:, localhost: false)
×
UNCOV
14
    @work = Work.find(work_id)
×
UNCOV
15
    @path = path
×
UNCOV
16
    @localhost = localhost
×
UNCOV
17
    @s3_query_service = nil
×
UNCOV
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
UNCOV
22
  def preserve!
×
UNCOV
23
    create_preservation_directory
×
UNCOV
24
    preserve_file(io: metadata_io, filename: "metadata.json")
×
UNCOV
25
    preserve_file(io: datacite_io, filename: "datacite.xml")
×
UNCOV
26
    preserve_file(io: provenance_io, filename: "provenance.json")
×
UNCOV
27
    Rails.logger.info "Preservation files for work #{@work.id} saved to #{target_location}"
×
UNCOV
28
    target_location
×
UNCOV
29
  end
×
30

UNCOV
31
  def preservation_metadata
×
32
    # Always use the post-curation file list
UNCOV
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.
UNCOV
36
    metadata["files"] = metadata["files"].map do |file|
×
UNCOV
37
      if file["filename"].include?("/#{PRESERVATION_DIRECTORY}/")
×
UNCOV
38
        nil
×
UNCOV
39
      else
×
UNCOV
40
        file
×
UNCOV
41
      end
×
UNCOV
42
    end.compact
×
UNCOV
43
    metadata.to_json
×
UNCOV
44
  end
×
45

UNCOV
46
  private
×
47

UNCOV
48
    def local?
×
UNCOV
49
      @localhost
×
UNCOV
50
    end
×
51

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

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

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

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

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

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

UNCOV
80
    def bucket_name
×
UNCOV
81
      s3_query_service.bucket_name
×
UNCOV
82
    end
×
83

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

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

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

UNCOV
107
    def save_local_file(io:, filename:)
×
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)
×
UNCOV
111
      end
×
UNCOV
112
      full_filename
×
UNCOV
113
    end
×
UNCOV
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