bpl-derivatives 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +7 -0
  5. data/Gemfile +11 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +60 -0
  8. data/Rakefile +7 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/bpl-derivatives.gemspec +50 -0
  12. data/lib/bpl/derivatives.rb +74 -0
  13. data/lib/bpl/derivatives/audio_encoder.rb +27 -0
  14. data/lib/bpl/derivatives/config.rb +64 -0
  15. data/lib/bpl/derivatives/datastream_decorator.rb +31 -0
  16. data/lib/bpl/derivatives/input_object_decorator.rb +11 -0
  17. data/lib/bpl/derivatives/io_decorator.rb +15 -0
  18. data/lib/bpl/derivatives/logger.rb +25 -0
  19. data/lib/bpl/derivatives/output_object_decorator.rb +12 -0
  20. data/lib/bpl/derivatives/processors.rb +18 -0
  21. data/lib/bpl/derivatives/processors/audio.rb +5 -0
  22. data/lib/bpl/derivatives/processors/document.rb +45 -0
  23. data/lib/bpl/derivatives/processors/ffmpeg.rb +21 -0
  24. data/lib/bpl/derivatives/processors/image.rb +76 -0
  25. data/lib/bpl/derivatives/processors/jpeg2k_image.rb +127 -0
  26. data/lib/bpl/derivatives/processors/processor.rb +43 -0
  27. data/lib/bpl/derivatives/processors/raw_image.rb +37 -0
  28. data/lib/bpl/derivatives/processors/shell_based_processor.rb +103 -0
  29. data/lib/bpl/derivatives/processors/video.rb +10 -0
  30. data/lib/bpl/derivatives/processors/video/config.rb +66 -0
  31. data/lib/bpl/derivatives/processors/video/processor.rb +41 -0
  32. data/lib/bpl/derivatives/runners/audio_derivatives.rb +7 -0
  33. data/lib/bpl/derivatives/runners/document_derivatives.rb +7 -0
  34. data/lib/bpl/derivatives/runners/image_derivatives.rb +15 -0
  35. data/lib/bpl/derivatives/runners/jpeg2k_image_derivatives.rb +15 -0
  36. data/lib/bpl/derivatives/runners/pdf_derivatives.rb +4 -0
  37. data/lib/bpl/derivatives/runners/runner.rb +59 -0
  38. data/lib/bpl/derivatives/runners/video_derivatives.rb +7 -0
  39. data/lib/bpl/derivatives/services/capability_service.rb +17 -0
  40. data/lib/bpl/derivatives/services/mime_type_service.rb +14 -0
  41. data/lib/bpl/derivatives/services/persist_basic_contained_output_file_service.rb +73 -0
  42. data/lib/bpl/derivatives/services/persist_datastream_output_service.rb +30 -0
  43. data/lib/bpl/derivatives/services/persist_file_system_output_service.rb +31 -0
  44. data/lib/bpl/derivatives/services/persist_output_file_service.rb +24 -0
  45. data/lib/bpl/derivatives/services/retrieve_source_file_from_datastream_service.rb +12 -0
  46. data/lib/bpl/derivatives/services/retrieve_source_file_service.rb +13 -0
  47. data/lib/bpl/derivatives/services/tempfile_service.rb +65 -0
  48. data/lib/bpl/derivatives/version.rb +5 -0
  49. data/lib/color_profiles/license.txt +7 -0
  50. data/lib/color_profiles/sRGB_IEC61966-2-1_no_black_scaling.icc +0 -0
  51. metadata +238 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f604a101cd3cc44e245b293607b53448d859356c
4
+ data.tar.gz: 4a811f18df3c232cee8d428fdd2bd35ab5e218c6
5
+ SHA512:
6
+ metadata.gz: 6e0846cde9bfe0ebc88fc07dfdccf2eaf29a92b635bb6a29847e7bedda5b5e99bf79624b6cc170d752456cde2b27e3386bf4deb6054b57e3f08f03e8e7e5ee23
7
+ data.tar.gz: b2472b522793ce7b06662995fb5fb5aa4fced051f418f539552d8e23420be7482637486d873e66e4a2d50f10ad7d500e316fdcbe8988910e45fff860a2b5843d
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ #specific
10
+
11
+ Gemfile.lock
12
+ .ruby-version
13
+
14
+ # rspec failure tracking
15
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in bpl-derivatives.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem 'coveralls'
8
+ # gem 'rubocop', '~> 0.52.0', require: false
9
+ # gem 'rubocop-rspec', require: false
10
+ gem 'simplecov'
11
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 bbarberBPL
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # BPL::Derivatives
2
+
3
+ Modified version of [samvera/hydra-derivatives](https://github.com/samvera/hydra-derivatives) to be backwards compatible with Fedora Commons 3.8.1.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'bpl-derivatives'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install bpl-derivatives
20
+
21
+ ## Usage
22
+
23
+ Currently there is no implementation of the `ActiveTranscoder` features. Also the Audio and Video derivatives have not been thoroughly tested either.
24
+
25
+ The main purpose of this was to make newer versions of hydra-derivatives backwards compatible with Fedora 3.8.1 datastreams. Rather than fork this from Samvera I determined that the BPL's use case was out of their current scope and instead implemented this version. Also be aware this has not been tested with `ActiveFedora > 9`
26
+
27
+
28
+ Given an `ActiveFedora` Model Like So
29
+ ```ruby
30
+ class MyObject < ActiveFedora::Base
31
+ has_file_datastream 'masterFile' ..
32
+ end
33
+ ```
34
+
35
+ You can now add the following
36
+ ```ruby
37
+ class MyObject < ActiveFedora::Base
38
+ include BPL::Derivatives
39
+ ...
40
+ ...
41
+ def generate_derivatives
42
+ case self.masterFile.mimeType
43
+ when 'image/tif'
44
+ derivatize runner: :jpeg2k_image, source_datastream: "masterFile", outputs: [ {recipe: :default, dsid: 'myJp2kDatastream' } ]
45
+ derivatize runner: :image, source_datastream: "masterFile", outputs: [
46
+ { label: :thumb, size: "x800>", dsid: 'my800pxThumbDS', format: 'jpg' },
47
+ { label: :thumb, size: "300x300>", dsid: 'my300pxThumbDS', format: 'jpg' }
48
+ ]
49
+ when 'application/pdf'
50
+ derivatize runner: :image, source_datastream: "masterFile", outputs: [{label: :thumb, size: "300x300>", dsid: 'my300pxThumbDD', format: 'jpg', quality: 100, density: 200, layer: 0}] #Recommend passing in quality denisty and layer 0 for pdfs
51
+
52
+ ```
53
+
54
+ ## Contributing
55
+
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/boston-library/bpl-derivatives.
57
+
58
+ ## License
59
+
60
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ require 'fcrepo_wrapper'
6
+
7
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bpl/derivatives"
5
+ require 'awesome_print'
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 "pry"
14
+ Pry.start
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
@@ -0,0 +1,50 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "bpl/derivatives/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bpl-derivatives"
8
+ spec.version = BPL::Derivatives::VERSION
9
+ spec.authors = ["Benjamin Barber"]
10
+ spec.email = ["bbarber@bpl.org"]
11
+
12
+ spec.summary = %q{Dervivatives Processor borrowed heavily from Samvera hydra-derivatives}
13
+ spec.description = %q{Dervivatives Processor borrowed heavily from Samvera hydra-derivatives with the added benefit of being decoupled to ActiveFedora}
14
+ spec.homepage = "https://github.com/boston-library/bpl-derivatives"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+ #
22
+ # spec.metadata["homepage_uri"] = spec.homepage
23
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
24
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
25
+ # else
26
+ # raise "RubyGems 2.0 or newer is required to protect against " \
27
+ # "public gem pushes."
28
+ # end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
+ end
35
+ spec.bindir = "exe"
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ["lib"]
38
+
39
+ # spec.add_dependency 'active_encode', '~> 0.1'
40
+ spec.add_dependency 'activesupport', '>= 4.0', '< 6'
41
+ spec.add_dependency 'addressable', '~> 2.5'
42
+ spec.add_dependency 'mime-types', '> 2.0', '< 4.0'
43
+ spec.add_dependency 'mini_magick', '>= 3.2', '< 5'
44
+
45
+ spec.add_development_dependency "bundler", "~> 2.0"
46
+ spec.add_development_dependency "rake", "~> 10.0"
47
+ spec.add_development_dependency "rspec", "~> 3.8"
48
+ spec.add_development_dependency 'pry', '~> 0'
49
+ spec.add_development_dependency 'awesome_print', '~> 0'
50
+ end
@@ -0,0 +1,74 @@
1
+ require "bpl/derivatives/version"
2
+ require 'active_support/all'
3
+
4
+
5
+ module BPL
6
+ module Derivatives
7
+ extend ActiveSupport::Concern
8
+ extend ActiveSupport::Autoload
9
+
10
+ class Error < StandardError; end
11
+ class TimeoutError < ::Timeout::Error; end
12
+
13
+
14
+
15
+ autoload_under 'runners' do
16
+ autoload :AudioDerivatives
17
+ autoload :DocumentDerivatives
18
+ autoload :ImageDerivatives
19
+ autoload :Jpeg2kImageDerivatives
20
+ autoload :PdfDerivatives
21
+ autoload :Runner
22
+ autoload :VideoDerivatives
23
+ end
24
+
25
+
26
+ autoload :Config
27
+ autoload :Logger
28
+ autoload :Processors
29
+ autoload :TempfileService
30
+ autoload :IoDecorator
31
+ autoload :InputObjectDecorator
32
+ autoload :DatastreamDecorator
33
+ autoload :OutputObjectDecorator
34
+ autoload :AudioEncoder
35
+
36
+
37
+ autoload_under 'services' do
38
+ autoload :RetrieveSourceFileService
39
+ autoload :RetrieveSourceFileFromDatastreamService
40
+ autoload :PersistOutputFileService
41
+ autoload :PersistBasicContainedOutputFileService
42
+ autoload :PersistDatastreamOutputService
43
+ autoload :PersistFileSystemOutputService
44
+ autoload :TempfileService
45
+ autoload :MimeTypeService
46
+ end
47
+
48
+ @@config = Config.new
49
+ mattr_reader :config
50
+
51
+ def self.configure
52
+ yield(config) if block_given?
53
+ end
54
+
55
+ def self.reset_config!
56
+ @@config = Config.new
57
+ end
58
+
59
+ class_methods do
60
+ def config
61
+ @@config ||= Config.new
62
+ end
63
+ end
64
+
65
+ def config
66
+ self.class.config
67
+ end
68
+
69
+ def derivatize(opts = {})
70
+ runner = opts[:runner] ? opts.delete(:runner) : :image
71
+ "BPL::Derivatives::#{runner.to_s.classify}Derivatives".constantize.create(self, opts)
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,27 @@
1
+ require 'open3'
2
+
3
+ module BPL::Derivatives
4
+ class AudioEncoder
5
+ def initialize
6
+ @ffmpeg_output = Open3.capture3('ffmpeg -codecs').to_s
7
+ rescue StandardError
8
+ Logger.warn('Unable to find ffmpeg')
9
+ @ffmpeg_output = ""
10
+ end
11
+
12
+ def audio_encoder
13
+ audio_encoder = if fdk_aac?
14
+ 'libfdk_aac'
15
+ else
16
+ 'aac'
17
+ end
18
+ audio_encoder
19
+ end
20
+
21
+ private
22
+
23
+ def fdk_aac?
24
+ @ffmpeg_output.include?('--enable-libfdk-aac') || @ffmpeg_output.include?('--with-fdk-aac')
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,64 @@
1
+ require 'tmpdir'
2
+
3
+ module BPL::Derivatives
4
+ class Config
5
+ CONFIG_METHODS = %i[ffmpeg_path libreoffice_path temp_file_base fits_path kdu_compress_path
6
+ kdu_compress_recipes enable_ffmpeg source_file_service output_file_service active_encode_poll_time output_object_class].freeze
7
+
8
+ attr_accessor :ffmpeg_path, :libreoffice_path, :temp_file_base,
9
+ :source_file_service, :output_file_service, :fits_path,
10
+ :enable_ffmpeg, :kdu_compress_path, :kdu_compress_recipes,
11
+ :active_encode_poll_time, :base_logger, :output_object_class
12
+
13
+ alias_method :jp2_recipes=, :kdu_compress_recipes=
14
+
15
+ def initialize
16
+ @ffmpeg_path ||= 'ffmpeg'
17
+ @libreoffice_path ||= 'soffice'
18
+ @temp_file_base ||= Dir.tmpdir
19
+ @source_file_service ||= BPL::Derivatives::RetrieveSourceFileService
20
+ @output_file_service ||= BPL::Derivatives::PersistBasicContainedOutputFileService
21
+ @fits_path ||= 'fits.sh'
22
+ @enable_ffmpeg = nil
23
+ @kdu_compress_path ||= 'kdu_compress'
24
+ @kdu_compress_recipes ||= {
25
+ default_color: %(-rate 2.4,1.48331273,.91673033,.56657224,.35016049,.21641118,.13374944,.08266171
26
+ -jp2_space sRGB
27
+ -double_buffering 10
28
+ -num_threads 4
29
+ -no_weights
30
+ Clevels=6
31
+ Clayers=8
32
+ "Cblk={64,64}"
33
+ Cuse_sop=yes
34
+ Cuse_eph=yes
35
+ Corder=RPCL
36
+ ORGgen_plt=yes
37
+ ORGtparts=R
38
+ "Stiles={1024,1024}" ).gsub(/\s+/, " ").strip,
39
+ default_grey: %(-rate 2.4,1.48331273,.91673033,.56657224,.35016049,.21641118,.13374944,.08266171
40
+ -jp2_space sLUM
41
+ -double_buffering 10
42
+ -num_threads 4
43
+ -no_weights
44
+ Clevels=6
45
+ Clayers=8
46
+ "Cblk={64,64}"
47
+ Cuse_sop=yes
48
+ Cuse_eph=yes
49
+ Corder=RPCL
50
+ ORGgen_plt=yes
51
+ ORGtparts=R
52
+ "Stiles={1024,1024}" ).gsub(/\s+/, " ").strip
53
+ }
54
+ @active_encode_poll_time ||= 10
55
+ @base_logger ||= ::Logger.new(STDOUT)
56
+ @output_object_class ||= "ActiveFedora::File"
57
+ end
58
+
59
+ def enable_ffmpeg
60
+ return @enable_ffmpeg unless @enable_ffmpeg.nil?
61
+ @enable_ffmpeg = true
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,31 @@
1
+ require 'delegate'
2
+ module BPL::Derivatives
3
+ class DatastreamDecorator < InputObjectDecorator
4
+ attr_accessor :source_datastream
5
+ def initialize(object, source_datastream_name)
6
+ super(object)
7
+ self.source_datastream = object.send(source_datastream_name)
8
+ end
9
+
10
+ def content
11
+ self.source_datastream.content
12
+ end
13
+
14
+ def has_content?
15
+ self.source_datastream.has_content?
16
+ end
17
+
18
+ def filename_for_characterization
19
+ return source_datastream.filename_for_characterization if source_datastream.respond_to?(:filename_for_characterization)
20
+ self.default_filename_for_charaterization
21
+ end
22
+
23
+ protected
24
+ def default_filename_for_charaterization
25
+ registered_mime_type = BPL::Derivatives::MimeTypeService.type_lookup(source_datastream.mimeType)
26
+ BPL::Derivatives.base_logger.warn "Unable to find a registered mime type for #{source_datastream.mimeType.inspect} on #{pid}" unless registered_mime_type
27
+ extension = registered_mime_type ? ".#{registered_mime_type.extensions.first}" : ''
28
+ ["#{source_datastream.pid}-#{source_datastream.dsVersionID}", "#{extension}"]
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,11 @@
1
+ require 'delegate'
2
+
3
+ module BPL::Derivatives
4
+ class InputObjectDecorator < SimpleDelegator
5
+ attr_accessor :source_path
6
+ def initialize(object_or_filename)
7
+ self.source_path = object_or_filename if object_or_filename.is_a?(String)
8
+ super(object_or_filename)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ require 'delegate'
2
+
3
+ module BPL::Derivatives
4
+ class IoDecorator < SimpleDelegator
5
+ attr_accessor :mime_type, :original_filename
6
+ alias original_name original_filename
7
+ alias original_name= original_filename=
8
+
9
+ def initialize(file, mime_type = nil, original_filename = nil)
10
+ super(file)
11
+ self.mime_type = mime_type
12
+ self.original_filename = original_filename
13
+ end
14
+ end
15
+ end