hydra-works 0.15.0 → 0.16.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +12 -0
- data/.rubocop_todo.yml +105 -0
- data/.travis.yml +4 -1
- data/CONTRIBUTING.md +63 -19
- data/Gemfile +1 -1
- data/lib/hydra/works.rb +1 -0
- data/lib/hydra/works/services/add_external_file_to_file_set.rb +108 -0
- data/lib/hydra/works/services/add_file_to_file_set.rb +2 -1
- data/lib/hydra/works/services/characterization_service.rb +2 -1
- data/lib/hydra/works/services/virus_checker_service.rb +14 -2
- data/lib/hydra/works/version.rb +1 -1
- data/spec/hydra/works/services/add_external_file_to_file_set_spec.rb +61 -0
- data/spec/hydra/works/services/characterization_service_spec.rb +1 -1
- data/spec/hydra/works/services/virus_checker_service_spec.rb +45 -6
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c1d6b6f3987a8df560123eee26e60f231fad1e8a
|
4
|
+
data.tar.gz: fefe3540d414cf554eea479f2de2249ee0417071
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8eb1fb9eae7a8dd692008ccc0c30c2707f694717e2d0d59e190afce5466557b25a26692e63be6dca281a083d96603d1a66c59d7503d0b3b17bdf35e2c159728f
|
7
|
+
data.tar.gz: 60c735f5169ae1f41a9b8a33013147961b830d536e4c41fd4f3b28b76709a731d192983966798276ca30ec44612a67b8bcb214f6712ae93251e60ad5fd22fcd4
|
data/.rubocop.yml
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require: rubocop-rspec
|
2
|
+
inherit_from: .rubocop_todo.yml
|
2
3
|
|
3
4
|
AllCops:
|
4
5
|
DisplayCopNames: true
|
@@ -9,6 +10,17 @@ AllCops:
|
|
9
10
|
- 'spec/internal/bin/*'
|
10
11
|
- 'spec/internal/db/schema.rb'
|
11
12
|
|
13
|
+
Metrics/BlockLength:
|
14
|
+
Exclude:
|
15
|
+
- 'spec/**/*.rb'
|
16
|
+
|
17
|
+
# By default RSpec/MessageSpies has the following:
|
18
|
+
# Prefer have_received for setting message expectations. Setup form as a spy using allow or instance_spy.
|
19
|
+
# The default assumes EnforcedStyle is 'have_received'. Most of our specs are 'receive'
|
20
|
+
RSpec/MessageSpies:
|
21
|
+
Enabled: true
|
22
|
+
EnforcedStyle: receive
|
23
|
+
|
12
24
|
Metrics/LineLength:
|
13
25
|
Enabled: false
|
14
26
|
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2017-01-24 14:50:50 -0500 using RuboCop version 0.47.1.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 2
|
10
|
+
# Configuration parameters: CountComments, ExcludedMethods.
|
11
|
+
Metrics/BlockLength:
|
12
|
+
Max: 137
|
13
|
+
|
14
|
+
# Offense count: 1
|
15
|
+
RSpec/BeEql:
|
16
|
+
Exclude:
|
17
|
+
- 'spec/hydra/works/services/upload_file_spec.rb'
|
18
|
+
|
19
|
+
# Offense count: 5
|
20
|
+
# Configuration parameters: Max.
|
21
|
+
RSpec/ExampleLength:
|
22
|
+
Exclude:
|
23
|
+
- 'spec/hydra/works/models/work_spec.rb'
|
24
|
+
- 'spec/hydra/works/services/characterization_service_spec.rb'
|
25
|
+
|
26
|
+
# Offense count: 8
|
27
|
+
RSpec/LeadingSubject:
|
28
|
+
Exclude:
|
29
|
+
- 'spec/hydra/works/characterization_spec.rb'
|
30
|
+
- 'spec/hydra/works/models/file_set_spec.rb'
|
31
|
+
- 'spec/hydra/works/services/add_file_to_file_set_spec.rb'
|
32
|
+
- 'spec/hydra/works/services/determine_mime_type_spec.rb'
|
33
|
+
- 'spec/hydra/works/services/determine_original_name_spec.rb'
|
34
|
+
- 'spec/hydra/works/virus_scanner_spec.rb'
|
35
|
+
|
36
|
+
# Offense count: 1
|
37
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
38
|
+
# SupportedStyles: have_received, receive
|
39
|
+
RSpec/MessageSpies:
|
40
|
+
Exclude:
|
41
|
+
- 'spec/hydra/works/services/virus_checker_service_spec.rb'
|
42
|
+
|
43
|
+
# Offense count: 52
|
44
|
+
RSpec/MultipleExpectations:
|
45
|
+
Max: 8
|
46
|
+
|
47
|
+
# Offense count: 119
|
48
|
+
RSpec/NamedSubject:
|
49
|
+
Exclude:
|
50
|
+
- 'spec/hydra/works/models/collection_spec.rb'
|
51
|
+
- 'spec/hydra/works/models/concerns/file_set/contained_files_spec.rb'
|
52
|
+
- 'spec/hydra/works/models/concerns/file_set/mime_types_spec.rb'
|
53
|
+
- 'spec/hydra/works/models/concerns/file_set/versioned_content_spec.rb'
|
54
|
+
- 'spec/hydra/works/models/concerns/file_set/virus_check_spec.rb'
|
55
|
+
- 'spec/hydra/works/models/concerns/file_set_behavior_spec.rb'
|
56
|
+
- 'spec/hydra/works/models/file_set_spec.rb'
|
57
|
+
- 'spec/hydra/works/models/work_spec.rb'
|
58
|
+
- 'spec/hydra/works/services/add_file_to_file_set_spec.rb'
|
59
|
+
- 'spec/hydra/works/services/upload_file_spec.rb'
|
60
|
+
|
61
|
+
# Offense count: 8
|
62
|
+
# Configuration parameters: Max.
|
63
|
+
RSpec/NestedGroups:
|
64
|
+
Exclude:
|
65
|
+
- 'spec/hydra/works/models/collection_spec.rb'
|
66
|
+
- 'spec/hydra/works/models/work_spec.rb'
|
67
|
+
- 'spec/hydra/works/services/characterization_service_spec.rb'
|
68
|
+
- 'spec/hydra/works/services/virus_checker_service_spec.rb'
|
69
|
+
|
70
|
+
# Offense count: 6
|
71
|
+
RSpec/SubjectStub:
|
72
|
+
Exclude:
|
73
|
+
- 'spec/hydra/works/models/concerns/file_set/mime_types_spec.rb'
|
74
|
+
- 'spec/hydra/works/models/concerns/file_set/virus_check_spec.rb'
|
75
|
+
|
76
|
+
# Offense count: 1
|
77
|
+
# Configuration parameters: IgnoreSymbolicNames.
|
78
|
+
RSpec/VerifiedDoubles:
|
79
|
+
Exclude:
|
80
|
+
- 'spec/hydra/works/services/upload_file_spec.rb'
|
81
|
+
|
82
|
+
# Offense count: 3
|
83
|
+
# Configuration parameters: MinBodyLength.
|
84
|
+
Style/GuardClause:
|
85
|
+
Exclude:
|
86
|
+
- 'lib/hydra/works/not_collection_validator.rb'
|
87
|
+
- 'lib/hydra/works/not_file_set_validator.rb'
|
88
|
+
- 'lib/hydra/works/services/add_file_to_file_set.rb'
|
89
|
+
|
90
|
+
# Offense count: 1
|
91
|
+
# Cop supports --auto-correct.
|
92
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
93
|
+
# SupportedStyles: symmetrical, new_line, same_line
|
94
|
+
Style/MultilineMethodCallBraceLayout:
|
95
|
+
Exclude:
|
96
|
+
- 'spec/spec_helper.rb'
|
97
|
+
|
98
|
+
# Offense count: 1
|
99
|
+
# Cop supports --auto-correct.
|
100
|
+
# Configuration parameters: AutoCorrect, EnforcedStyle, SupportedStyles.
|
101
|
+
# SupportedStyles: predicate, comparison
|
102
|
+
Style/NumericPredicate:
|
103
|
+
Exclude:
|
104
|
+
- 'spec/**/*'
|
105
|
+
- 'lib/hydra/works/virus_scanner.rb'
|
data/.travis.yml
CHANGED
data/CONTRIBUTING.md
CHANGED
@@ -3,6 +3,13 @@
|
|
3
3
|
We want your help to make Project Hydra great.
|
4
4
|
There are a few guidelines that we need contributors to follow so that we can have a chance of keeping on top of things.
|
5
5
|
|
6
|
+
## Code of Conduct
|
7
|
+
|
8
|
+
The Hydra community is dedicated to providing a welcoming and positive experience for all its
|
9
|
+
members, whether they are at a formal gathering, in a social setting, or taking part in activities
|
10
|
+
online. Please see our [Code of Conduct](https://wiki.duraspace.org/display/hydra/Code+of+Conduct)
|
11
|
+
for more information.
|
12
|
+
|
6
13
|
## Hydra Project Intellectual Property Licensing and Ownership
|
7
14
|
|
8
15
|
All code contributors must have an Individual Contributor License Agreement (iCLA) on file with the Hydra Project Steering Group.
|
@@ -16,8 +23,10 @@ You should also add yourself to the `CONTRIBUTORS.md` file in the root of the pr
|
|
16
23
|
|
17
24
|
* Reporting Issues
|
18
25
|
* Making Changes
|
26
|
+
* Documenting Code
|
27
|
+
* Committing Changes
|
19
28
|
* Submitting Changes
|
20
|
-
* Merging Changes
|
29
|
+
* Reviewing and Merging Changes
|
21
30
|
|
22
31
|
### Reporting Issues
|
23
32
|
|
@@ -38,8 +47,28 @@ You should also add yourself to the `CONTRIBUTORS.md` file in the root of the pr
|
|
38
47
|
* Then checkout the new branch with `git checkout fix/master/my_contribution`.
|
39
48
|
* Please avoid working directly on the `master` branch.
|
40
49
|
* You may find the [hub suite of commands](https://github.com/defunkt/hub) helpful
|
50
|
+
* Make sure you have added sufficient tests and documentation for your changes.
|
51
|
+
* Test functionality with RSpec; est features / UI with Capybara.
|
52
|
+
* Run _all_ the tests to assure nothing else was accidentally broken.
|
53
|
+
|
54
|
+
### Documenting Code
|
55
|
+
|
56
|
+
* All new public methods, modules, and classes should include inline documentation in [YARD](http://yardoc.org/).
|
57
|
+
* Documentation should seek to answer the question "why does this code exist?"
|
58
|
+
* Document private / protected methods as desired.
|
59
|
+
* If you are working in a file with no prior documentation, do try to document as you gain understanding of the code.
|
60
|
+
* If you don't know exactly what a bit of code does, it is extra likely that it needs to be documented. Take a stab at it and ask for feedback in your pull request. You can use the 'blame' button on GitHub to identify the original developer of the code and @mention them in your comment.
|
61
|
+
* This work greatly increases the usability of the code base and supports the on-ramping of new committers.
|
62
|
+
* We will all be understanding of one another's time constraints in this area.
|
63
|
+
* YARD examples:
|
64
|
+
* [Hydra::Works::RemoveGenericFile](https://github.com/projecthydra-labs/hydra-works/blob/master/lib/hydra/works/services/generic_work/remove_generic_file.rb)
|
65
|
+
* [ActiveTriples::LocalName::Minter](https://github.com/ActiveTriples/active_triples-local_name/blob/master/lib/active_triples/local_name/minter.rb)
|
66
|
+
* [Getting started with YARD](http://www.rubydoc.info/gems/yard/file/docs/GettingStarted.md)
|
67
|
+
|
68
|
+
### Committing changes
|
69
|
+
|
41
70
|
* Make commits of logical units.
|
42
|
-
* Your commit should include a high level description of your work in HISTORY.textile
|
71
|
+
* Your commit should include a high level description of your work in HISTORY.textile
|
43
72
|
* Check for unnecessary whitespace with `git diff --check` before committing.
|
44
73
|
* Make sure your commit messages are [well formed](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html).
|
45
74
|
* If you created an issue, you can close it by including "Closes #issue" in your commit message. See [Github's blog post for more details](https://github.com/blog/1386-closing-issues-via-commit-messages)
|
@@ -60,7 +89,9 @@ You should also add yourself to the `CONTRIBUTORS.md` file in the root of the pr
|
|
60
89
|
|
61
90
|
class PostsController
|
62
91
|
def index
|
63
|
-
|
92
|
+
respond_to do |wants|
|
93
|
+
wants.html { render 'index' }
|
94
|
+
end
|
64
95
|
end
|
65
96
|
end
|
66
97
|
|
@@ -72,10 +103,6 @@ You should also add yourself to the `CONTRIBUTORS.md` file in the root of the pr
|
|
72
103
|
long to fit in 72 characters
|
73
104
|
```
|
74
105
|
|
75
|
-
* Make sure you have added the necessary tests for your changes.
|
76
|
-
* Run _all_ the tests to assure nothing else was accidentally broken.
|
77
|
-
* When you are ready to submit a pull request
|
78
|
-
|
79
106
|
### Submitting Changes
|
80
107
|
|
81
108
|
* Read the article ["Using Pull Requests"](https://help.github.com/articles/using-pull-requests) on GitHub.
|
@@ -84,28 +111,45 @@ You should also add yourself to the `CONTRIBUTORS.md` file in the root of the pr
|
|
84
111
|
* `git pull --rebase`
|
85
112
|
* `git checkout <your-branch>`
|
86
113
|
* `git rebase master`
|
87
|
-
* It is
|
114
|
+
* It is a good idea to run your tests again.
|
88
115
|
* If you've made more than one commit take a moment to consider whether squashing commits together would help improve their logical grouping.
|
89
116
|
* [Detailed Walkthrough of One Pull Request per Commit](http://ndlib.github.io/practices/one-commit-per-pull-request/)
|
90
|
-
* `git rebase --interactive
|
91
|
-
* To determine the number of commits on your branch: `git log master..<your-branch> --oneline | wc -l`
|
117
|
+
* `git rebase --interactive master` ([See Github help](https://help.github.com/articles/interactive-rebase))
|
92
118
|
* Squashing your branch's changes into one commit is "good form" and helps the person merging your request to see everything that is going on.
|
93
119
|
* Push your changes to a topic branch in your fork of the repository.
|
94
120
|
* Submit a pull request from your fork to the project.
|
95
121
|
|
96
|
-
### Merging Changes
|
122
|
+
### Reviewing and Merging Changes
|
123
|
+
|
124
|
+
We adopted [Github's Pull Request Review](https://help.github.com/articles/about-pull-request-reviews/) for our repositories.
|
125
|
+
Common checks that may occur in our repositories:
|
126
|
+
|
127
|
+
1. Travis CI - where our automated tests are running
|
128
|
+
2. Hound CI - where we check for style violations
|
129
|
+
3. Approval Required - Github enforces at least one person approve a pull request. Also, all reviewers that have chimed in must approve.
|
130
|
+
4. CodeClimate - is our code remaining healthy (at least according to static code analysis)
|
131
|
+
|
132
|
+
If one or more of the required checks failed (or are incomplete), the code should not be merged (and the UI will not allow it). If all of the checks have passed, then anyone on the project (including the pull request submitter) may merge the code.
|
133
|
+
|
134
|
+
*Example: Carolyn submits a pull request, Justin reviews the pull request and approves. However, Justin is still waiting on other checks (Travis CI is usually the culprit), so he does not merge the pull request. Eventually, all of the checks pass. At this point, Carolyn or anyone else may merge the pull request.*
|
135
|
+
|
136
|
+
#### Things to Consider When Reviewing
|
137
|
+
|
138
|
+
First, the person contributing the code is putting themselves out there. Be mindful of what you say in a review.
|
139
|
+
|
140
|
+
* Ask clarifying questions
|
141
|
+
* State your understanding and expectations
|
142
|
+
* Provide example code or alternate solutions, and explain why
|
143
|
+
|
144
|
+
This is your chance for a mentoring moment of another developer. Take time to give an honest and thorough review of what has changed. Things to consider:
|
97
145
|
|
98
|
-
* It is considered "poor from" to merge your own request.
|
99
|
-
* Please take the time to review the changes and get a sense of what is being changed. Things to consider:
|
100
146
|
* Does the commit message explain what is going on?
|
101
|
-
* Does the code changes have tests? _Not all changes need new tests, some changes are
|
147
|
+
* Does the code changes have tests? _Not all changes need new tests, some changes are refactors_
|
148
|
+
* Do new or changed methods, modules, and classes have documentation?
|
102
149
|
* Does the commit contain more than it should? Are two separate concerns being addressed in one commit?
|
103
|
-
*
|
104
|
-
* If you are uncertain, bring other contributors into the conversation by creating a comment that includes their @username.
|
105
|
-
* If you like the pull request, but want others to chime in, create a +1 comment and tag a user.
|
150
|
+
* Does the description of the new/changed specs match your understanding of what the spec is doing?
|
106
151
|
|
107
|
-
|
108
|
-
A great way to get new developers involved and to learn the process is to have them go through the Hierarchy of Promises and create a pull request to improve it.
|
152
|
+
If you are uncertain, bring other contributors into the conversation by assigning them as a reviewer.
|
109
153
|
|
110
154
|
# Additional Resources
|
111
155
|
|
data/Gemfile
CHANGED
@@ -6,7 +6,7 @@ gemspec
|
|
6
6
|
gem 'slop', '~> 3.6' # For byebug
|
7
7
|
|
8
8
|
group :development, :test do
|
9
|
-
gem 'rubocop', '~> 0.
|
9
|
+
gem 'rubocop', '~> 0.47.0', require: false
|
10
10
|
gem 'rubocop-rspec', require: false
|
11
11
|
gem 'pry' unless ENV['CI']
|
12
12
|
gem 'pry-byebug' unless ENV['CI']
|
data/lib/hydra/works.rb
CHANGED
@@ -0,0 +1,108 @@
|
|
1
|
+
module Hydra::Works
|
2
|
+
class AddExternalFileToFileSet
|
3
|
+
# Adds a file to the file_set
|
4
|
+
# @param [Hydra::PCDM::FileSet] file_set the file will be added to
|
5
|
+
# @param [String] external_file_url URL representing the externally stored file
|
6
|
+
# @param [RDF::URI or String] type URI for the RDF.type that identifies the file's role within the file_set
|
7
|
+
# @param [Hash] opts Options
|
8
|
+
# @option opts [Boolean] :update_existing When set to true, performs a create_or_update. When set to false, always creates a new file within file_set.files.
|
9
|
+
# @option opts [Boolean] :versioning Whether to create new version entries (only applicable if +type+ corresponds to a versionable file)
|
10
|
+
# @option opts [String] :filename A string for the original file name. Defaults to the same value as external_file_url
|
11
|
+
def self.call(file_set, external_file_url, type, opts = {})
|
12
|
+
fail ArgumentError, 'supplied object must be a file set' unless file_set.file_set?
|
13
|
+
|
14
|
+
update_existing = opts.fetch(:update_existing, true)
|
15
|
+
versioning = opts.fetch(:versioning, true)
|
16
|
+
filename = opts.fetch(:filename, external_file_url)
|
17
|
+
|
18
|
+
# TODO: required as a workaround for https://github.com/projecthydra/active_fedora/pull/858
|
19
|
+
file_set.save unless file_set.persisted?
|
20
|
+
|
21
|
+
updater_class = versioning ? VersioningUpdater : Updater
|
22
|
+
updater = updater_class.new(file_set, type, update_existing)
|
23
|
+
status = updater.update(external_file_url, filename)
|
24
|
+
status ? file_set : false
|
25
|
+
end
|
26
|
+
|
27
|
+
class Updater
|
28
|
+
attr_reader :file_set, :current_file
|
29
|
+
|
30
|
+
def initialize(file_set, type, update_existing)
|
31
|
+
@file_set = file_set
|
32
|
+
@current_file = find_or_create_file(type, update_existing)
|
33
|
+
end
|
34
|
+
|
35
|
+
# @param [#read] file object that will be interrogated using the methods: :path, :original_name, :original_filename, :mime_type, :content_type
|
36
|
+
# None of the attribute description methods are required.
|
37
|
+
def update(external_file_url, filename = nil)
|
38
|
+
attach_attributes(external_file_url, filename)
|
39
|
+
persist
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
# Persist a new file with its containing file set; otherwise, just save the file itself
|
45
|
+
def persist
|
46
|
+
if current_file.new_record?
|
47
|
+
file_set.save
|
48
|
+
else
|
49
|
+
current_file.save
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def attach_attributes(external_file_url, filename = nil)
|
54
|
+
current_file.content = StringIO.new('')
|
55
|
+
current_file.original_name = filename
|
56
|
+
current_file.mime_type = "message/external-body; access-type=URL; URL=\"#{external_file_url}\""
|
57
|
+
end
|
58
|
+
|
59
|
+
# @param [Symbol, RDF::URI] the type of association or filter to use
|
60
|
+
# @param [true, false] update_existing when true, try to retrieve existing element before building one
|
61
|
+
def find_or_create_file(type, update_existing)
|
62
|
+
if type.instance_of? Symbol
|
63
|
+
find_or_create_file_for_symbol(type, update_existing)
|
64
|
+
else
|
65
|
+
find_or_create_file_for_rdf_uri(type, update_existing)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def find_or_create_file_for_symbol(type, update_existing)
|
70
|
+
association = file_set.association(type)
|
71
|
+
fail ArgumentError, "you're attempting to add a file to a file_set using '#{type}' association but the file_set does not have an association called '#{type}''" unless association
|
72
|
+
current_file = association.reader if update_existing
|
73
|
+
current_file || association.build
|
74
|
+
end
|
75
|
+
|
76
|
+
def find_or_create_file_for_rdf_uri(type, update_existing)
|
77
|
+
current_file = file_set.filter_files_by_type(type_to_uri(type)).first if update_existing
|
78
|
+
unless current_file
|
79
|
+
file_set.files.build
|
80
|
+
current_file = file_set.files.last
|
81
|
+
Hydra::PCDM::AddTypeToFile.call(current_file, type_to_uri(type))
|
82
|
+
end
|
83
|
+
current_file
|
84
|
+
end
|
85
|
+
|
86
|
+
# Returns appropriate URI for the requested type
|
87
|
+
# * Converts supported symbols to corresponding URIs
|
88
|
+
# * Converts URI strings to RDF::URI
|
89
|
+
# * Returns RDF::URI objects as-is
|
90
|
+
def type_to_uri(type)
|
91
|
+
case type
|
92
|
+
when ::RDF::URI
|
93
|
+
type
|
94
|
+
when String
|
95
|
+
::RDF::URI(type)
|
96
|
+
else
|
97
|
+
fail ArgumentError, 'Invalid file type. You must submit a URI or a symbol.'
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
class VersioningUpdater < Updater
|
103
|
+
def update(*)
|
104
|
+
super && current_file.create_version
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -2,7 +2,7 @@ module Hydra::Works
|
|
2
2
|
class AddFileToFileSet
|
3
3
|
# Adds a file to the file_set
|
4
4
|
# @param [Hydra::PCDM::FileSet] file_set the file will be added to
|
5
|
-
# @param [IO,File,Rack::Multipart::UploadedFile, #read] object that will be the contents. If file responds to :mime_type, :content_type, :original_name, or :original_filename, those will be called to provide metadata.
|
5
|
+
# @param [IO,File,Rack::Multipart::UploadedFile, #read] file the object that will be the contents. If file responds to :mime_type, :content_type, :original_name, or :original_filename, those will be called to provide metadata.
|
6
6
|
# @param [RDF::URI or String] type URI for the RDF.type that identifies the file's role within the file_set
|
7
7
|
# @param [Boolean] update_existing whether to update an existing file if there is one. When set to true, performs a create_or_update. When set to false, always creates a new file within file_set.files.
|
8
8
|
# @param [Boolean] versioning whether to create new version entries (only applicable if +type+ corresponds to a versionable file)
|
@@ -76,6 +76,7 @@ module Hydra::Works
|
|
76
76
|
current_file = file_set.files.last
|
77
77
|
Hydra::PCDM::AddTypeToFile.call(current_file, type_to_uri(type))
|
78
78
|
end
|
79
|
+
current_file
|
79
80
|
end
|
80
81
|
|
81
82
|
# Returns appropriate URI for the requested type
|
@@ -36,7 +36,8 @@ module Hydra::Works
|
|
36
36
|
# @return content of object if source is nil; otherwise, return a File or the source
|
37
37
|
def source_to_content
|
38
38
|
return object.content if source.nil?
|
39
|
-
|
39
|
+
# do not read the file into memory It could be huge...
|
40
|
+
return File.open(source) if source.is_a? String
|
40
41
|
source.rewind
|
41
42
|
source.read
|
42
43
|
end
|
@@ -28,13 +28,25 @@ module Hydra::Works
|
|
28
28
|
# @param [File] file object to retrieve a path for
|
29
29
|
def local_path_for_file(file)
|
30
30
|
return file.path if file.respond_to?(:path)
|
31
|
+
return file.content.path if file.content.respond_to?(:path)
|
32
|
+
|
31
33
|
Tempfile.open('') do |t|
|
32
34
|
t.binmode
|
33
|
-
|
34
|
-
file.content.rewind
|
35
|
+
write_to_temp_file(file, t)
|
35
36
|
t.close
|
36
37
|
t.path
|
37
38
|
end
|
38
39
|
end
|
40
|
+
|
41
|
+
def write_to_temp_file(file, temp_file)
|
42
|
+
if file.new_record?
|
43
|
+
temp_file.write(file.content.read)
|
44
|
+
file.content.rewind
|
45
|
+
else
|
46
|
+
file.stream.each do |chunk|
|
47
|
+
temp_file.write(chunk)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
39
51
|
end
|
40
52
|
end
|
data/lib/hydra/works/version.rb
CHANGED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Hydra::Works::AddExternalFileToFileSet do
|
4
|
+
let(:file_set) { Hydra::Works::FileSet.new }
|
5
|
+
let(:file_set2) { Hydra::Works::FileSet.new }
|
6
|
+
let(:filename) { 'sample-file.pdf' }
|
7
|
+
let(:filename2) { 'updated-file.txt' }
|
8
|
+
let(:original_name) { 'original-name.pdf' }
|
9
|
+
# let(:file) { File.open(File.join(fixture_path, filename)) }
|
10
|
+
# let(:file2) { File.open(File.join(fixture_path, filename2)) }
|
11
|
+
let(:external_file_url) { "http://foo.org/abc1234" }
|
12
|
+
let(:type) { ::RDF::URI('http://pcdm.org/use#ExtractedText') }
|
13
|
+
let(:update_existing) { true }
|
14
|
+
# let(:mime_type) { 'application/pdf' }
|
15
|
+
|
16
|
+
context 'when file_set is not persisted' do
|
17
|
+
let(:file_set) { Hydra::Works::FileSet.new }
|
18
|
+
it 'saves file_set' do
|
19
|
+
described_class.call(file_set, external_file_url, type)
|
20
|
+
expect(file_set.persisted?).to be true
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'when file_set is not valid' do
|
25
|
+
before do
|
26
|
+
file_set.save
|
27
|
+
allow(file_set).to receive(:valid?).and_return(false)
|
28
|
+
end
|
29
|
+
it 'returns false' do
|
30
|
+
expect(described_class.call(file_set, external_file_url, type)).to be false
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'when file set is valid' do
|
35
|
+
before do
|
36
|
+
described_class.call(file_set, external_file_url, type, filename: original_name)
|
37
|
+
end
|
38
|
+
|
39
|
+
subject(:file) { file_set.filter_files_by_type(type).first }
|
40
|
+
|
41
|
+
it 'sets mime type of the File object to message/external body containing external file URL' do
|
42
|
+
expect(file.mime_type).to eq "message/external-body;access-type=URL;url=\"http://foo.org/abc1234\""
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'assigns value of :filename option to the File object' do
|
46
|
+
expect(file.original_name).to eq original_name
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'when no filename is passed in' do
|
50
|
+
before do
|
51
|
+
described_class.call(file_set, external_file_url, type)
|
52
|
+
end
|
53
|
+
|
54
|
+
subject(:file) { file_set.filter_files_by_type(type).last }
|
55
|
+
|
56
|
+
it 'sets filename of File objectd to be the same as the external file url' do
|
57
|
+
expect(file.original_name).to eq external_file_url
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -65,7 +65,7 @@ describe Hydra::Works::CharacterizationService do
|
|
65
65
|
context "using a string path as the source." do
|
66
66
|
it 'passes a file with the string as a path to FileCharacterization.' do
|
67
67
|
path_on_disk = File.join(fixture_path, filename)
|
68
|
-
expect(Hydra::FileCharacterization).to receive(:characterize).with(
|
68
|
+
expect(Hydra::FileCharacterization).to receive(:characterize).with(kind_of(File), filename, :fits)
|
69
69
|
described_class.run(file, path_on_disk)
|
70
70
|
end
|
71
71
|
end
|
@@ -2,8 +2,16 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Hydra::Works::VirusCheckerService do
|
4
4
|
let(:system_virus_scanner) { double }
|
5
|
-
let(:file) { Hydra::PCDM::File.new
|
5
|
+
let(:file) { Hydra::PCDM::File.new }
|
6
6
|
let(:virus_checker) { described_class.new(file, system_virus_scanner) }
|
7
|
+
let(:datastream) { instance_double "ActiveFedora::File::Streaming::FileBody" }
|
8
|
+
let(:content) { instance_double "File" }
|
9
|
+
|
10
|
+
before do
|
11
|
+
allow(file).to receive(:stream).and_return(datastream)
|
12
|
+
allow(file).to receive(:content).and_return(content)
|
13
|
+
allow(datastream).to receive(:each)
|
14
|
+
end
|
7
15
|
|
8
16
|
context '.file_has_virus?' do
|
9
17
|
it 'is a convenience method' do
|
@@ -16,14 +24,15 @@ describe Hydra::Works::VirusCheckerService do
|
|
16
24
|
|
17
25
|
context 'with an infected file' do
|
18
26
|
context 'that responds to :path' do
|
19
|
-
it 'will return
|
27
|
+
it 'will return true' do
|
20
28
|
expect(system_virus_scanner).to receive(:infected?).with('/tmp/file.pdf').and_return(true)
|
21
29
|
allow(file).to receive(:path).and_return('/tmp/file.pdf')
|
22
30
|
expect(virus_checker.file_has_virus?).to eq(true)
|
23
31
|
end
|
24
32
|
end
|
25
33
|
context 'that does not respond to :path' do
|
26
|
-
|
34
|
+
let(:content) { instance_double "File", read: ["abc123", nil], rewind: true }
|
35
|
+
it 'will return true' do
|
27
36
|
expect(system_virus_scanner).to receive(:infected?).with(kind_of(String)).and_return(true)
|
28
37
|
allow(file).to receive(:respond_to?).and_call_original
|
29
38
|
allow(file).to receive(:respond_to?).with(:path).and_return(false)
|
@@ -32,17 +41,47 @@ describe Hydra::Works::VirusCheckerService do
|
|
32
41
|
end
|
33
42
|
end
|
34
43
|
|
35
|
-
context 'with a clean file' do
|
44
|
+
context 'with a clean unsaved file' do
|
36
45
|
context 'that responds to :path' do
|
37
|
-
it 'will return
|
46
|
+
it 'will return false' do
|
38
47
|
expect(system_virus_scanner).to receive(:infected?).with('/tmp/file.pdf').and_return(false)
|
39
48
|
allow(file).to receive(:path).and_return('/tmp/file.pdf')
|
40
49
|
expect(virus_checker.file_has_virus?).to eq(false)
|
41
50
|
end
|
42
51
|
end
|
52
|
+
|
53
|
+
context "that does not respond to path" do
|
54
|
+
before do
|
55
|
+
allow(file).to receive(:respond_to?).with(:path).and_return(false)
|
56
|
+
end
|
57
|
+
context 'that the content responds to path' do
|
58
|
+
let(:content) { instance_double "File", read: ["abc123", nil], rewind: true, path: "abc123" }
|
59
|
+
|
60
|
+
it 'will return false' do
|
61
|
+
expect(system_virus_scanner).to receive(:infected?).with(kind_of(String)).and_return(false)
|
62
|
+
expect(content).not_to receive(:read)
|
63
|
+
expect(virus_checker.file_has_virus?).to eq(false)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
context "that the content does not respond to path" do
|
67
|
+
let(:content) { instance_double "File", read: ["abc123", nil], rewind: true }
|
68
|
+
it 'will return false' do
|
69
|
+
expect(system_virus_scanner).to receive(:infected?).with(kind_of(String)).and_return(false)
|
70
|
+
expect(file).not_to receive(:stream)
|
71
|
+
expect(virus_checker.file_has_virus?).to eq(false)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "with a clean saved file that does not respond to :path" do
|
78
|
+
before do
|
79
|
+
allow(file).to receive(:new_record?).and_return(false)
|
80
|
+
end
|
43
81
|
context 'that does not respond to :path' do
|
44
|
-
it 'will return
|
82
|
+
it 'will return false' do
|
45
83
|
expect(system_virus_scanner).to receive(:infected?).with(kind_of(String)).and_return(false)
|
84
|
+
expect(content).not_to receive(:read)
|
46
85
|
allow(file).to receive(:respond_to?).and_call_original
|
47
86
|
allow(file).to receive(:respond_to?).with(:path).and_return(false)
|
48
87
|
expect(virus_checker.file_has_virus?).to eq(false)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hydra-works
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.16.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Coyne
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-03-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hydra-pcdm
|
@@ -196,6 +196,7 @@ files:
|
|
196
196
|
- ".gitignore"
|
197
197
|
- ".rspec"
|
198
198
|
- ".rubocop.yml"
|
199
|
+
- ".rubocop_todo.yml"
|
199
200
|
- ".travis.yml"
|
200
201
|
- CONTRIBUTING.md
|
201
202
|
- Gemfile
|
@@ -225,6 +226,7 @@ files:
|
|
225
226
|
- lib/hydra/works/models/work.rb
|
226
227
|
- lib/hydra/works/not_collection_validator.rb
|
227
228
|
- lib/hydra/works/not_file_set_validator.rb
|
229
|
+
- lib/hydra/works/services/add_external_file_to_file_set.rb
|
228
230
|
- lib/hydra/works/services/add_file_to_file_set.rb
|
229
231
|
- lib/hydra/works/services/characterization_service.rb
|
230
232
|
- lib/hydra/works/services/determine_mime_type.rb
|
@@ -282,6 +284,7 @@ files:
|
|
282
284
|
- spec/hydra/works/models/concerns/file_set_behavior_spec.rb
|
283
285
|
- spec/hydra/works/models/file_set_spec.rb
|
284
286
|
- spec/hydra/works/models/work_spec.rb
|
287
|
+
- spec/hydra/works/services/add_external_file_to_file_set_spec.rb
|
285
288
|
- spec/hydra/works/services/add_file_to_file_set_spec.rb
|
286
289
|
- spec/hydra/works/services/characterization_service_spec.rb
|
287
290
|
- spec/hydra/works/services/determine_mime_type_spec.rb
|
@@ -322,7 +325,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
322
325
|
version: '0'
|
323
326
|
requirements: []
|
324
327
|
rubyforge_project:
|
325
|
-
rubygems_version: 2.
|
328
|
+
rubygems_version: 2.6.10
|
326
329
|
signing_key:
|
327
330
|
specification_version: 4
|
328
331
|
summary: Fundamental repository data model for hydra
|
@@ -358,6 +361,7 @@ test_files:
|
|
358
361
|
- spec/hydra/works/models/concerns/file_set_behavior_spec.rb
|
359
362
|
- spec/hydra/works/models/file_set_spec.rb
|
360
363
|
- spec/hydra/works/models/work_spec.rb
|
364
|
+
- spec/hydra/works/services/add_external_file_to_file_set_spec.rb
|
361
365
|
- spec/hydra/works/services/add_file_to_file_set_spec.rb
|
362
366
|
- spec/hydra/works/services/characterization_service_spec.rb
|
363
367
|
- spec/hydra/works/services/determine_mime_type_spec.rb
|