hydra-derivatives 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc081eda663412c44512d4b336226d4c88c08662
4
- data.tar.gz: 415d09b21e47399744d0a0b36fe9a0bab8459f8d
3
+ metadata.gz: e418eb7b224258c51b95ad6746452c9ffbf31676
4
+ data.tar.gz: ebef5a24146e2eff209b238dadb307a0b7e98c8d
5
5
  SHA512:
6
- metadata.gz: 3e413c0dfa5b730da9e0baf62cc52093ad495f5da10baf19da2ece4bcdad2c35c910cd8888b5254b6045bcad491d198246a638c23aec59a3786c27fe4469acba
7
- data.tar.gz: 89b012cb0cd07053335c9f70b464d7a3cf8cac855e27afcafddbb41c7868e7a98e2f37d618ae24fa1e70066fcaef81e09488d0fdb12a887f4099f68378bd92af
6
+ metadata.gz: a7e457990c5b95c834944b319f9355c6abc12870e733b652d4b05f898be684071f4e5595a56feaaf6c7348b90a6810e780f00096b206c30a02e7202d1133d1d3
7
+ data.tar.gz: 4c5a120d2287b7f315bf78a01c644a08efc70ba0733fa295c62cce4d15ba64ef4aa15f3991afd3ce7a0c9fbd312e952b11bd4b7feaa7f9ae7792bd45a7e078a3
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.0
1
+ 1.2.1
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.add_dependency 'hydra-file_characterization', '~> 0.3'
27
27
  spec.add_dependency 'mini_magick', '>= 3.2', '< 5'
28
28
  spec.add_dependency 'activesupport', '~> 4.0'
29
- spec.add_dependency 'mime-types', '~> 1'
29
+ spec.add_dependency 'mime-types', '>= 1'
30
30
  spec.add_dependency 'deprecation', '~> 0.1'
31
31
  end
32
32
 
@@ -19,6 +19,7 @@ module Hydra
19
19
  autoload :ShellBasedProcessor
20
20
  autoload :Jpeg2kImage
21
21
  autoload :Logger
22
+ autoload :TempfileService
22
23
 
23
24
  # Raised if the timout elapses
24
25
  class TimeoutError < ::Timeout::Error; end
@@ -11,7 +11,7 @@ module Hydra
11
11
 
12
12
  def encode_file(dest_path, file_suffix, mime_type, options = { })
13
13
  new_output = ''
14
- source_file.to_tempfile do |f|
14
+ Hydra::Derivatives::TempfileService.create(source_file) do |f|
15
15
  if mime_type == 'image/jpeg'
16
16
  temp_file = File.join(Hydra::Derivatives.temp_file_base, [File.basename(f.path).sub(File.extname(f.path), ''), 'pdf'].join('.'))
17
17
  new_output = File.join(Hydra::Derivatives.temp_file_base, [File.basename(temp_file).sub(File.extname(temp_file), ''), file_suffix].join('.'))
@@ -12,22 +12,6 @@ module Hydra
12
12
  end
13
13
  end
14
14
 
15
- # Restored method. It was required by other creatures
16
- def to_tempfile(&block)
17
- return unless has_content?
18
- Tempfile.open(filename_for_characterization) do |f|
19
- f.binmode
20
- if content.respond_to? :read
21
- f.write(content.read)
22
- else
23
- f.write(content)
24
- end
25
- content.rewind if content.respond_to? :rewind
26
- f.rewind
27
- yield(f)
28
- end
29
- end
30
-
31
15
  protected
32
16
 
33
17
  def filename_for_characterization
@@ -30,7 +30,7 @@ module Hydra
30
30
  if opts[:file_path]
31
31
  self.class.encode(opts[:file_path], recipe, output_file)
32
32
  else
33
- source_file.to_tempfile do |f|
33
+ Hydra::Derivatives::TempfileService.create(source_file) do |f|
34
34
  self.class.encode(f.path, recipe, output_file)
35
35
  end
36
36
  end
@@ -32,7 +32,7 @@ module Hydra
32
32
  def encode_file(dest_path, file_suffix, mime_type, options)
33
33
  out_file = nil
34
34
  output_file = Dir::Tmpname.create(['sufia', ".#{file_suffix}"], Hydra::Derivatives.temp_file_base){}
35
- source_file.to_tempfile do |f|
35
+ Hydra::Derivatives::TempfileService.create(source_file) do |f|
36
36
  self.class.encode(f.path, options, output_file)
37
37
  end
38
38
  out_file = File.open(output_file, "rb")
@@ -0,0 +1,47 @@
1
+ require 'mime/types'
2
+
3
+ module Hydra::Derivatives
4
+ class TempfileService
5
+
6
+ def self.create(file, &block)
7
+ new(file).tempfile(&block)
8
+ end
9
+
10
+ attr_reader :source_file
11
+
12
+ def initialize(source_file)
13
+ @source_file = source_file
14
+ end
15
+
16
+ def tempfile(&block)
17
+ if source_file.respond_to? :to_tempfile
18
+ source_file.send(:to_tempfile, &block)
19
+ elsif source_file.has_content?
20
+ default_tempfile(&block)
21
+ end
22
+ end
23
+
24
+ def default_tempfile(&block)
25
+ Tempfile.open(filename_for_characterization) do |f|
26
+ f.binmode
27
+ if source_file.content.respond_to? :read
28
+ f.write(source_file.content.read)
29
+ else
30
+ f.write(source_file.content)
31
+ end
32
+ source_file.content.rewind if source_file.content.respond_to? :rewind
33
+ f.rewind
34
+ yield(f)
35
+ end
36
+ end
37
+
38
+ def filename_for_characterization
39
+ registered_mime_type = MIME::Types[source_file.mime_type].first
40
+ Logger.warn "Unable to find a registered mime type for #{source_file.mime_type.inspect} on #{source_file.uri}" unless registered_mime_type
41
+ extension = registered_mime_type ? ".#{registered_mime_type.extensions.first}" : ''
42
+ version_id = 1 # TODO fixme
43
+ m = /\/([^\/]*)$/.match(source_file.uri)
44
+ ["#{m[1]}-#{version_id}", "#{extension}"]
45
+ end
46
+ end
47
+ end
@@ -35,11 +35,5 @@ module Hydra::Derivatives
35
35
  end
36
36
  end
37
37
  end
38
-
39
- context '#to_tempfile' do
40
- it 'has a method called to_tempfile' do
41
- expect { |b| subject.to_tempfile(&b) }.to yield_with_args(Tempfile)
42
- end
43
- end
44
38
  end
45
39
  end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hydra::Derivatives::TempfileService do
4
+ let(:class_with_metadata_extraction) do
5
+ Class.new do
6
+ attr_reader :content, :mime_type, :uri
7
+
8
+ def initialize(options = {})
9
+ @content = options.fetch(:content, '')
10
+ @mime_uype = options.fetch(:mime_type, nil)
11
+ @uri = 'http://example.com/pid/123'
12
+ end
13
+
14
+ def has_content?; content.present?; end
15
+ end
16
+ end
17
+
18
+ let(:initialization_options) { { content: 'abc', mime_type: 'text/plain' } }
19
+
20
+ let(:file) { class_with_metadata_extraction.new(initialization_options) }
21
+
22
+ subject { Hydra::Derivatives::TempfileService.new(file) }
23
+ context '#tempfile' do
24
+ it 'has a method called to_tempfile' do
25
+ expect { |b| subject.tempfile(&b) }.to yield_with_args(Tempfile)
26
+ end
27
+ end
28
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hydra-derivatives
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Coyne
@@ -132,14 +132,14 @@ dependencies:
132
132
  name: mime-types
133
133
  requirement: !ruby/object:Gem::Requirement
134
134
  requirements:
135
- - - "~>"
135
+ - - ">="
136
136
  - !ruby/object:Gem::Version
137
137
  version: '1'
138
138
  type: :runtime
139
139
  prerelease: false
140
140
  version_requirements: !ruby/object:Gem::Requirement
141
141
  requirements:
142
- - - "~>"
142
+ - - ">="
143
143
  - !ruby/object:Gem::Version
144
144
  version: '1'
145
145
  - !ruby/object:Gem::Dependency
@@ -190,6 +190,7 @@ files:
190
190
  - lib/hydra/derivatives/processor.rb
191
191
  - lib/hydra/derivatives/railtie.rb
192
192
  - lib/hydra/derivatives/shell_based_processor.rb
193
+ - lib/hydra/derivatives/tempfile_service.rb
193
194
  - lib/hydra/derivatives/video.rb
194
195
  - lib/hydra/derivatives/video/config.rb
195
196
  - lib/hydra/derivatives/video/processor.rb
@@ -208,6 +209,7 @@ files:
208
209
  - spec/fixtures/test.xlsx
209
210
  - spec/fixtures/world.png
210
211
  - spec/lib/hydra/derivatives/extract_metadata_spec.rb
212
+ - spec/services/tempfile_service_spec.rb
211
213
  - spec/spec_helper.rb
212
214
  - spec/units/config_spec.rb
213
215
  - spec/units/derivatives_spec.rb
@@ -258,6 +260,7 @@ test_files:
258
260
  - spec/fixtures/test.xlsx
259
261
  - spec/fixtures/world.png
260
262
  - spec/lib/hydra/derivatives/extract_metadata_spec.rb
263
+ - spec/services/tempfile_service_spec.rb
261
264
  - spec/spec_helper.rb
262
265
  - spec/units/config_spec.rb
263
266
  - spec/units/derivatives_spec.rb