riiif 0.3.0 → 0.4.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 +4 -0
- data/.rubocop_todo.yml +153 -0
- data/Rakefile +10 -4
- data/app/controllers/riiif/images_controller.rb +33 -33
- data/app/models/riiif/file.rb +23 -23
- data/app/models/riiif/image.rb +8 -8
- data/lib/riiif/abstract_file_system_resolver.rb +26 -0
- data/lib/riiif/akubra_system_file_resolver.rb +7 -16
- data/lib/riiif/file_system_file_resolver.rb +8 -19
- data/lib/riiif/http_file_resolver.rb +27 -27
- data/lib/riiif/rails/routes.rb +1 -1
- data/lib/riiif/routes.rb +4 -4
- data/lib/riiif/version.rb +1 -1
- data/lib/riiif.rb +4 -3
- data/riiif.gemspec +16 -14
- data/spec/controllers/riiif/images_controller_spec.rb +132 -0
- data/spec/models/riiif/akubra_system_file_resolver_spec.rb +12 -0
- data/spec/models/riiif/file_system_file_resolver_spec.rb +41 -0
- data/spec/models/{http_file_resolver_spec.rb → riiif/http_file_resolver_spec.rb} +8 -8
- data/spec/models/{image_spec.rb → riiif/image_spec.rb} +47 -47
- data/spec/routing/redirect_spec.rb +4 -4
- data/spec/routing/resize_routes_spec.rb +28 -28
- data/spec/spec_helper.rb +1 -4
- data/spec/test_app_templates/lib/generators/test_app_generator.rb +1 -2
- metadata +49 -18
- data/spec/controllers/images_controller_spec.rb +0 -138
- data/spec/models/akubra_system_file_resolver_spec.rb +0 -13
- data/spec/models/file_system_file_resolver_spec.rb +0 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a1934121b4a1108f275c5a0331519770bb408eb
|
4
|
+
data.tar.gz: eb9d3f833d1e77fe46421a754a2d8365eb081102
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 848a8082a7768d0011f3e13dedebd36987aebbf5fef0c77103f0847539550066a84bac07f526689a80bd2b2d6fbd09b31c0eb9b19b732fec06dcfa3d4512a6d2
|
7
|
+
data.tar.gz: 58b1a338bf60c86fbba8b53fe7f340bab83d8a28bce65d7a84e2fab82bc2c79440340e498a1e52c248e7e5e5286dd3486a29638551298170a1d19d78e3958893
|
data/.rubocop.yml
ADDED
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
require: rubocop-rspec
|
2
|
+
# This configuration was generated by
|
3
|
+
# `rubocop --auto-gen-config`
|
4
|
+
# on 2016-07-11 08:45:16 -0500 using RuboCop version 0.41.2.
|
5
|
+
# The point is for the user to remove these configuration records
|
6
|
+
# one by one as the offenses are removed from the code base.
|
7
|
+
# Note that changes in the inspected code, or installation of new
|
8
|
+
# versions of RuboCop, may require this file to be generated again.
|
9
|
+
|
10
|
+
# Offense count: 9
|
11
|
+
# Configuration parameters: AllowSafeAssignment.
|
12
|
+
Lint/AssignmentInCondition:
|
13
|
+
Exclude:
|
14
|
+
- 'app/models/riiif/file.rb'
|
15
|
+
- 'app/models/riiif/image.rb'
|
16
|
+
- 'lib/riiif/http_file_resolver.rb'
|
17
|
+
|
18
|
+
# Offense count: 1
|
19
|
+
Lint/HandleExceptions:
|
20
|
+
Exclude:
|
21
|
+
- 'spec/models/riiif/http_file_resolver_spec.rb'
|
22
|
+
|
23
|
+
# Offense count: 1
|
24
|
+
Lint/UselessAssignment:
|
25
|
+
Exclude:
|
26
|
+
- 'app/models/riiif/file.rb'
|
27
|
+
|
28
|
+
# Offense count: 5
|
29
|
+
Metrics/AbcSize:
|
30
|
+
Max: 28
|
31
|
+
|
32
|
+
# Offense count: 1
|
33
|
+
Metrics/CyclomaticComplexity:
|
34
|
+
Max: 8
|
35
|
+
|
36
|
+
# Offense count: 65
|
37
|
+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
|
38
|
+
# URISchemes: http, https
|
39
|
+
Metrics/LineLength:
|
40
|
+
Max: 160
|
41
|
+
|
42
|
+
# Offense count: 7
|
43
|
+
# Configuration parameters: CountComments.
|
44
|
+
Metrics/MethodLength:
|
45
|
+
Max: 17
|
46
|
+
|
47
|
+
# Offense count: 1
|
48
|
+
Metrics/PerceivedComplexity:
|
49
|
+
Max: 9
|
50
|
+
|
51
|
+
# Offense count: 1
|
52
|
+
# Cop supports --auto-correct.
|
53
|
+
Performance/RedundantMatch:
|
54
|
+
Exclude:
|
55
|
+
- 'lib/riiif/file_system_file_resolver.rb'
|
56
|
+
|
57
|
+
# Offense count: 1
|
58
|
+
RSpec/DescribeClass:
|
59
|
+
Exclude:
|
60
|
+
- 'spec/routing/resize_routes_spec.rb'
|
61
|
+
|
62
|
+
# Offense count: 6
|
63
|
+
# Configuration parameters: Max.
|
64
|
+
RSpec/ExampleLength:
|
65
|
+
Exclude:
|
66
|
+
- 'spec/controllers/riiif/images_controller_spec.rb'
|
67
|
+
- 'spec/models/riiif/http_file_resolver_spec.rb'
|
68
|
+
|
69
|
+
# Offense count: 4
|
70
|
+
RSpec/VerifiedDoubles:
|
71
|
+
Exclude:
|
72
|
+
- 'spec/controllers/riiif/images_controller_spec.rb'
|
73
|
+
|
74
|
+
# Offense count: 3
|
75
|
+
# Cop supports --auto-correct.
|
76
|
+
# Configuration parameters: EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle, SupportedLastArgumentHashStyles.
|
77
|
+
# SupportedLastArgumentHashStyles: always_inspect, always_ignore, ignore_implicit, ignore_explicit
|
78
|
+
Style/AlignHash:
|
79
|
+
Exclude:
|
80
|
+
- 'lib/riiif/routes.rb'
|
81
|
+
|
82
|
+
# Offense count: 1
|
83
|
+
# Cop supports --auto-correct.
|
84
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
85
|
+
# SupportedStyles: braces, no_braces, context_dependent
|
86
|
+
Style/BracesAroundHashParameters:
|
87
|
+
Exclude:
|
88
|
+
- 'app/models/riiif/image.rb'
|
89
|
+
|
90
|
+
# Offense count: 1
|
91
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
92
|
+
# SupportedStyles: nested, compact
|
93
|
+
Style/ClassAndModuleChildren:
|
94
|
+
Exclude:
|
95
|
+
- 'lib/riiif/rails/routes.rb'
|
96
|
+
|
97
|
+
# Offense count: 1
|
98
|
+
# Cop supports --auto-correct.
|
99
|
+
Style/ColonMethodCall:
|
100
|
+
Exclude:
|
101
|
+
- 'lib/riiif/http_file_resolver.rb'
|
102
|
+
|
103
|
+
# Offense count: 13
|
104
|
+
Style/Documentation:
|
105
|
+
Exclude:
|
106
|
+
- 'spec/**/*'
|
107
|
+
- 'test/**/*'
|
108
|
+
- 'app/controllers/riiif/images_controller.rb'
|
109
|
+
- 'app/models/riiif/file.rb'
|
110
|
+
- 'app/models/riiif/image.rb'
|
111
|
+
- 'lib/riiif.rb'
|
112
|
+
- 'lib/riiif/akubra_system_file_resolver.rb'
|
113
|
+
- 'lib/riiif/engine.rb'
|
114
|
+
- 'lib/riiif/file_system_file_resolver.rb'
|
115
|
+
- 'lib/riiif/http_file_resolver.rb'
|
116
|
+
- 'lib/riiif/nil_authorization_service.rb'
|
117
|
+
- 'lib/riiif/rails/routes.rb'
|
118
|
+
- 'lib/riiif/routes.rb'
|
119
|
+
|
120
|
+
# Offense count: 3
|
121
|
+
# Cop supports --auto-correct.
|
122
|
+
Style/EmptyLines:
|
123
|
+
Exclude:
|
124
|
+
- 'app/models/riiif/image.rb'
|
125
|
+
- 'lib/riiif/file_system_file_resolver.rb'
|
126
|
+
- 'lib/riiif/http_file_resolver.rb'
|
127
|
+
|
128
|
+
# Offense count: 6
|
129
|
+
# Cop supports --auto-correct.
|
130
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
131
|
+
# SupportedStyles: empty_lines, no_empty_lines
|
132
|
+
Style/EmptyLinesAroundClassBody:
|
133
|
+
Exclude:
|
134
|
+
- 'app/models/riiif/file.rb'
|
135
|
+
- 'app/models/riiif/image.rb'
|
136
|
+
- 'lib/riiif/file_system_file_resolver.rb'
|
137
|
+
- 'lib/riiif/http_file_resolver.rb'
|
138
|
+
|
139
|
+
# Offense count: 3
|
140
|
+
# Cop supports --auto-correct.
|
141
|
+
Style/EmptyLinesAroundMethodBody:
|
142
|
+
Exclude:
|
143
|
+
- 'app/models/riiif/file.rb'
|
144
|
+
- 'app/models/riiif/image.rb'
|
145
|
+
- 'lib/riiif/rails/routes.rb'
|
146
|
+
|
147
|
+
# Offense count: 1
|
148
|
+
# Cop supports --auto-correct.
|
149
|
+
# Configuration parameters: AllowForAlignment, ForceEqualSignAlignment.
|
150
|
+
Style/ExtraSpacing:
|
151
|
+
Exclude:
|
152
|
+
- 'lib/riiif/routes.rb'
|
153
|
+
|
data/Rakefile
CHANGED
@@ -1,11 +1,17 @@
|
|
1
|
-
require
|
1
|
+
require 'bundler/gem_tasks'
|
2
2
|
require 'bundler/setup'
|
3
3
|
require 'rspec/core/rake_task'
|
4
|
+
require 'rubocop/rake_task'
|
4
5
|
RSpec::Core::RakeTask.new(:spec)
|
5
6
|
|
7
|
+
desc 'Run style checker'
|
8
|
+
RuboCop::RakeTask.new(:rubocop) do |task|
|
9
|
+
task.fail_on_error = true
|
10
|
+
end
|
11
|
+
|
6
12
|
require 'engine_cart/rake_task'
|
7
|
-
task ci: ['engine_cart:generate'] do
|
8
|
-
Rake::Task[
|
13
|
+
task ci: [:rubocop, 'engine_cart:generate'] do
|
14
|
+
Rake::Task['spec'].invoke
|
9
15
|
end
|
10
16
|
|
11
|
-
task default: :ci
|
17
|
+
task default: :ci
|
@@ -13,12 +13,12 @@ module Riiif
|
|
13
13
|
:ok
|
14
14
|
else
|
15
15
|
:unauthorized
|
16
|
-
|
16
|
+
end
|
17
17
|
rescue ImageNotFoundError
|
18
18
|
status = :not_found
|
19
19
|
end
|
20
20
|
|
21
|
-
image = not_found_image unless status == :ok
|
21
|
+
image = not_found_image unless status == :ok
|
22
22
|
|
23
23
|
data = image.render(params.permit(:region, :size, :rotation, :quality, :format))
|
24
24
|
headers['Access-Control-Allow-Origin'] = '*'
|
@@ -46,43 +46,43 @@ module Riiif
|
|
46
46
|
|
47
47
|
protected
|
48
48
|
|
49
|
-
|
49
|
+
LEVEL1 = 'http://iiif.io/api/image/2/level1.json'.freeze
|
50
50
|
|
51
|
-
|
52
|
-
|
53
|
-
|
51
|
+
def model
|
52
|
+
params.fetch(:model, 'riiif/image').camelize.constantize
|
53
|
+
end
|
54
54
|
|
55
|
-
|
56
|
-
|
57
|
-
|
55
|
+
def image_id
|
56
|
+
params[:id]
|
57
|
+
end
|
58
58
|
|
59
|
-
|
60
|
-
|
61
|
-
|
59
|
+
def authorization_service
|
60
|
+
model.authorization_service.new(self)
|
61
|
+
end
|
62
62
|
|
63
|
-
|
64
|
-
|
65
|
-
|
63
|
+
def link_header
|
64
|
+
response.headers['Link'] = "<#{LEVEL1}>;rel=\"profile\""
|
65
|
+
end
|
66
66
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
67
|
+
def not_found_image
|
68
|
+
raise "Not found image doesn't exist" unless Riiif.not_found_image
|
69
|
+
model.new(image_id, Riiif::File.new(Riiif.not_found_image))
|
70
|
+
end
|
71
71
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
72
|
+
CONTEXT = '@context'.freeze
|
73
|
+
CONTEXT_URI = 'http://iiif.io/api/image/2/context.json'.freeze
|
74
|
+
ID = '@id'.freeze
|
75
|
+
PROTOCOL = 'protocol'.freeze
|
76
|
+
PROTOCOL_URI = 'http://iiif.io/api/image'.freeze
|
77
|
+
PROFILE = 'profile'.freeze
|
78
78
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
79
|
+
def server_info
|
80
|
+
{
|
81
|
+
CONTEXT => CONTEXT_URI,
|
82
|
+
ID => request.original_url.sub('/info.json', ''),
|
83
|
+
PROTOCOL => PROTOCOL_URI,
|
84
|
+
PROFILE => [LEVEL1, 'formats' => model::OUTPUT_FORMATS]
|
85
|
+
}
|
86
|
+
end
|
87
87
|
end
|
88
88
|
end
|
data/app/models/riiif/file.rb
CHANGED
@@ -22,16 +22,16 @@ module Riiif
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
def self.create(ext = nil,
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
25
|
+
def self.create(ext = nil, _validate = true, &block)
|
26
|
+
|
27
|
+
tempfile = Tempfile.new(['mini_magick', ext.to_s.downcase])
|
28
|
+
tempfile.binmode
|
29
|
+
block.call(tempfile)
|
30
|
+
tempfile.close
|
31
|
+
image = new(tempfile.path, tempfile)
|
32
|
+
ensure
|
33
|
+
tempfile.close if tempfile
|
34
|
+
|
35
35
|
end
|
36
36
|
|
37
37
|
def extract(options)
|
@@ -57,24 +57,24 @@ module Riiif
|
|
57
57
|
def info
|
58
58
|
return @info if @info
|
59
59
|
height, width = execute("identify -format %hx%w #{path}").split('x')
|
60
|
-
@info = {height: Integer(height), width: Integer(width)}
|
60
|
+
@info = { height: Integer(height), width: Integer(width) }
|
61
61
|
end
|
62
62
|
|
63
63
|
private
|
64
64
|
|
65
65
|
def execute(command)
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
66
|
+
out = nil
|
67
|
+
benchmark("Riiif executed #{command}") do
|
68
|
+
stdin, stdout, stderr, wait_thr = popen3(command)
|
69
|
+
stdin.close
|
70
|
+
stdout.binmode
|
71
|
+
out = stdout.read
|
72
|
+
stdout.close
|
73
|
+
err = stderr.read
|
74
|
+
stderr.close
|
75
|
+
raise "Unable to execute command \"#{command}\"\n#{err}" unless wait_thr.value.success?
|
76
|
+
end
|
77
|
+
out
|
78
78
|
end
|
79
79
|
|
80
80
|
end
|
data/app/models/riiif/image.rb
CHANGED
@@ -17,13 +17,13 @@ module Riiif
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
OUTPUT_FORMATS = %
|
20
|
+
OUTPUT_FORMATS = %w(jpg png).freeze
|
21
21
|
|
22
22
|
attr_reader :id
|
23
23
|
|
24
24
|
# @param [String] id The identifier of the file to be looked up.
|
25
25
|
# @param [Riiif::File] file Optional: The Riiif::File to use instead of looking one up.
|
26
|
-
def initialize(id, file=nil)
|
26
|
+
def initialize(id, file = nil)
|
27
27
|
@id = id
|
28
28
|
@image = file if file.present?
|
29
29
|
end
|
@@ -50,7 +50,7 @@ module Riiif
|
|
50
50
|
|
51
51
|
|
52
52
|
def cache_key(id, options)
|
53
|
-
str = options.merge(id: id).delete_if {|_, v| v.nil?}.to_s
|
53
|
+
str = options.merge(id: id).delete_if { |_, v| v.nil? }.to_s
|
54
54
|
# Use a MD5 digest to ensure the keys aren't too long.
|
55
55
|
Digest::MD5.hexdigest(str)
|
56
56
|
end
|
@@ -70,9 +70,9 @@ module Riiif
|
|
70
70
|
end
|
71
71
|
|
72
72
|
def decode_quality(quality)
|
73
|
-
return if quality.nil? ||
|
74
|
-
return quality if
|
75
|
-
raise InvalidAttributeError, "Unsupported quality: #{quality}"
|
73
|
+
return if quality.nil? || %w(default color).include?(quality)
|
74
|
+
return quality if %w(bitonal grey).include?(quality)
|
75
|
+
raise InvalidAttributeError, "Unsupported quality: #{quality}"
|
76
76
|
end
|
77
77
|
|
78
78
|
def decode_rotation(rotation)
|
@@ -91,7 +91,7 @@ module Riiif
|
|
91
91
|
|
92
92
|
def decode_region(region)
|
93
93
|
if region.nil? || region == 'full'
|
94
|
-
nil
|
94
|
+
nil
|
95
95
|
elsif md = /^pct:(\d+),(\d+),(\d+),(\d+)$/.match(region)
|
96
96
|
# Image magic can't do percentage offsets, so we have to calculate it
|
97
97
|
offset_x = (info[:width] * Integer(md[1]).to_f / 100).round
|
@@ -110,7 +110,7 @@ module Riiif
|
|
110
110
|
elsif md = /^,(\d+)$/.match(size)
|
111
111
|
"x#{md[1]}"
|
112
112
|
elsif md = /^(\d+),$/.match(size)
|
113
|
-
|
113
|
+
(md[1]).to_s
|
114
114
|
elsif md = /^pct:(\d+(.\d+)?)$/.match(size)
|
115
115
|
"#{md[1]}%"
|
116
116
|
elsif md = /^(\d+),(\d+)$/.match(size)
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Riiif
|
2
|
+
class AbstractFileSystemResolver
|
3
|
+
attr_accessor :root, :base_path
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@root = ::File.expand_path(::File.join(::File.dirname(__FILE__), '../..'))
|
7
|
+
@base_path = ::File.join(root, 'spec/samples')
|
8
|
+
end
|
9
|
+
|
10
|
+
def find(id)
|
11
|
+
Riiif::File.new(path(id))
|
12
|
+
end
|
13
|
+
|
14
|
+
# @param [String] id the id to resolve
|
15
|
+
# @return the path of the file
|
16
|
+
def path(id)
|
17
|
+
search = pattern(id)
|
18
|
+
Dir.glob(search).first || raise(ImageNotFoundError, search)
|
19
|
+
end
|
20
|
+
|
21
|
+
def pattern(id)
|
22
|
+
raise NotImplementedError, "Implement `pattern(id)' in the concrete class"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
@@ -1,22 +1,13 @@
|
|
1
1
|
require 'digest'
|
2
2
|
require 'cgi'
|
3
3
|
module Riiif
|
4
|
-
class AkubraSystemFileResolver
|
4
|
+
class AkubraSystemFileResolver < AbstractFileSystemResolver
|
5
5
|
attr_accessor :pathroot, :imagetype, :akubraconfig
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
end
|
12
|
-
|
13
|
-
def find(id)
|
14
|
-
Riiif::File.new(path(id))
|
15
|
-
end
|
16
|
-
|
17
|
-
def path(id)
|
18
|
-
search = pattern(id)
|
19
|
-
Dir.glob(search).first || raise(ImageNotFoundError, search)
|
7
|
+
def initialize(pr = '/yourfedora/data/datastreamStore/', ir = 'jp2', ac = [[0, 2], [2, 2], [4, 1]])
|
8
|
+
@pathroot = pr
|
9
|
+
@imagetype = ir
|
10
|
+
@akubraconfig = ac
|
20
11
|
end
|
21
12
|
|
22
13
|
def pattern(id)
|
@@ -24,8 +15,8 @@ module Riiif
|
|
24
15
|
md5 = Digest::MD5.new
|
25
16
|
md5.update fullpid
|
26
17
|
digest = md5.hexdigest
|
27
|
-
directorystr =
|
28
|
-
@akubraconfig.each { |a| directorystr << digest[a[0],a[1]] <<
|
18
|
+
directorystr = ''
|
19
|
+
@akubraconfig.each { |a| directorystr << digest[a[0], a[1]] << '/' }
|
29
20
|
filename = CGI.escape(fullpid)
|
30
21
|
@pathroot + directorystr + filename
|
31
22
|
end
|
@@ -1,27 +1,16 @@
|
|
1
1
|
module Riiif
|
2
|
-
class FileSystemFileResolver
|
3
|
-
|
4
|
-
|
5
|
-
def initialize
|
6
|
-
@root = ::File.expand_path(::File.join(::File.dirname(__FILE__), '../..'))
|
7
|
-
@base_path = ::File.join(root, 'spec/samples')
|
8
|
-
@input_types = %W{png jpg tiff jp jp2}
|
9
|
-
end
|
10
|
-
|
11
|
-
def find(id)
|
12
|
-
Riiif::File.new(path(id))
|
13
|
-
end
|
14
|
-
|
15
|
-
def path(id)
|
16
|
-
search = pattern(id)
|
17
|
-
Dir.glob(search).first || raise(ImageNotFoundError, search)
|
18
|
-
end
|
19
|
-
|
2
|
+
class FileSystemFileResolver < AbstractFileSystemResolver
|
3
|
+
attr_writer :input_types
|
20
4
|
|
21
5
|
def pattern(id)
|
22
|
-
raise ArgumentError, "Invalid characters in id `#{id}`" unless
|
6
|
+
raise ArgumentError, "Invalid characters in id `#{id}`" unless %r{^[\w\-:]+$}.match(id)
|
23
7
|
::File.join(base_path, "#{id}.{#{input_types.join(',')}}")
|
24
8
|
end
|
25
9
|
|
10
|
+
private
|
11
|
+
|
12
|
+
def input_types
|
13
|
+
@input_types ||= %w(png jpg tiff jp jp2)
|
14
|
+
end
|
26
15
|
end
|
27
16
|
end
|
@@ -51,48 +51,48 @@ module Riiif
|
|
51
51
|
|
52
52
|
private
|
53
53
|
|
54
|
-
|
55
|
-
|
56
|
-
|
54
|
+
def ext
|
55
|
+
@ext ||= ::File.extname(URI.parse(url).path)
|
56
|
+
end
|
57
57
|
|
58
|
-
|
59
|
-
|
60
|
-
|
58
|
+
def file_name
|
59
|
+
@cache_file_name ||= ::File.join(cache_path, Digest::MD5.hexdigest(url) + ext.to_s)
|
60
|
+
end
|
61
61
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
62
|
+
def download_file
|
63
|
+
ensure_cache_path(::File.dirname(file_name))
|
64
|
+
benchmark("Riiif downloaded #{url}") do
|
65
|
+
::File.atomic_write(file_name, cache_path) do |local|
|
66
|
+
begin
|
67
|
+
Kernel::open(url, download_opts) do |remote|
|
68
|
+
while chunk = remote.read(8192)
|
69
|
+
local.write(chunk)
|
70
|
+
end
|
70
71
|
end
|
72
|
+
rescue OpenURI::HTTPError => e
|
73
|
+
raise ImageNotFoundError, e
|
71
74
|
end
|
72
|
-
rescue OpenURI::HTTPError => e
|
73
|
-
raise ImageNotFoundError.new(e)
|
74
75
|
end
|
75
76
|
end
|
76
77
|
end
|
77
|
-
end
|
78
78
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
79
|
+
# Get a hash of options for passing to Kernel::open
|
80
|
+
# This is the primary pathway for passing basic auth credentials
|
81
|
+
def download_opts
|
82
|
+
basic_auth_credentials ? { http_basic_authentication: basic_auth_credentials } : {}
|
83
|
+
end
|
84
84
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
85
|
+
# Make sure a file path's directories exist.
|
86
|
+
def ensure_cache_path(path)
|
87
|
+
FileUtils.makedirs(path) unless ::File.exist?(path)
|
88
|
+
end
|
89
89
|
end
|
90
90
|
|
91
91
|
|
92
92
|
protected
|
93
93
|
|
94
94
|
def uri(id)
|
95
|
-
raise
|
95
|
+
raise 'Must set the id_to_uri lambda' if id_to_uri.nil?
|
96
96
|
id_to_uri.call(id)
|
97
97
|
end
|
98
98
|
|
data/lib/riiif/rails/routes.rb
CHANGED
data/lib/riiif/routes.rb
CHANGED
@@ -8,7 +8,7 @@ module Riiif
|
|
8
8
|
@options = options
|
9
9
|
end
|
10
10
|
|
11
|
-
def add_routes
|
11
|
+
def add_routes(&blk)
|
12
12
|
@router.instance_exec(@options, &blk)
|
13
13
|
end
|
14
14
|
|
@@ -17,12 +17,12 @@ module Riiif
|
|
17
17
|
resource = options.fetch(:resource)
|
18
18
|
route_prefix = options[:at]
|
19
19
|
route_prefix ||= "/#{options[:as]}" if options[:as]
|
20
|
-
get "#{route_prefix}/:id/:region/:size/:rotation/:quality.:format" =>
|
20
|
+
get "#{route_prefix}/:id/:region/:size/:rotation/:quality.:format" => 'riiif/images#show',
|
21
21
|
constraints: { rotation: ALLOW_DOTS, size: SIZES },
|
22
22
|
defaults: { format: 'jpg', rotation: '0', region: 'full', quality: 'default', model: resource },
|
23
|
-
as: options[:as] ||
|
23
|
+
as: options[:as] || 'image'
|
24
24
|
|
25
|
-
get "#{route_prefix}/:id/info.json" =>
|
25
|
+
get "#{route_prefix}/:id/info.json" => 'riiif/images#info', defaults: { format: 'json', model: resource }, as: [options[:as], 'info'].compact.join('_')
|
26
26
|
|
27
27
|
# This doesn't work presently
|
28
28
|
# get "#{route_prefix}/:id", to: redirect("#{route_prefix}/%{id}/info.json")
|
data/lib/riiif/version.rb
CHANGED
data/lib/riiif.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'riiif/version'
|
2
|
+
require 'riiif/engine'
|
3
3
|
|
4
4
|
module Riiif
|
5
5
|
extend ActiveSupport::Autoload
|
6
6
|
autoload :Image
|
7
|
+
autoload :AbstractFileSystemResolver
|
7
8
|
autoload :FileSystemFileResolver
|
8
9
|
autoload :HTTPFileResolver
|
9
10
|
autoload :Routes
|
@@ -14,7 +15,7 @@ module Riiif
|
|
14
15
|
class InvalidAttributeError < Error; end
|
15
16
|
class ImageNotFoundError < Error
|
16
17
|
attr_reader :original_exception
|
17
|
-
def initialize(orig=nil)
|
18
|
+
def initialize(orig = nil)
|
18
19
|
@original_exception = orig
|
19
20
|
end
|
20
21
|
end
|