assembly-objectfile 1.7.1 → 1.7.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.rubocop.yml +22 -0
- data/.rubocop_todo.yml +132 -0
- data/Gemfile +1 -1
- data/Rakefile +9 -9
- data/assembly-objectfile.gemspec +11 -10
- data/lib/assembly-objectfile.rb +24 -22
- data/lib/assembly-objectfile/content_metadata.rb +225 -220
- data/lib/assembly-objectfile/object_file.rb +6 -9
- data/lib/assembly-objectfile/object_fileable.rb +131 -198
- data/lib/assembly-objectfile/version.rb +2 -4
- data/spec/content_metadata_spec.rb +455 -439
- data/spec/object_file_spec.rb +86 -83
- data/spec/spec_helper.rb +48 -45
- data/spec/test_data/input/someobject.obj +1 -0
- data/spec/test_data/input/someobject.ply +1 -0
- metadata +45 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 833c781110e681cf9555ef036857efd1a4758902c8aea55f55f4e712e78470d1
|
4
|
+
data.tar.gz: 83111a9c4145aac66c8e7ba24a07336c13cbf851407d05ad9a9d45d7bbf138d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9346400c4e01abece34de372d086037a60f3f0cf78479630986d0d25a346042e51dc8d6e8a6835f0b9dd5bf9e70fdbd4385a8689cbd9d9ddbd899142b60534ae
|
7
|
+
data.tar.gz: 80cf1fa0011f28e08975d401c8d2ce3156485b4b4b3107b665d628679f1e9e4d4032313fb2434f5c7d24997f7004af6533f36b3f561560f284095264ff262a25
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
inherit_from: .rubocop_todo.yml
|
2
|
+
require: rubocop-rspec
|
3
|
+
|
4
|
+
# Configuration parameters: AllowURI, URISchemes.
|
5
|
+
Metrics/LineLength:
|
6
|
+
Max: 200
|
7
|
+
|
8
|
+
RSpec/ContextWording:
|
9
|
+
Enabled: false # too dogmatic
|
10
|
+
|
11
|
+
RSpec/ExampleLength:
|
12
|
+
Max: 25
|
13
|
+
|
14
|
+
# we like 'expect(x).to receive' better than 'have_received'
|
15
|
+
RSpec/MessageSpies:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
RSpec/MultipleExpectations:
|
19
|
+
Max: 5
|
20
|
+
|
21
|
+
RSpec/NestedGroups:
|
22
|
+
Max: 4 # default: 3
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2018-09-13 12:44:21 -0700 using RuboCop version 0.59.0.
|
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: 1
|
10
|
+
# Cop supports --auto-correct.
|
11
|
+
# Configuration parameters: EnforcedStyleAlignWith, AutoCorrect, Severity.
|
12
|
+
# SupportedStylesAlignWith: keyword, variable, start_of_line
|
13
|
+
Layout/EndAlignment:
|
14
|
+
Exclude:
|
15
|
+
- 'lib/assembly-objectfile/content_metadata.rb'
|
16
|
+
|
17
|
+
# Offense count: 1
|
18
|
+
Lint/UselessAssignment:
|
19
|
+
Exclude:
|
20
|
+
- 'config/boot.rb'
|
21
|
+
|
22
|
+
# Offense count: 5
|
23
|
+
# Configuration parameters: CheckForMethodsWithNoSideEffects.
|
24
|
+
Lint/Void:
|
25
|
+
Exclude:
|
26
|
+
- 'spec/content_metadata_spec.rb'
|
27
|
+
|
28
|
+
# Offense count: 3
|
29
|
+
Metrics/AbcSize:
|
30
|
+
Max: 170
|
31
|
+
|
32
|
+
# Offense count: 15
|
33
|
+
# Configuration parameters: CountComments, ExcludedMethods.
|
34
|
+
# ExcludedMethods: refine
|
35
|
+
Metrics/BlockLength:
|
36
|
+
Max: 549
|
37
|
+
|
38
|
+
# Offense count: 1
|
39
|
+
# Configuration parameters: CountComments.
|
40
|
+
Metrics/ClassLength:
|
41
|
+
Max: 137
|
42
|
+
|
43
|
+
# Offense count: 1
|
44
|
+
Metrics/CyclomaticComplexity:
|
45
|
+
Max: 63
|
46
|
+
|
47
|
+
# Offense count: 26
|
48
|
+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
49
|
+
# URISchemes: http, https
|
50
|
+
Metrics/LineLength:
|
51
|
+
Max: 304
|
52
|
+
|
53
|
+
# Offense count: 1
|
54
|
+
# Configuration parameters: CountComments.
|
55
|
+
Metrics/MethodLength:
|
56
|
+
Max: 131
|
57
|
+
|
58
|
+
# Offense count: 1
|
59
|
+
# Configuration parameters: CountComments.
|
60
|
+
Metrics/ModuleLength:
|
61
|
+
Max: 106
|
62
|
+
|
63
|
+
# Offense count: 1
|
64
|
+
Metrics/PerceivedComplexity:
|
65
|
+
Max: 59
|
66
|
+
|
67
|
+
# Offense count: 2
|
68
|
+
# Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms.
|
69
|
+
# AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS
|
70
|
+
Naming/FileName:
|
71
|
+
Exclude:
|
72
|
+
- 'assembly-objectfile.gemspec'
|
73
|
+
- 'lib/assembly-objectfile.rb'
|
74
|
+
|
75
|
+
# Offense count: 2
|
76
|
+
# Configuration parameters: NamePrefix, NamePrefixBlacklist, NameWhitelist, MethodDefinitionMacros.
|
77
|
+
# NamePrefix: is_, has_, have_
|
78
|
+
# NamePrefixBlacklist: is_, has_, have_
|
79
|
+
# NameWhitelist: is_a?
|
80
|
+
# MethodDefinitionMacros: define_method, define_singleton_method
|
81
|
+
Naming/PredicateName:
|
82
|
+
Exclude:
|
83
|
+
- 'spec/**/*'
|
84
|
+
- 'lib/assembly-objectfile/content_metadata.rb'
|
85
|
+
- 'lib/assembly-objectfile/object_fileable.rb'
|
86
|
+
|
87
|
+
# Offense count: 8
|
88
|
+
# Configuration parameters: Max.
|
89
|
+
RSpec/ExampleLength:
|
90
|
+
Exclude:
|
91
|
+
- 'spec/content_metadata_spec.rb'
|
92
|
+
|
93
|
+
# Offense count: 2
|
94
|
+
# Configuration parameters: CustomTransform, IgnoreMethods.
|
95
|
+
RSpec/FilePath:
|
96
|
+
Exclude:
|
97
|
+
- 'spec/content_metadata_spec.rb'
|
98
|
+
- 'spec/object_file_spec.rb'
|
99
|
+
|
100
|
+
# Offense count: 60
|
101
|
+
# Configuration parameters: AssignmentOnly.
|
102
|
+
RSpec/InstanceVariable:
|
103
|
+
Exclude:
|
104
|
+
- 'spec/object_file_spec.rb'
|
105
|
+
|
106
|
+
# Offense count: 28
|
107
|
+
# Configuration parameters: AggregateFailuresByDefault.
|
108
|
+
RSpec/MultipleExpectations:
|
109
|
+
Max: 29
|
110
|
+
|
111
|
+
# Offense count: 2
|
112
|
+
RSpec/RepeatedDescription:
|
113
|
+
Exclude:
|
114
|
+
- 'spec/object_file_spec.rb'
|
115
|
+
|
116
|
+
# Offense count: 6
|
117
|
+
Style/CommentedKeyword:
|
118
|
+
Exclude:
|
119
|
+
- 'lib/assembly-objectfile/content_metadata.rb'
|
120
|
+
|
121
|
+
# Offense count: 1
|
122
|
+
Style/Documentation:
|
123
|
+
Exclude:
|
124
|
+
- 'spec/**/*'
|
125
|
+
- 'test/**/*'
|
126
|
+
- 'lib/assembly-objectfile.rb'
|
127
|
+
|
128
|
+
# Offense count: 1
|
129
|
+
# Configuration parameters: MinBodyLength.
|
130
|
+
Style/GuardClause:
|
131
|
+
Exclude:
|
132
|
+
- 'lib/assembly-objectfile/object_file.rb'
|
data/Gemfile
CHANGED
data/Rakefile
CHANGED
@@ -1,17 +1,17 @@
|
|
1
|
-
require
|
1
|
+
require 'bundler/gem_tasks'
|
2
2
|
|
3
|
-
#require 'dlss/rake/dlss_release'
|
4
|
-
#Dlss::Release.new
|
3
|
+
# require 'dlss/rake/dlss_release'
|
4
|
+
# Dlss::Release.new
|
5
5
|
|
6
|
-
desc
|
7
|
-
task :console, :irb do |
|
8
|
-
irb = args[:irb].nil
|
9
|
-
sh irb,
|
6
|
+
desc 'Run console with irb (default), pry, etc.'
|
7
|
+
task :console, :irb do |_t, args|
|
8
|
+
irb = args[:irb].nil? ? 'irb' : args[:irb]
|
9
|
+
sh irb, '-r', "#{File.dirname(__FILE__)}/config/boot.rb"
|
10
10
|
end
|
11
11
|
|
12
12
|
require 'rspec/core/rake_task'
|
13
13
|
|
14
|
-
desc
|
14
|
+
desc 'Run specs'
|
15
15
|
RSpec::Core::RakeTask.new(:spec)
|
16
16
|
|
17
|
-
task :
|
17
|
+
task default: :spec
|
data/assembly-objectfile.gemspec
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
$LOAD_PATH.push File.expand_path(
|
1
|
+
$LOAD_PATH.push File.expand_path('lib', __dir__)
|
2
2
|
require 'assembly-objectfile/version'
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = 'assembly-objectfile'
|
6
6
|
s.version = Assembly::ObjectFile::VERSION
|
7
7
|
s.platform = Gem::Platform::RUBY
|
8
|
-
s.authors = [
|
9
|
-
s.email = [
|
8
|
+
s.authors = ['Peter Mangiafico', 'Renzo Sanchez-Silva', 'Monty Hindman', 'Tony Calavano']
|
9
|
+
s.email = ['pmangiafico@stanford.edu']
|
10
10
|
s.homepage = 'https://github.com/sul-dlss/assembly-objectfile'
|
11
|
-
s.summary =
|
12
|
-
s.description =
|
11
|
+
s.summary = 'Ruby immplementation of file services needed to prepare objects to be accessioned in SULAIR digital library'
|
12
|
+
s.description = 'Get exif data, file sizes and more.'
|
13
13
|
s.license = 'ALv2'
|
14
14
|
|
15
15
|
s.rubyforge_project = 'assembly-objectfile'
|
@@ -20,13 +20,14 @@ Gem::Specification.new do |s|
|
|
20
20
|
s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
21
|
s.require_paths = ['lib']
|
22
22
|
|
23
|
+
s.add_dependency 'mime-types', '> 3'
|
23
24
|
s.add_dependency 'mini_exiftool'
|
24
|
-
s.add_dependency 'mime-types'
|
25
25
|
s.add_dependency 'nokogiri'
|
26
26
|
|
27
|
-
s.add_development_dependency 'rake'
|
28
27
|
s.add_development_dependency 'json'
|
29
|
-
s.add_development_dependency
|
30
|
-
s.add_development_dependency
|
31
|
-
|
28
|
+
s.add_development_dependency 'rake'
|
29
|
+
s.add_development_dependency 'rspec', '~> 3.0'
|
30
|
+
s.add_development_dependency 'rubocop'
|
31
|
+
s.add_development_dependency 'rubocop-rspec'
|
32
|
+
s.add_development_dependency 'yard'
|
32
33
|
end
|
data/lib/assembly-objectfile.rb
CHANGED
@@ -1,37 +1,39 @@
|
|
1
1
|
module Assembly
|
2
|
-
|
3
2
|
# the path to the gem, used to access profiles stored with the gem
|
4
3
|
PATH_TO_GEM = File.expand_path(File.dirname(__FILE__) + '/..')
|
5
4
|
|
6
5
|
# if input image is not one of these mime types, it will not be regarded as a valid image for the purpose of generating a JP2 derivative
|
7
|
-
VALID_IMAGE_MIMETYPES=[
|
8
|
-
|
6
|
+
VALID_IMAGE_MIMETYPES = ['image/jpeg', 'image/tiff', 'image/tif', 'image/png'].freeze
|
7
|
+
|
8
|
+
# if input file has one of these extensions in a 3D object, it will get the 3d resource type
|
9
|
+
VALID_THREE_DIMENSION_EXTENTIONS = ['.obj', '.ply', '.threejs', '.gltf'].freeze
|
10
|
+
|
9
11
|
# the list of mimetypes that will be "trusted" by the unix file command; if a mimetype other than one of these is returned
|
10
12
|
# by the file command, then a check will be made to see if exif data exists...if so, the mimetype returned by the exif data will be used
|
11
13
|
# if no exif data exists, then the mimetype returned by the unix file command will be used
|
12
|
-
TRUSTED_MIMETYPES=[
|
13
|
-
|
14
|
+
TRUSTED_MIMETYPES = ['text/plain', 'plain/text', 'application/pdf', 'text/html', 'application/xml'].freeze
|
15
|
+
|
14
16
|
# default publish/preserve/shelve attributes used in content metadata
|
15
|
-
FILE_ATTRIBUTES=
|
17
|
+
FILE_ATTRIBUTES = {}
|
16
18
|
# if no mimetype specific attributes are specified for a given file, define some defaults, and override for specific mimetypes below
|
17
|
-
FILE_ATTRIBUTES['default']={:
|
18
|
-
FILE_ATTRIBUTES['image/tif']={:
|
19
|
-
FILE_ATTRIBUTES['image/tiff']={:
|
20
|
-
FILE_ATTRIBUTES['image/jp2']={:
|
21
|
-
FILE_ATTRIBUTES['image/jpeg']={:
|
22
|
-
FILE_ATTRIBUTES['audio/wav']={:
|
23
|
-
FILE_ATTRIBUTES['audio/x-wav']={:
|
24
|
-
FILE_ATTRIBUTES['audio/mp3']={:
|
25
|
-
FILE_ATTRIBUTES['audio/mpeg']={:
|
26
|
-
FILE_ATTRIBUTES['application/pdf']={:
|
27
|
-
FILE_ATTRIBUTES['plain/text']={:
|
28
|
-
FILE_ATTRIBUTES['text/plain']={:
|
29
|
-
FILE_ATTRIBUTES['image/png']={:
|
30
|
-
FILE_ATTRIBUTES['application/zip']={:
|
31
|
-
|
19
|
+
FILE_ATTRIBUTES['default'] = { preserve: 'yes', shelve: 'no', publish: 'no' }
|
20
|
+
FILE_ATTRIBUTES['image/tif'] = { preserve: 'yes', shelve: 'no', publish: 'no' }
|
21
|
+
FILE_ATTRIBUTES['image/tiff'] = { preserve: 'yes', shelve: 'no', publish: 'no' }
|
22
|
+
FILE_ATTRIBUTES['image/jp2'] = { preserve: 'no', shelve: 'yes', publish: 'yes' }
|
23
|
+
FILE_ATTRIBUTES['image/jpeg'] = { preserve: 'yes', shelve: 'no', publish: 'no' }
|
24
|
+
FILE_ATTRIBUTES['audio/wav'] = { preserve: 'yes', shelve: 'no', publish: 'no' }
|
25
|
+
FILE_ATTRIBUTES['audio/x-wav'] = { preserve: 'yes', shelve: 'no', publish: 'no' }
|
26
|
+
FILE_ATTRIBUTES['audio/mp3'] = { preserve: 'no', shelve: 'yes', publish: 'yes' }
|
27
|
+
FILE_ATTRIBUTES['audio/mpeg'] = { preserve: 'no', shelve: 'yes', publish: 'yes' }
|
28
|
+
FILE_ATTRIBUTES['application/pdf'] = { preserve: 'yes', shelve: 'yes', publish: 'yes' }
|
29
|
+
FILE_ATTRIBUTES['plain/text'] = { preserve: 'yes', shelve: 'yes', publish: 'yes' }
|
30
|
+
FILE_ATTRIBUTES['text/plain'] = { preserve: 'yes', shelve: 'yes', publish: 'yes' }
|
31
|
+
FILE_ATTRIBUTES['image/png'] = { preserve: 'yes', shelve: 'yes', publish: 'no'}
|
32
|
+
FILE_ATTRIBUTES['application/zip'] = { preserve: 'yes', shelve: 'no', publish: 'no' }
|
33
|
+
FILE_ATTRIBUTES['application/json'] = { preserve: 'yes', shelve: 'yes', publish: 'yes' }
|
32
34
|
end
|
33
35
|
|
34
36
|
require 'assembly-objectfile/content_metadata'
|
35
37
|
require 'assembly-objectfile/object_fileable'
|
36
38
|
require 'assembly-objectfile/object_file'
|
37
|
-
require 'assembly-objectfile/version'
|
39
|
+
require 'assembly-objectfile/version'
|
@@ -1,232 +1,237 @@
|
|
1
1
|
require 'nokogiri'
|
2
2
|
|
3
3
|
module Assembly
|
4
|
+
SPECIAL_DPG_FOLDERS = %w[31 44 50].freeze # these special dpg folders will force any files contained in them into their own resources, regardless of filenaming convention
|
5
|
+
# these are used when :bundle=>:dpg only
|
6
|
+
|
7
|
+
DEPRECATED_STYLES = %i[book_with_pdf book_as_image].freeze
|
4
8
|
|
5
|
-
SPECIAL_DPG_FOLDERS = ['31', '44', '50'] # these special dpg folders will force any files contained in them into their own resources, regardless of filenaming convention
|
6
|
-
# these are used when :bundle=>:dpg only
|
7
|
-
|
8
|
-
DEPRECATED_STYLES = [:book_with_pdf, :book_as_image]
|
9
|
-
|
10
9
|
# This class generates content metadata for image files
|
11
10
|
class ContentMetadata
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
11
|
+
# Generates image content XML metadata for a repository object.
|
12
|
+
# This method only produces content metadata for images
|
13
|
+
# and does not depend on a specific folder structure. Note that it is class level method.
|
14
|
+
#
|
15
|
+
# @param [Hash] params a hash containg parameters needed to produce content metadata
|
16
|
+
# :druid = required - a string of druid of the repository object's druid id (with or without 'druid:' prefix)
|
17
|
+
# :objects = required - an array of Assembly::ObjectFile objects containing the list of files to add to content metadata
|
18
|
+
# NOTE: if you set the :bundle option to :prebundled, you will need to pass in an array of arrays, and not a flat array, as noted below
|
19
|
+
# :style = optional - a symbol containing the style of metadata to create, allowed values are
|
20
|
+
# :simple_image (default), contentMetadata type="image", resource type="image"
|
21
|
+
# :file, contentMetadata type="file", resource type="file"
|
22
|
+
# :simple_book, contentMetadata type="book", resource type="page", but any resource which has file(s) other than an image, and also contains no images at all, will be resource type="object"
|
23
|
+
# :book_with_pdf, contentMetadata type="book", resource type="page", but any resource which has any file(s) other than an image will be resource type="object" - NOTE: THIS IS DEPRECATED
|
24
|
+
# :book_as_image, as simple_book, but with contentMetadata type="book", resource type="image" (same rule applies for resources with non images) - NOTE: THIS IS DEPRECATED
|
25
|
+
# :map, like simple_image, but with contentMetadata type="map", resource type="image"
|
26
|
+
# :3d, contentMetadata type="3d", ".obj" and other configured 3d extension files go into resource_type="3d", everything else into resource_type="file"
|
27
|
+
# :bundle = optional - a symbol containing the method of bundling files into resources, allowed values are
|
28
|
+
# :default = all files get their own resources (default)
|
29
|
+
# :filename = files with the same filename but different extensions get bundled together in a single resource
|
30
|
+
# :dpg = files representing the same image but of different mimetype that use the SULAIR DPG filenaming standard (00 vs 05) get bundled together in a single resource
|
31
|
+
# :prebundlded = this option requires you to prebundled the files passed in as an array of arrays, indicating how files are bundlded into resources; this is the most flexible option since it gives you full control
|
32
|
+
# :add_exif = optional - a boolean to indicate if exif data should be added (mimetype, filesize, image height/width, etc.) to each file, defaults to false and is not required if project goes through assembly
|
33
|
+
# :add_file_attributes = optional - a boolean to indicate if publish/preserve/shelve/role attributes should be added using defaults or by supplied override by mime/type, defaults to false and is not required if project goes through assembly
|
34
|
+
# :file_attributes = optional - a hash of file attributes by mimetype to use instead of defaults, only used if add_file_attributes is also true,
|
35
|
+
# If a mimetype match is not found in your hash, the default is used (either your supplied default or the gems).
|
36
|
+
# e.g. {'default'=>{:preserve=>'yes',:shelve=>'yes',:publish=>'yes'},'image/tif'=>{:preserve=>'yes',:shelve=>'no',:publish=>'no'},'application/pdf'=>{:preserve=>'yes',:shelve=>'yes',:publish=>'yes'}}
|
37
|
+
# :include_root_xml = optional - a boolean to indicate if the contentMetadata returned includes a root <?xml version="1.0"?> tag, defaults to true
|
38
|
+
# :preserve_common_paths = optional - When creating the file "id" attribute, content metadata uses the "relative_path" attribute of the ObjectFile objects passed in. If the "relative_path" attribute is not set, the "path" attribute is used instead,
|
39
|
+
# which includes a full path to the file. If the "preserve_common_paths" parameter is set to false or left off, then the common paths of all of the ObjectFile's passed in are removed from any "path" attributes. This should turn full paths into
|
40
|
+
# the relative paths that are required in content metadata file id nodes. If you do not want this behavior, set "preserve_common_paths" to true. The default is false.
|
41
|
+
# :flatten_folder_structure = optional - Will remove *all* folder structure when genearting file IDs (e.g. DPG subfolders like '00','05' will be removed) when generating file IDs. This is useful if the folder structure is flattened when staging files (like for DPG).
|
42
|
+
# The default is false. If set to true, will override the "preserve_common_paths" parameter.
|
43
|
+
# :auto_labels = optional - Will add automated resource labels (e.g. "File 1") when labels are not provided by the user. The default is true.
|
44
|
+
# Example:
|
45
|
+
# Assembly::ContentMetadata.create_content_metadata(:druid=>'druid:nx288wh8889',:style=>:simple_image,:objects=>object_files,:add_file_attributes=>false)
|
46
|
+
def self.create_content_metadata(params = {})
|
47
|
+
druid = params[:druid]
|
48
|
+
objects = params[:objects]
|
49
|
+
|
50
|
+
raise 'No objects and/or druid supplied' if druid.nil? || objects.nil?
|
51
|
+
|
52
|
+
pid = druid.gsub('druid:', '') # remove druid prefix when creating IDs
|
53
|
+
|
54
|
+
style = params[:style] || :simple_image
|
55
|
+
bundle = params[:bundle] || :default
|
56
|
+
add_exif = params[:add_exif] || false
|
57
|
+
auto_labels = (params[:auto_labels].nil? ? true : params[:auto_labels])
|
58
|
+
add_file_attributes = params[:add_file_attributes] || false
|
59
|
+
file_attributes = params[:file_attributes] || {}
|
60
|
+
preserve_common_paths = params[:preserve_common_paths] || false
|
61
|
+
flatten_folder_structure = params[:flatten_folder_structure] || false
|
62
|
+
include_root_xml = params[:include_root_xml]
|
63
|
+
|
64
|
+
all_paths = []
|
65
|
+
objects.flatten.each do |obj|
|
66
|
+
raise "File '#{obj.path}' not found" unless obj.file_exists?
|
67
|
+
|
68
|
+
all_paths << obj.path unless preserve_common_paths # collect all of the filenames into an array
|
69
|
+
end
|
70
|
+
|
71
|
+
common_path = Assembly::ObjectFile.common_path(all_paths) unless preserve_common_paths # find common paths to all files provided if needed
|
72
|
+
|
73
|
+
# these are the valid strings for each type of document to be use contentMetadata type and resourceType
|
74
|
+
content_type_descriptions = { file: 'file', image: 'image', book: 'book', map: 'map', '3d': '3d' }
|
75
|
+
resource_type_descriptions = { object: 'object', file: 'file', image: 'image', book: 'page', map: 'image', '3d': '3d' }
|
76
|
+
|
77
|
+
# global sequence for resource IDs
|
78
|
+
sequence = 0
|
79
|
+
|
80
|
+
# a counter to use when creating auto-labels for resources, with incremenets for each type
|
81
|
+
resource_type_counters = Hash.new(0)
|
82
|
+
|
83
|
+
# set the object level content type id
|
84
|
+
case style
|
85
|
+
when :simple_image
|
86
|
+
content_type_description = content_type_descriptions[:image]
|
87
|
+
when :file
|
88
|
+
content_type_description = content_type_descriptions[:file]
|
89
|
+
when :simple_book, :book_with_pdf, :book_as_image
|
90
|
+
content_type_description = content_type_descriptions[:book]
|
91
|
+
when :map
|
92
|
+
content_type_description = content_type_descriptions[:map]
|
93
|
+
when :'3d'
|
94
|
+
content_type_description = content_type_descriptions[:'3d']
|
95
|
+
else
|
96
|
+
raise 'Supplied style not valid'
|
97
|
+
end
|
98
|
+
|
99
|
+
puts "WARNING - the style #{style} is now deprecated and should not be used." if DEPRECATED_STYLES.include? style
|
100
|
+
|
101
|
+
# determine how many resources to create
|
102
|
+
# setup an array of arrays, where the first array is the number of resources, and the second array is the object files containined in that resource
|
103
|
+
case bundle
|
104
|
+
when :default # one resource per object
|
105
|
+
resources = objects.collect { |obj| [obj] }
|
106
|
+
when :filename # one resource per distinct filename (excluding extension)
|
107
|
+
# loop over distinct filenames, this determines how many resources we will have and
|
108
|
+
# create one resource node per distinct filename, collecting the relevant objects with the distinct filename into that resource
|
109
|
+
resources = []
|
110
|
+
distinct_filenames = objects.collect(&:filename_without_ext).uniq # find all the unique filenames in the set of objects, leaving off extensions and base paths
|
111
|
+
distinct_filenames.each { |distinct_filename| resources << objects.collect { |obj| obj if obj.filename_without_ext == distinct_filename }.compact }
|
112
|
+
when :dpg # group by DPG filename
|
113
|
+
# loop over distinct dpg base names, this determines how many resources we will have and
|
114
|
+
# create one resource node per distinct dpg base name, collecting the relevant objects with the distinct names into that resource
|
115
|
+
resources = []
|
116
|
+
distinct_filenames = objects.collect(&:dpg_basename).uniq # find all the unique DPG filenames in the set of objects
|
117
|
+
distinct_filenames.each do |distinct_filename|
|
118
|
+
resources << objects.collect { |obj| obj if obj.dpg_basename == distinct_filename && !is_special_dpg_folder?(obj.dpg_folder) }.compact
|
96
119
|
end
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
120
|
+
objects.each { |obj| resources << [obj] if is_special_dpg_folder?(obj.dpg_folder) } # certain subfolders require individual resources for files within them regardless of file-naming convention
|
121
|
+
when :prebundled
|
122
|
+
# if the user specifies this method, they will pass in an array of arrays, indicating resources, so we don't need to bundle in the gem
|
123
|
+
resources = objects
|
124
|
+
else
|
125
|
+
raise 'Invalid bundle method'
|
126
|
+
end
|
127
|
+
|
128
|
+
resources.delete([]) # delete any empty elements
|
129
|
+
|
130
|
+
builder = Nokogiri::XML::Builder.new do |xml|
|
131
|
+
xml.contentMetadata(objectId: druid.to_s, type: content_type_description) do
|
132
|
+
resources.each do |resource_files| # iterate over all the resources
|
133
|
+
# start a new resource element
|
134
|
+
sequence += 1
|
135
|
+
resource_id = "#{pid}_#{sequence}"
|
136
|
+
|
137
|
+
# grab all of the file types within a resource into an array so we can decide what the resource type should be
|
138
|
+
resource_file_types = resource_files.collect(&:object_type)
|
139
|
+
resource_has_non_images = !(resource_file_types - [:image]).empty?
|
140
|
+
resource_from_special_dpg_folder = resource_files.collect { |obj| is_special_dpg_folder?(obj.dpg_folder) }.uniq
|
141
|
+
|
142
|
+
if bundle == :dpg && resource_from_special_dpg_folder.include?(true) # objects in the special DPG folders are always type=object when we using :bundle=>:dpg
|
143
|
+
resource_type_description = resource_type_descriptions[:object]
|
144
|
+
else # otherwise look at the style to determine the resource_type_description
|
145
|
+
case style
|
146
|
+
when :simple_image
|
147
|
+
resource_type_description = resource_type_descriptions[:image]
|
148
|
+
when :file
|
149
|
+
resource_type_description = resource_type_descriptions[:file]
|
150
|
+
when :simple_book # in a simple book project, all resources are pages unless they are *all* non-images -- if so, switch the type to object
|
151
|
+
resource_type_description = resource_has_non_images && resource_file_types.include?(:image) == false ? resource_type_descriptions[:object] : resource_type_descriptions[:book]
|
152
|
+
when :book_as_image # same as simple book, but all resources are images instead of pages, unless we need to switch them to object type
|
153
|
+
resource_type_description = resource_has_non_images && resource_file_types.include?(:image) == false ? resource_type_descriptions[:object] : resource_type_descriptions[:image]
|
154
|
+
when :book_with_pdf # in book with PDF type, if we find a resource with *any* non images, switch it's type from book to object
|
155
|
+
resource_type_description = resource_has_non_images ? resource_type_descriptions[:object] : resource_type_descriptions[:book]
|
156
|
+
when :map
|
157
|
+
resource_type_description = resource_type_descriptions[:map]
|
158
|
+
when :'3d'
|
159
|
+
resource_extensions = resource_files.collect {|obj| obj.ext}
|
160
|
+
if (resource_extensions & VALID_THREE_DIMENSION_EXTENTIONS).empty? # if this resource contains no known 3D file extensions, the resource type is file
|
161
|
+
resource_type_description = resource_type_descriptions[:file]
|
162
|
+
else # otherwise the resource type is 3d
|
163
|
+
resource_type_description = resource_type_descriptions[:'3d']
|
164
|
+
end
|
165
|
+
end
|
118
166
|
end
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
resource_files.each do |obj| # iterate over all the files in a resource
|
174
|
-
|
175
|
-
mimetype = obj.mimetype if (add_file_attributes || add_exif) # we only need to compute the mimetype if we are adding file attributes or exif info, otherwise skip it for performance reasons
|
176
|
-
|
177
|
-
# set file id attribute, first check the relative_path parameter on the object, and if it is set, just use that
|
178
|
-
if obj.relative_path
|
179
|
-
file_id=obj.relative_path
|
180
|
-
else
|
181
|
-
# if the relative_path attribute is not set, then use the path attribute and check to see if we need to remove the common part of the path
|
182
|
-
file_id=preserve_common_paths ? obj.path : obj.path.gsub(common_path,'')
|
183
|
-
file_id=File.basename(file_id) if flatten_folder_structure
|
184
|
-
end
|
185
|
-
|
186
|
-
xml_file_params = {:id=> file_id}
|
187
|
-
|
188
|
-
if add_file_attributes
|
189
|
-
file_attributes_hash=obj.file_attributes || file_attributes[mimetype] || file_attributes['default'] || Assembly::FILE_ATTRIBUTES[mimetype] || Assembly::FILE_ATTRIBUTES['default']
|
190
|
-
xml_file_params.merge!({
|
191
|
-
:preserve => file_attributes_hash[:preserve],
|
192
|
-
:publish => file_attributes_hash[:publish],
|
193
|
-
:shelve => file_attributes_hash[:shelve],
|
194
|
-
:role => file_attributes_hash[:role],
|
195
|
-
})
|
196
|
-
xml_file_params.reject! { |k, v| v.nil? || v.empty? }
|
197
|
-
end
|
198
|
-
|
199
|
-
xml_file_params.merge!({:mimetype => mimetype,:size => obj.filesize}) if add_exif
|
200
|
-
xml.file(xml_file_params) {
|
201
|
-
if add_exif # add exif info if the user requested it
|
202
|
-
xml.checksum(obj.sha1, :type => 'sha1')
|
203
|
-
xml.checksum(obj.md5, :type => 'md5')
|
204
|
-
xml.imageData(:height => obj.exif.imageheight, :width => obj.exif.imagewidth) if obj.image? # add image data for an image
|
205
|
-
elsif obj.provider_md5 || obj.provider_sha1 # if we did not add exif info, see if there are user supplied checksums to add
|
206
|
-
xml.checksum(obj.provider_sha1, :type => 'sha1') if obj.provider_sha1
|
207
|
-
xml.checksum(obj.provider_md5, :type => 'md5') if obj.provider_md5
|
208
|
-
end #add_exif
|
209
|
-
}
|
210
|
-
end # end resource_files.each
|
211
|
-
}
|
212
|
-
end # resources.each
|
213
|
-
}
|
214
|
-
end
|
215
|
-
|
216
|
-
if include_root_xml == false
|
217
|
-
result = builder.doc.root.to_xml
|
218
|
-
else
|
219
|
-
result = builder.to_xml
|
167
|
+
|
168
|
+
resource_type_counters[resource_type_description.to_sym] += 1 # each resource type description gets its own incrementing counter
|
169
|
+
|
170
|
+
xml.resource(id: resource_id, sequence: sequence, type: resource_type_description) do
|
171
|
+
# create a generic resource label if needed
|
172
|
+
resource_label = (auto_labels == true ? "#{resource_type_description.capitalize} #{resource_type_counters[resource_type_description.to_sym]}" : '')
|
173
|
+
|
174
|
+
# but if one of the files has a label, use it instead
|
175
|
+
resource_files.each { |obj| resource_label = obj.label unless obj.label.nil? || obj.label.empty? }
|
176
|
+
|
177
|
+
xml.label(resource_label) unless resource_label.empty?
|
178
|
+
|
179
|
+
resource_files.each do |obj| # iterate over all the files in a resource
|
180
|
+
mimetype = obj.mimetype if add_file_attributes || add_exif # we only need to compute the mimetype if we are adding file attributes or exif info, otherwise skip it for performance reasons
|
181
|
+
|
182
|
+
# set file id attribute, first check the relative_path parameter on the object, and if it is set, just use that
|
183
|
+
if obj.relative_path
|
184
|
+
file_id = obj.relative_path
|
185
|
+
else
|
186
|
+
# if the relative_path attribute is not set, then use the path attribute and check to see if we need to remove the common part of the path
|
187
|
+
file_id = preserve_common_paths ? obj.path : obj.path.gsub(common_path, '')
|
188
|
+
file_id = File.basename(file_id) if flatten_folder_structure
|
189
|
+
end
|
190
|
+
|
191
|
+
xml_file_params = { id: file_id }
|
192
|
+
|
193
|
+
if add_file_attributes
|
194
|
+
file_attributes_hash = obj.file_attributes || file_attributes[mimetype] || file_attributes['default'] || Assembly::FILE_ATTRIBUTES[mimetype] || Assembly::FILE_ATTRIBUTES['default']
|
195
|
+
xml_file_params.merge!(
|
196
|
+
preserve: file_attributes_hash[:preserve],
|
197
|
+
publish: file_attributes_hash[:publish],
|
198
|
+
shelve: file_attributes_hash[:shelve],
|
199
|
+
role: file_attributes_hash[:role]
|
200
|
+
)
|
201
|
+
xml_file_params.reject! { |_k, v| v.nil? || v.empty? }
|
202
|
+
end
|
203
|
+
|
204
|
+
if add_exif
|
205
|
+
xml_file_params[:mimetype] = mimetype
|
206
|
+
xml_file_params[:size] = obj.filesize
|
207
|
+
end
|
208
|
+
xml.file(xml_file_params) do
|
209
|
+
if add_exif # add exif info if the user requested it
|
210
|
+
xml.checksum(obj.sha1, type: 'sha1')
|
211
|
+
xml.checksum(obj.md5, type: 'md5')
|
212
|
+
xml.imageData(height: obj.exif.imageheight, width: obj.exif.imagewidth) if obj.image? # add image data for an image
|
213
|
+
elsif obj.provider_md5 || obj.provider_sha1 # if we did not add exif info, see if there are user supplied checksums to add
|
214
|
+
xml.checksum(obj.provider_sha1, type: 'sha1') if obj.provider_sha1
|
215
|
+
xml.checksum(obj.provider_md5, type: 'md5') if obj.provider_md5
|
216
|
+
end # add_exif
|
217
|
+
end
|
218
|
+
end # end resource_files.each
|
219
|
+
end
|
220
|
+
end # resources.each
|
220
221
|
end
|
221
|
-
|
222
|
-
return result
|
223
|
-
|
224
|
-
end # create_content_metadata
|
225
|
-
|
226
|
-
def self.is_special_dpg_folder?(folder)
|
227
|
-
SPECIAL_DPG_FOLDERS.include?(folder)
|
228
222
|
end
|
229
|
-
|
223
|
+
|
224
|
+
result = if include_root_xml == false
|
225
|
+
builder.doc.root.to_xml
|
226
|
+
else
|
227
|
+
builder.to_xml
|
228
|
+
end
|
229
|
+
|
230
|
+
result
|
231
|
+
end # create_content_metadata
|
232
|
+
|
233
|
+
def self.is_special_dpg_folder?(folder)
|
234
|
+
SPECIAL_DPG_FOLDERS.include?(folder)
|
235
|
+
end
|
230
236
|
end # class
|
231
|
-
|
232
237
|
end # module
|