sdr-client 0.1.0 → 0.35.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +35 -0
  3. data/.github/pull_request_template.md +8 -1
  4. data/.rubocop.yml +7 -4
  5. data/.rubocop_todo.yml +17 -10
  6. data/README.md +34 -3
  7. data/bin/console +1 -1
  8. data/exe/sdr +90 -13
  9. data/lib/sdr-client.rb +3 -0
  10. data/lib/sdr_client/background_job_results.rb +21 -0
  11. data/lib/sdr_client/cli.rb +76 -2
  12. data/lib/sdr_client/connection.rb +41 -0
  13. data/lib/sdr_client/credentials.rb +35 -0
  14. data/lib/sdr_client/deposit/file.rb +64 -0
  15. data/lib/sdr_client/deposit/file_metadata_builder.rb +51 -0
  16. data/lib/sdr_client/deposit/file_metadata_builder_operations/md5.rb +17 -0
  17. data/lib/sdr_client/deposit/file_metadata_builder_operations/mime_type.rb +18 -0
  18. data/lib/sdr_client/deposit/file_metadata_builder_operations/sha1.rb +17 -0
  19. data/lib/sdr_client/deposit/file_set.rb +51 -0
  20. data/lib/sdr_client/deposit/files/direct_upload_request.rb +5 -5
  21. data/lib/sdr_client/deposit/matching_file_grouping_strategy.rb +17 -0
  22. data/lib/sdr_client/deposit/metadata_builder.rb +58 -0
  23. data/lib/sdr_client/deposit/model_process.rb +108 -0
  24. data/lib/sdr_client/deposit/process.rb +41 -71
  25. data/lib/sdr_client/deposit/request.rb +105 -26
  26. data/lib/sdr_client/deposit/single_file_grouping_strategy.rb +14 -0
  27. data/lib/sdr_client/deposit/upload_files.rb +88 -0
  28. data/lib/sdr_client/deposit/upload_resource.rb +62 -0
  29. data/lib/sdr_client/deposit.rb +69 -4
  30. data/lib/sdr_client/login.rb +26 -0
  31. data/lib/sdr_client/login_prompt.rb +19 -0
  32. data/lib/sdr_client/version.rb +1 -1
  33. data/lib/sdr_client.rb +13 -1
  34. data/sdr-client.gemspec +5 -2
  35. metadata +74 -14
  36. data/.travis.yml +0 -17
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 97ae47ce6b50c4fd12228990d25dc3fb63432ba1a1e0fff9a1e614dece546c69
4
- data.tar.gz: f64a044b2373cda941ec7fe593d2eacc1602a6178963cf80c0637415ecded9f4
3
+ metadata.gz: 551ee50a8e581a8a0e7cadcffec518168ef2b0fbba5368520ffeaaa086ccb66e
4
+ data.tar.gz: 5ca9d3232ccfac283d09d68310c9c4fa3355943c90ef5d5bc911f3367bea5396
5
5
  SHA512:
6
- metadata.gz: c573233fcff72bb1c1402d170d048b03182ac7dd45c81a7531dd02529869f2354c9d73bd8522cbb1ba463c52bb933cf692a67e3b7932c77f3746bf2276253913
7
- data.tar.gz: '0848314bb1fd0b02ca2add9e09c92185fbcf8d9db642d683fcb116dae2cbd7ca6df97d5e9e3464f4b2ab71fa1027eda3d67d07723b50bf3ffc6f08274590f649'
6
+ metadata.gz: fa183be1fd002a39f25816b3f990123b3a5324e87f26fda44115537d31aeeace1915f53ba45e4432760ba9e08089da9afcec7b96061fd96ea8108a7d6d0b2cf3
7
+ data.tar.gz: ee6b43d4d29222f0aaec528ff85e1e2c8c657c9c659fd8089bd105c634a94c59d14275501501af4707c929d9e16606f11b815b267be0d95073d8579e7b2055ea
@@ -0,0 +1,35 @@
1
+ version: 2.1
2
+
3
+ jobs:
4
+ build:
5
+ docker:
6
+ - image: circleci/ruby:2.7.1
7
+ environment:
8
+ CC_TEST_REPORTER_ID: 859fcfe88b00c026d15dce30e838e2299face8088b49fe62bc3a02d1507ce3d5
9
+ RAILS_ENV: test
10
+ steps:
11
+ - checkout
12
+ - run:
13
+ name: Install Bundler
14
+ command: gem install bundler
15
+ - run:
16
+ name: Which bundler?
17
+ command: bundle -v
18
+ - run:
19
+ name: Bundle Install
20
+ command: bundle check || bundle install
21
+ - run:
22
+ name: Lint using rubocop
23
+ command: bundle exec rubocop
24
+ - run:
25
+ name: Setup Code Climate test-reporter
26
+ command: |
27
+ curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
28
+ chmod +x ./cc-test-reporter
29
+ ./cc-test-reporter before-build
30
+ - run:
31
+ name: rspec
32
+ command: bundle exec rspec
33
+ - run:
34
+ name: upload test coverage report to Code Climate
35
+ command: ./cc-test-reporter after-build --coverage-input-type simplecov --exit-code $?
@@ -2,4 +2,11 @@
2
2
 
3
3
 
4
4
 
5
- ## Was the documentation (README, wiki) updated?
5
+ ## How was this change tested?
6
+
7
+
8
+
9
+ ## Which documentation and/or configurations were updated?
10
+
11
+
12
+
data/.rubocop.yml CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  inherit_from: .rubocop_todo.yml
3
2
 
4
3
  AllCops:
@@ -7,9 +6,13 @@ AllCops:
7
6
  - 'bin/console'
8
7
  - 'vendor/**/*'
9
8
 
10
- Layout/LineLength:
11
- Max: 120
12
-
13
9
  Metrics/BlockLength:
14
10
  Exclude:
15
11
  - 'spec/**/*'
12
+ - 'sdr-client.gemspec'
13
+ ExcludedMethods:
14
+ - 'OptionParser.new'
15
+
16
+ Naming/FileName:
17
+ Exclude:
18
+ - 'lib/sdr-client.rb'
data/.rubocop_todo.yml CHANGED
@@ -1,22 +1,29 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2020-01-07 23:33:21 -0800 using RuboCop version 0.79.0.
3
+ # on 2020-09-08 13:16:08 -0500 using RuboCop version 0.79.0.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
+ # Offense count: 2
10
+ Metrics/AbcSize:
11
+ Max: 16
12
+
13
+ # Offense count: 8
14
+ # Configuration parameters: CountComments, ExcludedMethods.
15
+ Metrics/MethodLength:
16
+ Max: 15
17
+
9
18
  # Offense count: 1
10
19
  # Cop supports --auto-correct.
11
- # Configuration parameters: AllowForAlignment, AllowBeforeTrailingComments, ForceEqualSignAlignment.
12
- Layout/ExtraSpacing:
20
+ Style/IfUnlessModifier:
13
21
  Exclude:
14
- - 'sdr-client.gemspec'
22
+ - 'exe/sdr'
15
23
 
16
- # Offense count: 1
24
+ # Offense count: 188
17
25
  # Cop supports --auto-correct.
18
- # Configuration parameters: AllowForAlignment, EnforcedStyleForExponentOperator.
19
- # SupportedStylesForExponentOperator: space, no_space
20
- Layout/SpaceAroundOperators:
21
- Exclude:
22
- - 'sdr-client.gemspec'
26
+ # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
27
+ # URISchemes: http, https
28
+ Layout/LineLength:
29
+ Max: 1230
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![Build Status](https://travis-ci.org/sul-dlss/sdr-client.svg?branch=master)](https://travis-ci.org/sul-dlss/sdr-client)
1
+ [![CircleCI](https://circleci.com/gh/sul-dlss/sdr-client.svg?style=svg)](https://circleci.com/gh/sul-dlss/sdr-client)
2
2
  [![Maintainability](https://api.codeclimate.com/v1/badges/1210855d46d4f424bf30/maintainability)](https://codeclimate.com/github/sul-dlss/sdr-client/maintainability)
3
3
  [![Test Coverage](https://api.codeclimate.com/v1/badges/1210855d46d4f424bf30/test_coverage)](https://codeclimate.com/github/sul-dlss/sdr-client/test_coverage)
4
4
  [![Gem Version](https://badge.fury.io/rb/sdr-client.svg)](https://badge.fury.io/rb/sdr-client)
@@ -8,9 +8,40 @@
8
8
  This is a CLI for interacting with the Stanford Digital Repository API.
9
9
  The code for the SDR API server is at https://github.com/sul-dlss/sdr-api
10
10
 
11
+ This provides a way for consumers to easily and correctly deposit files to the SDR without requiring access to the `/dor` NFS mount or to use Hydrus. A primary design goal was for this to have as few dependencies as possible so that it can be easily distributed by `gem install sdr-client` and then it can be used as a CLI.
12
+
13
+ ## Install
14
+
15
+ `gem install sdr-client`
16
+
11
17
  ## Usage
12
18
 
13
- Deposit a new object:
19
+ Log in:
20
+ ```
21
+ sdr --service-url http://sdr-api-server:3000 login
22
+ ```
23
+
24
+ Register a new object:
25
+ ```
26
+ sdr --service-url https://sdr-api-server:3000 register --label 'hey there' \
27
+ --admin-policy 'druid:bk123gh4567' \
28
+ --collection 'druid:gh456kw9876' \
29
+ --source-id 'googlebooks:stanford_12345' file1.png file2.png
30
+ ```
31
+
32
+ Deposit (register + accession) a new object:
33
+ ```
34
+ sdr --service-url https://sdr-api-server:3000 deposit --label 'hey there' \
35
+ --admin-policy 'druid:bk123gh4567' \
36
+ --collection 'druid:gh456kw9876' \
37
+ --source-id 'googlebooks:stanford_12345' file1.png file2.png
38
+ ```
39
+
40
+ Deposit a new object, providing metadata for files:
14
41
  ```
15
- sdr --service-url http://sdr-api-server:3000 deposit --label 'hey there' file1.png file2.png
42
+ sdr --service-url https://sdr-api-server:3000 deposit --label 'hey there' \
43
+ --files-metadata '{"image42.jp2":{"mime_type":"image/jp2"},"ocr.html":{"use":"transcription"}}'
44
+ --admin-policy 'druid:bk123gh4567' \
45
+ --collection 'druid:gh456kw9876' \
46
+ --source-id 'googlebooks:stanford_12345' image42.jp2 ocr.html
16
47
  ```
data/bin/console CHANGED
@@ -2,7 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'bundler/setup'
5
- require 'sdr/client'
5
+ require 'sdr_client'
6
6
 
7
7
  # You can add fixtures and/or initialization code here to make experimenting
8
8
  # with your gem easier. You can also use a different console, if you like.
data/exe/sdr CHANGED
@@ -3,40 +3,117 @@
3
3
 
4
4
  $LOAD_PATH.unshift 'lib'
5
5
  require 'optparse'
6
+ require 'sdr_client'
6
7
 
7
8
  options = {}
9
+
8
10
  global = OptionParser.new do |opts|
9
11
  opts.on('--service-url URL', 'Connect to the host at this URL') do |url|
10
12
  options[:url] = url
11
13
  end
12
14
  opts.on('-h', '--help', 'Display this screen') do
13
- puts opts
14
- exit
15
+ SdrClient::CLI.help
15
16
  end
16
17
  end
17
18
 
18
19
  global.order!
19
20
  command = ARGV.shift
20
21
 
21
- subcommands = {
22
- 'deposit' => OptionParser.new do |opts|
23
- opts.on('--label LABEL', 'The object label') do |label|
24
- options[:label] = label
25
- end
26
- opts.on('-h', '--help', 'Display this screen') do
27
- puts opts
28
- exit
22
+ deposit_options = OptionParser.new do |opts|
23
+ opts.banner = "Usage: sdr #{command} [options]"
24
+ opts.on('--label LABEL', 'The object label') do |label|
25
+ options[:label] = label
26
+ end
27
+
28
+ opts.on('--admin-policy ADMIN_POLICY', 'The druid identifier of the admin policy object') do |apo|
29
+ options[:apo] = apo
30
+ end
31
+
32
+ opts.on('--type TYPE', 'The object type to create. ' \
33
+ 'One of: "image", "book", "document", "map", "manuscript", "media", ' \
34
+ '"three_dimensional", "object", "collection", or "admin_policy"') do |type|
35
+ if %w[image book document map manuscript media three_dimensional object collection admin_policy].include?(type)
36
+ options[:type] = "http://cocina.sul.stanford.edu/models/#{type}.jsonld"
29
37
  end
30
38
  end
39
+
40
+ opts.on('--collection COLLECTION', 'The druid identifier of the collection object') do |collection|
41
+ options[:collection] = collection
42
+ end
43
+
44
+ opts.on('--catkey CATKEY', 'The catkey for this item') do |catkey|
45
+ options[:catkey] = catkey
46
+ end
47
+
48
+ opts.on('--source-id SOURCE_ID', 'The source id for this object') do |source_id|
49
+ options[:source_id] = source_id
50
+ end
51
+
52
+ opts.on('--copyright COPYRIGHT', 'The copyright statement') do |copyright|
53
+ options[:copyright] = copyright
54
+ end
55
+
56
+ opts.on('--use-statement STATEMENT', 'The use and reproduction statement') do |use_statement|
57
+ options[:use_statement] = use_statement
58
+ end
59
+
60
+ opts.on('--viewing-direction DIRECTION', 'The viewing direction (if a book). ' \
61
+ 'Either "left-to-right" or "right-to-left"') do |viewing_direction|
62
+ options[:viewing_direction] = viewing_direction if %w[left-to-right right-to-left].include?(viewing_direction)
63
+ end
64
+
65
+ opts.on('--access LEVEL', 'The access level for this object. ' \
66
+ 'Either "world", "stanford", "location-based", "citation-only" or "dark"') do |level|
67
+ options[:access] = level if %w[world stanford location-based citation-only dark].include?(level)
68
+ end
69
+
70
+ opts.on('--files-metadata FILES_METADATA', 'A JSON object representing per-file metadata') do |files_metadata|
71
+ options[:files_metadata] = JSON.parse(files_metadata)
72
+ end
73
+
74
+ opts.on('--strategy STRATEGY',
75
+ 'The strategy to use for distributing files into filesets. Either "default" or "filename"') do |strategy|
76
+ strategy_class = case strategy
77
+ when 'filename'
78
+ SdrClient::Deposit::MatchingFileGroupingStrategy
79
+ when 'default'
80
+ SdrClient::Deposit::SingleFileGroupingStrategy
81
+ else
82
+ warn "Unknown strategy #{strategy}"
83
+ exit(1)
84
+ end
85
+ options[:grouping_strategy] = strategy_class
86
+ end
87
+
88
+ opts.on('-h', '--help', 'Display this screen') do
89
+ puts opts
90
+ exit
91
+ end
92
+ end
93
+
94
+ SdrClient::CLI.help unless command
95
+
96
+ subcommands = {
97
+ 'deposit' => deposit_options,
98
+ 'register' => deposit_options,
99
+ 'login' => OptionParser.new
31
100
  }
32
101
 
33
102
  unless subcommands.key?(command)
34
103
  puts "unknown command '#{command}'"
35
- exit
104
+ SdrClient::CLI.help
36
105
  end
37
106
 
38
107
  subcommands[command].order!
39
108
 
40
- require 'sdr_client'
41
109
  options[:files] = ARGV unless ARGV.empty?
42
- SdrClient::CLI.start(command, options)
110
+ options[:url] ||= 'https://sdr-api-prod.stanford.edu'
111
+
112
+ begin
113
+ SdrClient::CLI.start(command, options)
114
+ rescue StandardError => e
115
+ warn "There was a problem making your request:\n\n"
116
+ warn e.message
117
+ puts
118
+ puts subcommands[command].help
119
+ end
data/lib/sdr-client.rb ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sdr_client'
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module SdrClient
6
+ # API calls around background job results from dor-services-app
7
+ module BackgroundJobResults
8
+ # Get status/result of a background job
9
+ # @param url [String] url for the service
10
+ # @param job_id [String] required string representing a job identifier
11
+ # @return [Hash] result of background job
12
+ def self.show(url:, job_id:)
13
+ connection = Connection.new(url: "#{url}/v1/background_job_results/#{job_id}").connection
14
+ resp = connection.get
15
+
16
+ raise "unexpected response: #{resp.status} #{resp.body}" unless resp.success?
17
+
18
+ JSON.parse(resp.body).with_indifferent_access
19
+ end
20
+ end
21
+ end
@@ -3,10 +3,84 @@
3
3
  module SdrClient
4
4
  # The command line interface
5
5
  module CLI
6
+ HELP = <<~HELP
7
+ DESCRIPTION:
8
+ The SDR Command Line Interface is a tool to interact with the Stanford Digital Repository.
9
+
10
+ SYNOPSIS:
11
+ sdr [options] <command>
12
+
13
+ To see help text for each command you can run:
14
+
15
+ sdr [options] <command> help
16
+
17
+ OPTIONS:
18
+ --service-url (string)
19
+ Override the command's default URL with the given URL.
20
+
21
+ -h, --help
22
+ Displays this screen
23
+
24
+
25
+ COMMANDS:
26
+ deposit
27
+ Accession an object into the SDR
28
+
29
+ register
30
+ Create a draft object in SDR and retrieve a Druid identifier.
31
+
32
+ login
33
+ Will prompt for email & password and exchange it for an login token, which it saves in ~/.sdr/token
34
+
35
+ HELP
36
+
6
37
  def self.start(command, options)
7
38
  case command
8
- when 'deposit'
9
- SdrClient::Deposit.run(options)
39
+ when 'deposit', 'register'
40
+ display_errors(validate_deposit_options(options))
41
+ job_id = SdrClient::Deposit.run(accession: command == 'deposit', **options)
42
+ poll_for_job_complete(job_id: job_id, url: options[:url]) # TODO: add an option that skips this
43
+ when 'login'
44
+ status = SdrClient::Login.run(options)
45
+ puts status.failure if status.failure?
46
+ else
47
+ raise "Unknown command #{command}"
48
+ end
49
+ rescue SdrClient::Credentials::NoCredentialsError
50
+ puts 'Log in first'
51
+ exit(1)
52
+ end
53
+
54
+ def self.display_errors(errors)
55
+ return if errors.empty?
56
+
57
+ raise errors.map { |k, v| "#{k} #{v}" }.join("\n")
58
+ end
59
+
60
+ def self.validate_deposit_options(options)
61
+ {}.tap do |errors|
62
+ errors['admin-policy'] = 'is a required argument' unless options[:apo]
63
+ errors['source-id'] = 'is a required argument' unless options[:source_id]
64
+ end
65
+ end
66
+
67
+ def self.help
68
+ puts HELP
69
+ exit
70
+ end
71
+
72
+ def self.poll_for_job_complete(job_id:, url:)
73
+ result = nil
74
+ 1.upto(5) do |_n|
75
+ result = SdrClient::BackgroundJobResults.show(url: url, job_id: job_id)
76
+ break unless %w[pending processing].include? result['status']
77
+
78
+ sleep 5
79
+ end
80
+ if result['status'] == 'complete'
81
+ puts result.dig('output', 'druid')
82
+ else
83
+ warn "Job #{job_id} did not complete\n#{result.inspect}"
10
84
  end
11
85
  end
12
86
  end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SdrClient
4
+ # The connection to the server
5
+ class Connection
6
+ include Dry::Monads[:result]
7
+
8
+ def initialize(url:, token: Credentials.read)
9
+ @url = url
10
+ @token = token
11
+ end
12
+
13
+ def connection
14
+ @connection ||= Faraday.new(url: url) do |conn|
15
+ conn.authorization :Bearer, token
16
+ conn.adapter :net_http
17
+ end
18
+ end
19
+
20
+ # This is only available to certain blessed accounts (argo) as it gives the
21
+ # token that allows you to act as any other user. Thus the caller must authenticate
22
+ # the user (e.g. using Shibboleth) before calling this method with their email address.
23
+ # @param [String] the email address of the person to proxy to.
24
+ # @return [Result] the token for the account
25
+ def proxy(to)
26
+ response = connection.post("/v1/auth/proxy?to=#{to}")
27
+ case response.status
28
+ when 200
29
+ Success(response.body)
30
+ else
31
+ Failure("Status: #{response.status}\n#{response.body}")
32
+ end
33
+ end
34
+
35
+ delegate :put, :post, to: :connection
36
+
37
+ private
38
+
39
+ attr_reader :url, :token
40
+ end
41
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SdrClient
4
+ # The stored credentials
5
+ class Credentials
6
+ class NoCredentialsError < StandardError; end
7
+
8
+ # @param [String] a json string that contains a field 'token'
9
+ def self.write(body)
10
+ json = JSON.parse(body)
11
+ Dir.mkdir(credentials_path, 0o700) unless Dir.exist?(credentials_path)
12
+ File.atomic_write(credentials_file) do |file|
13
+ file.write(json.fetch('token'))
14
+ end
15
+ File.chmod(0o600, credentials_file)
16
+ end
17
+
18
+ def self.read
19
+ raise NoCredentialsError unless ::File.exist?(credentials_file)
20
+
21
+ creds = IO.readlines(credentials_file, chomp: true).first if ::File.exist?(credentials_file)
22
+ raise NoCredentialsError if creds.nil?
23
+
24
+ creds
25
+ end
26
+
27
+ def self.credentials_path
28
+ @credentials_path ||= File.join(Dir.home, '.sdr')
29
+ end
30
+
31
+ def self.credentials_file
32
+ File.join(credentials_path, 'credentials')
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SdrClient
4
+ module Deposit
5
+ # This represents the File metadata that we send to the server for doing a deposit
6
+ class File
7
+ # rubocop:disable Metrics/ParameterLists
8
+ def initialize(external_identifier:, label:, filename:,
9
+ access: 'world', preserve: true, shelve: true,
10
+ mime_type: nil, md5: nil, sha1: nil, use: nil)
11
+ @external_identifier = external_identifier
12
+ @label = label
13
+ @filename = filename
14
+ @access = access
15
+ @preserve = preserve
16
+ @shelve = shelve
17
+ @mime_type = mime_type
18
+ @md5 = md5
19
+ @sha1 = sha1
20
+ @use = use
21
+ end
22
+ # rubocop:enable Metrics/ParameterLists
23
+
24
+ # rubocop:disable Metrics/MethodLength
25
+ def as_json
26
+ {
27
+ "type": 'http://cocina.sul.stanford.edu/models/file.jsonld',
28
+ label: @label,
29
+ filename: @filename,
30
+ externalIdentifier: @external_identifier,
31
+ access: {
32
+ access: @access
33
+ },
34
+ administrative: {
35
+ sdrPreserve: @preserve,
36
+ shelve: @shelve
37
+ },
38
+ version: 1,
39
+ hasMessageDigests: message_digests
40
+ }.tap do |json|
41
+ json['hasMimeType'] = @mime_type if @mime_type
42
+ json['use'] = @use if @use
43
+ end
44
+ end
45
+ # rubocop:enable Metrics/MethodLength
46
+
47
+ private
48
+
49
+ def message_digests
50
+ @message_digests ||= [].tap do |message_digests|
51
+ message_digests << create_message_digest('md5', @md5) unless @md5.nil?
52
+ message_digests << create_message_digest('sha1', @sha1) unless @sha1.nil?
53
+ end
54
+ end
55
+
56
+ def create_message_digest(algorithm, digest)
57
+ {
58
+ "type": algorithm,
59
+ digest: digest
60
+ }
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sdr_client/deposit/file_metadata_builder_operations/mime_type'
4
+ require 'sdr_client/deposit/file_metadata_builder_operations/md5'
5
+ require 'sdr_client/deposit/file_metadata_builder_operations/sha1'
6
+
7
+ module SdrClient
8
+ module Deposit
9
+ # Build basic metadata for files, iterating over a series of operations
10
+ # The available options are here: https://github.com/sul-dlss/sdr-client/blob/v0.8.1/lib/sdr_client/deposit/file.rb#L8-L10
11
+ class FileMetadataBuilder
12
+ OPERATIONS = [
13
+ FileMetadataBuilderOperations::MimeType,
14
+ FileMetadataBuilderOperations::MD5,
15
+ FileMetadataBuilderOperations::SHA1
16
+ ].freeze
17
+ private_constant :OPERATIONS
18
+
19
+ # @param (see #initialize)
20
+ # @return (see #build)
21
+ def self.build(files:, files_metadata:)
22
+ new(files: files, files_metadata: files_metadata.dup).build
23
+ end
24
+
25
+ # @param [Array<String>] files the list of files for which to generate metadata
26
+ def initialize(files:, files_metadata:)
27
+ @files = files
28
+ @files_metadata = files_metadata
29
+ end
30
+
31
+ # @return [Hash<String, Hash<String, String>>]
32
+ def build
33
+ files.each do |file_path|
34
+ file_key = ::File.basename(file_path)
35
+ OPERATIONS.each do |operation|
36
+ result = operation.for(file_path: file_path)
37
+ next if result.nil?
38
+
39
+ files_metadata[file_key] ||= {}
40
+ files_metadata[file_key][operation::NAME] = result
41
+ end
42
+ end
43
+ files_metadata
44
+ end
45
+
46
+ private
47
+
48
+ attr_reader :files, :files_metadata
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'digest'
4
+
5
+ module SdrClient
6
+ module Deposit
7
+ module FileMetadataBuilderOperations
8
+ # MD5 for this file.
9
+ class MD5
10
+ NAME = 'md5'
11
+ def self.for(file_path:, **)
12
+ Digest::MD5.file(file_path).hexdigest
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'shellwords'
4
+
5
+ module SdrClient
6
+ module Deposit
7
+ module FileMetadataBuilderOperations
8
+ # Mime-type for this file.
9
+ class MimeType
10
+ NAME = 'mime_type'
11
+ def self.for(file_path:, **)
12
+ argv = Shellwords.escape(file_path)
13
+ `file --mime-type -b #{argv}`.chomp
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'digest'
4
+
5
+ module SdrClient
6
+ module Deposit
7
+ module FileMetadataBuilderOperations
8
+ # SHA1 for this file.
9
+ class SHA1
10
+ NAME = 'sha1'
11
+ def self.for(file_path:, **)
12
+ Digest::SHA1.file(file_path).hexdigest
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end