valkyrie-shrine 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 43acefce16f29dafdd2627b82dbb79baa25e11e8b2503f33b1a49b8e4684fa81
4
+ data.tar.gz: 8cdd2a2fb849326f7945f9a4c20cf064c755d4bb1aac5371119d3a9db90ac9e3
5
+ SHA512:
6
+ metadata.gz: f7b3a2a459b419e0e046be241b1e5d32b063de7704e211672a7823430d16c47d22cf72af8f2edc43c6023d14f49b60dff781144486e2d06ef9be637ebc02b557
7
+ data.tar.gz: 4768279b6f1ef8b1872cefd71e0114397a2a9029d5978dc196ce43acdb97ff4dfdbdda062f5854ed92dbeb553e182564ed91b22ca60af225e7c04a88bd725d4d
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,23 @@
1
+ inherit_gem:
2
+ bixby: bixby_default.yml
3
+ AllCops:
4
+ DisplayCopNames: true
5
+ TargetRubyVersion: 2.3
6
+ Exclude:
7
+ - 'bin/*'
8
+ - 'vendor/**/*'
9
+ RSpec/ScatteredSetup:
10
+ Enabled: false
11
+ Metrics/BlockLength:
12
+ Exclude:
13
+ - 'spec/**/*'
14
+ - 'tasks/**/*'
15
+ - 'db/schema.rb'
16
+ - 'Rakefile'
17
+ - '*.gemspec'
18
+ RSpec/ExampleLength:
19
+ Enabled: false
20
+ RSpec/MultipleExpectations:
21
+ Enabled: false
22
+ Lint/UnusedMethodArgument:
23
+ AllowUnusedKeywordArguments: true
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in valkyrie-shrine.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,15 @@
1
+ ##########################################################################
2
+ # Copyright 2019 Northwestern University Library
3
+ # Additional copyright may be held by others, as reflected in the commit log
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # Valkyrie::Shrine
2
+
3
+ [Shrine](http://shrinerb.com/) storage adapter for [Valkyrie](https://github.com/samvera-labs/valkyrie).
4
+
5
+ [![CircleCI](https://circleci.com/gh/samvera-labs/valkyrie-shrine.svg?style=svg)](https://circleci.com/gh/samvera-labs/valkyrie-shrine)
6
+ [![Coverage Status](https://coveralls.io/repos/github/samvera-labs/valkyrie-shrine/badge.svg?branch=master)](https://coveralls.io/github/samvera-labs/valkyrie-shrine?branch=master)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'valkyrie-shrine'
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ Follow the Valkyrie [README](https://github.com/samvera-labs/valkyrie) to get a development or production environment up and running. To enable Shrine support, add the following to your application's config/initializers/valkyrie.rb:
19
+
20
+ ```ruby
21
+ # config/initializers/valkyrie.rb
22
+ require 'shrine/storage/s3'
23
+ require 'shrine/storage/file_system'
24
+ require 'valkyrie/storage/shrine/s3_checksum'
25
+ require 'valkyrie/storage/shrine/file_system_checksum'
26
+
27
+ Shrine.storages = {
28
+ file: Shrine::Storage::FileSystem.new("public", prefix: "uploads"),
29
+ s3: Shrine::Storage::S3.new(bucket: 'donut-uploads', prefix: 'cache')
30
+ }
31
+
32
+ Valkyrie::StorageAdapter.register(
33
+ Valkyrie::Storage::Shrine.new(Shrine.storages[:s3]), :s3
34
+ )
35
+
36
+ Valkyrie::StorageAdapter.register(
37
+ Valkyrie::Storage::Shrine.new(Shrine.storages[:file]), :disk
38
+ )
39
+ ```
40
+
41
+ ## Development
42
+
43
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
44
+
45
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
46
+
47
+ ## Contributing
48
+
49
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/valkyrie-shrine.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "valkyrie/shrine"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/circle.yml ADDED
@@ -0,0 +1,29 @@
1
+ version: 2
2
+ jobs:
3
+ build:
4
+ docker:
5
+ - image: circleci/ruby:2.6.1
6
+ environment:
7
+ BUNDLER_VERSION: 2.0.1
8
+ steps:
9
+ - checkout
10
+ - run:
11
+ name: Install Bundler 2.0.1
12
+ command: gem install --no-doc bundler:2.0.1
13
+ - restore_cache:
14
+ keys:
15
+ - bundle-{{ checksum "Gemfile" }}-{{ checksum "valkyrie-shrine.gemspec" }}
16
+ - bundle- # used if checksum fails
17
+ - run:
18
+ name: Install dependencies
19
+ command: bundle check --path=vendor/bundle || bundle install --path=vendor/bundle --jobs 4 --retry 3
20
+ - save_cache:
21
+ key: bundle-{{ checksum "Gemfile" }}-{{ checksum "valkyrie-shrine.gemspec" }}
22
+ paths:
23
+ - "vendor/bundle"
24
+ - run:
25
+ name: Run Rubocop
26
+ command: bundle exec rubocop
27
+ - run:
28
+ name: Run Specs
29
+ command: bundle exec rspec spec
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'valkyrie/shrine/version'
4
+ require 'valkyrie/storage/shrine'
5
+ require 'valkyrie/shrine/checksum/base'
6
+ require 'valkyrie/shrine/checksum/file_system'
7
+ require 'valkyrie/shrine/checksum/s3'
8
+
9
+ module Valkyrie
10
+ module Shrine
11
+ class Valkyrie::Shrine::IntegrityError < StandardError; end
12
+ end
13
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal:true
2
+
3
+ module Valkyrie
4
+ module Shrine
5
+ module Checksum
6
+ class Base
7
+ def self.digest_class
8
+ Digest::SHA256
9
+ end
10
+
11
+ def digest_class
12
+ self.class.digest_class
13
+ end
14
+
15
+ def verify_checksum(io, result)
16
+ checksum_for(io) == calculate_checksum(result)
17
+ end
18
+
19
+ def checksum_for(io)
20
+ simple_digest(io)
21
+ end
22
+
23
+ def simple_digest(io)
24
+ digest_class.new.tap do |result|
25
+ io.rewind
26
+ while (chunk = io.read(16 * 1024))
27
+ result << chunk
28
+ end
29
+ end
30
+ end
31
+
32
+ # Subclass is expected to implement #calculate_checksum
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal:true
2
+
3
+ module Valkyrie
4
+ module Shrine
5
+ module Checksum
6
+ class FileSystem < Base
7
+ def calculate_checksum(result)
8
+ checksum_for(result.io)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal:true
2
+
3
+ module Valkyrie
4
+ module Shrine
5
+ module Checksum
6
+ class S3 < Base
7
+ attr_reader :threshold, :part_size
8
+
9
+ def self.digest_class
10
+ Digest::MD5
11
+ end
12
+
13
+ def initialize(threshold: 15 * 1024 * 1024, part_size: 5 * 1024 * 1024)
14
+ @threshold = threshold
15
+ @part_size = part_size
16
+ end
17
+
18
+ def calculate_checksum(result)
19
+ result.io.data[:object].data.etag.delete('"')
20
+ end
21
+
22
+ def checksum_for(io)
23
+ io.rewind
24
+ io.size < threshold ? simple_digest(io) : multipart_checksum(io)
25
+ end
26
+
27
+ private
28
+
29
+ def multipart_checksum(io)
30
+ parts = 0
31
+ checksums = []
32
+ while (chunk = io.read(part_size))
33
+ parts += 1
34
+ checksums << digest_class.digest(chunk)
35
+ end
36
+ [digest_class.hexdigest(checksums.join('')), parts.to_s].join('-')
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Valkyrie
4
+ module Shrine
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'shrine'
4
+ require 'digest/md5'
5
+
6
+ module Valkyrie
7
+ module Storage
8
+ class Shrine
9
+ PROTOCOL = 'shrine://'
10
+
11
+ attr_reader :shrine, :verifier
12
+
13
+ def initialize(shrine_storage, verifier = nil)
14
+ @shrine = shrine_storage
15
+ if verifier.nil?
16
+ try_to_find_verifier
17
+ else
18
+ @verifier = verifier
19
+ end
20
+ end
21
+
22
+ # Upload a file via the Shrine storage upload implementation
23
+ # @param file [IO]
24
+ # @param original_filename [String]
25
+ # @param resource [Valkyrie::Resource]
26
+ # @return [Valkyrie::StorageAdapter::StreamFile]
27
+ # @raise Valkyrie::Shrine::IntegrityError if #verify_checksum is defined
28
+ # on the shrine object and the file and result digests do not match
29
+ def upload(file:, original_filename:, resource:, **upload_options)
30
+ identifier = resource.id.to_s
31
+ shrine.upload(file, identifier, **upload_options)
32
+ find_by(id: "#{PROTOCOL}#{identifier}").tap do |result|
33
+ if verifier
34
+ raise Valkyrie::Shrine::IntegrityError unless verifier.verify_checksum(file, result)
35
+ end
36
+ end
37
+ end
38
+
39
+ # Return the file associated with the given identifier
40
+ # @param id [Valkyrie::ID]
41
+ # @return [Valkyrie::StorageAdapter::StreamFile]
42
+ # @raise Valkyrie::StorageAdapter::FileNotFound if nothing is found
43
+ def find_by(id:)
44
+ Valkyrie::StorageAdapter::StreamFile.new(id: Valkyrie::ID.new(id.to_s), io: shrine.open(shrine_id_for(id)))
45
+ rescue Aws::S3::Errors::NoSuchKey
46
+ raise Valkyrie::StorageAdapter::FileNotFound
47
+ end
48
+
49
+ # @param id [Valkyrie::ID]
50
+ # @return [Boolean] true if this adapter can handle this type of identifier
51
+ def handles?(id:)
52
+ id.to_s.start_with?(PROTOCOL)
53
+ end
54
+
55
+ # Delete the file in S3 associated with the given identifier.
56
+ # @param id [Valkyrie::ID]
57
+ def delete(id:)
58
+ shrine.delete(shrine_id_for(id))
59
+ end
60
+
61
+ private
62
+
63
+ def try_to_find_verifier
64
+ class_const = shrine.class.name.split(/::/).last.to_sym
65
+ @verifier = Valkyrie::Shrine::Checksum.const_get(class_const).new if Valkyrie::Shrine::Checksum.const_defined?(class_const)
66
+ end
67
+
68
+ def shrine_id_for(id)
69
+ id.to_s.sub(/^#{PROTOCOL}/, '')
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'valkyrie/shrine/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'valkyrie-shrine'
9
+ spec.version = Valkyrie::Shrine::VERSION
10
+ spec.authors = ['Brendan Quinn']
11
+ spec.email = ['brendan-quinn@northwestern.edu']
12
+
13
+ spec.summary = 'Shrine storage adapter for Valkyrie'
14
+ spec.homepage = 'https://github.com/samvera-labs/valkyrie-shrine'
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'aws-sdk-s3', '~> 1'
26
+ spec.add_dependency 'shrine', '~> 2.0'
27
+ spec.add_dependency 'valkyrie', '~> 2.0.0.RC3'
28
+
29
+ spec.add_development_dependency 'bixby', '~> 2.0.0.pre.beta1'
30
+ spec.add_development_dependency 'bundler', '~> 2.0'
31
+ spec.add_development_dependency 'coveralls'
32
+ spec.add_development_dependency 'pry'
33
+ spec.add_development_dependency 'pry-byebug'
34
+ spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'rspec', '~> 3.0'
36
+ spec.add_development_dependency 'simplecov'
37
+ end
metadata ADDED
@@ -0,0 +1,214 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: valkyrie-shrine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brendan Quinn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-03-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-s3
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: shrine
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: valkyrie
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.0.0.RC3
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.0.0.RC3
55
+ - !ruby/object:Gem::Dependency
56
+ name: bixby
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.0.0.pre.beta1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.0.0.pre.beta1
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: coveralls
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry-byebug
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rake
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '10.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '10.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '3.0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '3.0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: simplecov
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description:
168
+ email:
169
+ - brendan-quinn@northwestern.edu
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - ".gitignore"
175
+ - ".rspec"
176
+ - ".rubocop.yml"
177
+ - ".travis.yml"
178
+ - Gemfile
179
+ - LICENSE
180
+ - README.md
181
+ - Rakefile
182
+ - bin/console
183
+ - bin/setup
184
+ - circle.yml
185
+ - lib/valkyrie/shrine.rb
186
+ - lib/valkyrie/shrine/checksum/base.rb
187
+ - lib/valkyrie/shrine/checksum/file_system.rb
188
+ - lib/valkyrie/shrine/checksum/s3.rb
189
+ - lib/valkyrie/shrine/version.rb
190
+ - lib/valkyrie/storage/shrine.rb
191
+ - valkyrie-shrine.gemspec
192
+ homepage: https://github.com/samvera-labs/valkyrie-shrine
193
+ licenses: []
194
+ metadata: {}
195
+ post_install_message:
196
+ rdoc_options: []
197
+ require_paths:
198
+ - lib
199
+ required_ruby_version: !ruby/object:Gem::Requirement
200
+ requirements:
201
+ - - ">="
202
+ - !ruby/object:Gem::Version
203
+ version: '0'
204
+ required_rubygems_version: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ requirements: []
210
+ rubygems_version: 3.0.2
211
+ signing_key:
212
+ specification_version: 4
213
+ summary: Shrine storage adapter for Valkyrie
214
+ test_files: []