purl_fetcher-client 1.3.0 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9b237518aad8cb3c8c617c36191a1e2b5fd81e7792e035ca920ca589c4d6de5f
4
- data.tar.gz: e81b7dbf9684ab37db18a4169edcd76a0d7a9914f5514472933985caeafa5cfa
3
+ metadata.gz: 027601e72f94ac6befda0395719b6d334221fce396e2c6ef306c7ec012e19609
4
+ data.tar.gz: f77c4469ee2f67e340db177d092844011619725aa28681e3ef074b362532715e
5
5
  SHA512:
6
- metadata.gz: 9585d2dada61a2353d584e07ad0adbe6b977b45bd638bcd12d0b0c0ecf22654828b0daf955a012d4ddc44e97c2085e1a11aee67b08f6f27ef7a805ddc3645851
7
- data.tar.gz: 3fe290818961d4dcadc5486079e63edc568a35e96f4b5a6cfebfaa8b94099361a3bb68369046ef24dd46b502bc6b0c9ddda08c581a7576ced156f6410b9833c6
6
+ metadata.gz: 2b704e9fac0330914822891a6ddd73034b7100eb11653cd506699569d341eefda56613949a64fffd4616bbfc7b63404ff529ef386228c02c54887e7316dc12e9
7
+ data.tar.gz: 43eabc9ce42dcdc937cacac235550bdb396096cce0caeedf8134976b0c50aaf8848065da739aa6afec8a6c0d3d83a96a9f182e6538c246820c74c158129d4ee3
data/Gemfile CHANGED
@@ -7,3 +7,4 @@ gemspec
7
7
 
8
8
  gem "rubocop-rails-omakase", require: false, group: [ :development ]
9
9
  gem "debug"
10
+ gem "cocina-models"
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PurlFetcher
4
+ class Client
5
+ # Publish (metadata-only) to the purl cache
6
+ class Publish
7
+ def self.publish(...)
8
+ new(...).publish
9
+ end
10
+
11
+ # @param [Cocina::Models::DRO,Cocina::Models::Collection] cocina the Cocina data object
12
+ # @param [Hash<String,String>] file_uploads map of filenames to signed_ids
13
+ def initialize(cocina:, file_uploads:)
14
+ @cocina = cocina
15
+ @file_uploads = file_uploads
16
+ end
17
+
18
+ def publish
19
+ logger.debug("Starting a publish request for: #{druid}")
20
+ client.post(path:, body:)
21
+ logger.debug("Publish request complete")
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :cocina, :file_uploads
27
+
28
+ def druid
29
+ cocina.externalIdentifier
30
+ end
31
+
32
+ def body
33
+ {
34
+ object: cocina.to_h,
35
+ file_uploads: file_uploads
36
+ }.to_json
37
+ end
38
+
39
+ def logger
40
+ Client.config.logger
41
+ end
42
+
43
+ def client
44
+ Client.instance
45
+ end
46
+
47
+ def path
48
+ "/v1/resources"
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PurlFetcher
4
+ class Client
5
+ # High-level client for publishing and shelving.
6
+ class PublishShelve
7
+ def self.publish_and_shelve(...)
8
+ new(...).publish_and_shelve
9
+ end
10
+
11
+ # @param [Cocina::Models::DRO,Cocina::Models::Collection] cocina the Cocina data object
12
+ # @param [Hash<String,String>] filepath_map map of relative filepaths to absolute filepaths
13
+ def initialize(cocina:, filepath_map:)
14
+ @cocina = cocina
15
+ @filepath_map = filepath_map
16
+ end
17
+
18
+ def publish_and_shelve
19
+ logger.debug("Starting publish and shelve for: #{cocina.externalIdentifier}")
20
+
21
+ direct_upload_responses = PurlFetcher::Client::UploadFiles.upload(file_metadata: file_metadata, filepath_map: filepath_map)
22
+ file_uploads = direct_upload_responses.map { |response| [ response.filename, response.signed_id ] }.to_h
23
+
24
+ PurlFetcher::Client::Publish.publish(cocina: cocina, file_uploads: file_uploads)
25
+ logger.debug("Publish and shelve complete")
26
+ end
27
+
28
+ private
29
+
30
+ attr_reader :cocina, :filepath_map
31
+
32
+ def file_metadata
33
+ return [] unless cocina.dro?
34
+
35
+ cocina.structural.contains.flat_map do |fileset|
36
+ fileset.structural.contains.select { |file| filepath_map.include?(file.filename) }.map do |file|
37
+ direct_upload_request_for(file)
38
+ end
39
+ end
40
+ end
41
+
42
+ def direct_upload_request_for(cocina_file)
43
+ PurlFetcher::Client::DirectUploadRequest.from_file(
44
+ hexdigest: md5_for(cocina_file),
45
+ byte_size: size_for(cocina_file),
46
+ content_type: "application/octet-stream",
47
+ file_name: cocina_file.filename
48
+ )
49
+ end
50
+
51
+ def md5_for(cocina_file)
52
+ cocina_file.hasMessageDigests.find { |digest| digest.type == "md5" }.digest
53
+ end
54
+
55
+ def size_for(cocina_file)
56
+ return cocina_file.size if cocina_file.size.present? && cocina_file.size.positive?
57
+
58
+ File.size(filepath_map[cocina_file.filename])
59
+ end
60
+
61
+ def logger
62
+ Client.config.logger
63
+ end
64
+ end
65
+ end
66
+ end
@@ -10,7 +10,7 @@ module PurlFetcher
10
10
  new(file_metadata: file_metadata, filepath_map: filepath_map).upload
11
11
  end
12
12
 
13
- # @param [Hash<String,DirectUploadRequest>] file_metadata map of relative filepaths to file metadata
13
+ # @param [Array<DirectUploadRequest>] file_metadata array of DirectUploadRequests for the files to be uploaded
14
14
  # @param [Hash<String,String>] filepath_map map of relative filepaths to absolute filepaths
15
15
  def initialize(file_metadata:, filepath_map:)
16
16
  @file_metadata = file_metadata
@@ -19,10 +19,10 @@ module PurlFetcher
19
19
 
20
20
  # @return [Array<DirectUploadResponse>] the responses from the server for the uploads
21
21
  def upload
22
- file_metadata.map do |filepath, metadata|
23
- direct_upload(metadata.to_json).tap do |response|
24
- # ActiveStorage modifies the filename provided in response, so setting here with the relative filename
25
- response = response.with_filename(filepath)
22
+ file_metadata.map do |metadata|
23
+ filepath = metadata.filename
24
+ # ActiveStorage modifies the filename provided in response, so setting here with the relative filename
25
+ direct_upload(metadata.to_json).with_filename(filepath).tap do |response|
26
26
  upload_file(response)
27
27
  logger.info("Upload of #{filepath} complete")
28
28
  end
@@ -1,5 +1,5 @@
1
1
  module PurlFetcher
2
2
  class Client
3
- VERSION = "1.3.0"
3
+ VERSION = "1.4.1"
4
4
  end
5
5
  end
@@ -11,8 +11,10 @@ require "purl_fetcher/client/upload_files"
11
11
  require "purl_fetcher/client/direct_upload_request"
12
12
  require "purl_fetcher/client/direct_upload_response"
13
13
  require "purl_fetcher/client/legacy_publish"
14
+ require "purl_fetcher/client/publish"
14
15
  require "purl_fetcher/client/release_tags"
15
16
  require "purl_fetcher/client/unpublish"
17
+ require "purl_fetcher/client/publish_shelve"
16
18
 
17
19
  module PurlFetcher
18
20
  class Client
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: purl_fetcher-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Beer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-05-20 00:00:00.000000000 Z
11
+ date: 2024-05-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -130,6 +130,8 @@ files:
130
130
  - lib/purl_fetcher/client/direct_upload_response.rb
131
131
  - lib/purl_fetcher/client/legacy_publish.rb
132
132
  - lib/purl_fetcher/client/mods.rb
133
+ - lib/purl_fetcher/client/publish.rb
134
+ - lib/purl_fetcher/client/publish_shelve.rb
133
135
  - lib/purl_fetcher/client/reader.rb
134
136
  - lib/purl_fetcher/client/release_tags.rb
135
137
  - lib/purl_fetcher/client/unpublish.rb
@@ -154,7 +156,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
154
156
  - !ruby/object:Gem::Version
155
157
  version: '0'
156
158
  requirements: []
157
- rubygems_version: 3.5.10
159
+ rubygems_version: 3.4.19
158
160
  signing_key:
159
161
  specification_version: 4
160
162
  summary: Traject-compatible reader implementation for streaming data from purl-fetcher