Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
RDMS
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
researchdata
RDMS
Commits
10b787b3
Commit
10b787b3
authored
1 month ago
by
Gyan Gupta
Browse files
Options
Downloads
Patches
Plain Diff
fix image loding issue
parent
bb32eb36
Branches
Branches containing commit
No related tags found
2 merge requests
!379
Implements universal viewer
,
!346
Draft: Hyrax 5 - Upgrade hyrax to v5
Pipeline
#20808
failed
1 month ago
Stage: build
Stage: test
Stage: deploy
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
hyrax/config/initializers/riiif.rb
+101
-21
101 additions, 21 deletions
hyrax/config/initializers/riiif.rb
with
101 additions
and
21 deletions
hyrax/config/initializers/riiif.rb
+
101
−
21
View file @
10b787b3
...
...
@@ -9,43 +9,123 @@ Rails.application.reloader.to_prepare do
resp
=
Hyrax
::
SolrService
.
get
(
"id:
#{
fs_id
}
"
)
doc
=
resp
[
'response'
][
'docs'
].
first
raise
"Unable to find solr document with id:
#{
fs_id
}
"
unless
doc
{
height:
doc
[
'height_is'
],
width:
doc
[
'width_is'
],
format:
doc
[
'mime_type_ssi'
],
channels:
doc
[
'alpha_channels_ssi'
]
}
end
#
if Hyrax.config.use_valkyrie?
#
Riiif::Image.file_resolver = Hyrax::RiiifFileResolver.new
#
else
if
Hyrax
.
config
.
use_valkyrie?
Riiif
::
Image
.
file_resolver
=
Hyrax
::
RiiifFileResolver
.
new
else
Riiif
::
Image
.
file_resolver
=
Riiif
::
HttpFileResolver
.
new
Riiif
::
Image
.
file_resolver
.
id_to_uri
=
lambda
do
|
id
|
fs_id
=
id
fs_id
=
fs_id
.
gsub
(
/^\//
,
''
)
if
fs_id
.
start_with?
(
'/'
)
fs_id
=
fs_id
.
gsub
(
/^images\//
,
''
)
if
fs_id
.
start_with?
(
'images/'
)
fs_id
=
fs_id
.
split
(
'/'
)[
0
]
if
fs_id
.
include?
(
'/'
)
s3_file_url
(
fs_id
)
Hyrax
::
Base
.
id_to_uri
(
CGI
.
unescape
(
id
)).
tap
do
|
url
|
Rails
.
logger
.
info
"Riiif resolved
#{
id
}
to
#{
url
}
"
end
end
#
end
end
Riiif
::
Image
.
authorization_service
=
Hyrax
::
IiifAuthorizationService
Riiif
.
not_found_image
=
Rails
.
root
.
join
(
'app'
,
'assets'
,
'images'
,
'us_404.svg'
)
Riiif
.
unauthorized_image
=
Rails
.
root
.
join
(
'app'
,
'assets'
,
'images'
,
'us_404.svg'
)
Riiif
::
Engine
.
config
.
cache_duration
=
1
.
month
Riiif
::
Engine
.
config
.
cache_duration
=
1
.
day
end
module
Hyrax
# Adds file locking to Riiif::File
# @see RiiifFileResolver
Rails
.
application
.
reloader
.
to_prepare
do
class
RiiifFile
<
Riiif
::
File
include
ActiveSupport
::
Benchmarkable
attr_reader
:id
def
initialize
(
input_path
,
tempfile
=
nil
,
id
:)
super
(
input_path
,
tempfile
)
raise
(
ArgumentError
,
"must specify id"
)
if
id
.
blank?
@id
=
id
end
# Wrap extract in a read lock and benchmark it
def
extract
(
transformation
,
image_info
=
nil
)
Riiif
::
Image
.
file_resolver
.
file_locks
[
id
].
with_read_lock
do
benchmark
"RiiifFile extracted
#{
path
}
with
#{
transformation
.
to_params
}
"
,
level: :debug
do
super
end
end
end
private
def
logger
Hyrax
.
logger
end
end
end
class
RiiifFileResolver
include
ActiveSupport
::
Benchmarkable
# @param [String] id from iiif manifest
# @return [Riiif::File]
def
find
(
id
)
path
=
nil
file_locks
[
id
].
with_write_lock
do
path
=
build_path
(
id
)
path
=
build_path
(
id
,
force:
true
)
unless
File
.
exist?
(
path
)
# Ensures the file is locally available
end
RiiifFile
.
new
(
path
,
id:
id
)
end
# tracks individual file locks
# @see RiiifFile
# @return [Concurrent::Map<Concurrent::ReadWriteLock>]
def
file_locks
@file_locks
||=
Concurrent
::
Map
.
new
do
|
k
,
v
|
k
.
compute_if_absent
(
v
)
{
Concurrent
::
ReadWriteLock
.
new
}
end
end
private
def
build_path
(
id
,
force:
false
)
Riiif
::
Image
.
cache
.
fetch
(
"riiif:"
+
Digest
::
MD5
.
hexdigest
(
"path:
#{
id
}
"
),
expires_in:
Riiif
::
Image
.
expires_in
,
force:
force
)
do
load_file
(
id
)
end
end
def
load_file
(
id
)
benchmark
"RiiifFileResolver loaded
#{
id
}
"
,
level: :debug
do
fs_id
=
id
.
sub
(
/\A([^\/]*)\/.*/
,
'\1'
)
file_set
=
Hyrax
.
query_service
.
find_by
(
id:
fs_id
)
file_metadata
=
Hyrax
.
custom_queries
.
find_original_file
(
file_set:
file_set
)
load_s3_file
(
file_metadata
)
end
end
def
load_s3_file
(
file_metadata
)
shrine_protocol
=
Valkyrie
::
Storage
::
Shrine
::
PROTOCOL
file_key
=
file_metadata
.
file_identifier
.
to_s
.
gsub
(
shrine_protocol
,
''
)
disk_url
=
File
.
join
(
'tmp/network_files'
,
file_key
)
return
disk_url
if
File
.
exist?
(
disk_url
)
def
s3_file_url
(
fs_id
)
file_set
=
Hyrax
.
query_service
.
find_by
(
id:
fs_id
)
dirname
=
File
.
dirname
(
disk_url
)
FileUtils
.
mkdir_p
(
dirname
)
unless
File
.
directory?
(
dirname
)
s3
=
S3StorageService
.
new
s3
.
init_client
bucket_name
=
s3
.
sanitise_name
(
ENV
[
'S3_FILE_UPLOAD_BUCKET'
])
s3
.
get_content
(
bucket_name
,
file_key
,
disk_url
)
disk_url
end
file_metadata
=
Hyrax
.
custom_queries
.
find_original_file
(
file_set:
file_set
)
shrine_protocol
=
Valkyrie
::
Storage
::
Shrine
::
PROTOCOL
file_key
=
file_metadata
.
file_identifier
.
to_s
.
gsub
(
shrine_protocol
,
''
)
s3
=
S3StorageService
.
new
s3
.
init_client
bucket_name
=
s3
.
sanitise_name
(
ENV
[
'S3_FILE_UPLOAD_BUCKET'
])
s3
.
presigned_url_for_download
(
bucket_name
,
file_key
)
def
logger
Hyrax
.
logger
end
end
end
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment