Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #669 from scientist-softserv/i667-install-pdfjs
Browse files Browse the repository at this point in the history
I667 install pdfjs
  • Loading branch information
laritakr authored Nov 23, 2023
2 parents ee4a961 + d73a07b commit 5289af5
Show file tree
Hide file tree
Showing 207 changed files with 126,539 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,5 @@ gem 'iiif_print', git: 'https://github.com/scientist-softserv/iiif_print.git', b
gem 'order_already'
gem 'redis', '~> 4.0'
gem 'redlock', '~> 1.2.1'
# locking to 5.1.0 because it was throwing errors on 5.3.0 in development
gem 'simple_form', '5.1.0'
3 changes: 2 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ GEM
faraday (>= 0.17.5, < 3.a)
jwt (>= 1.5, < 3.0)
multi_json (~> 1.10)
simple_form (5.3.0)
simple_form (5.1.0)
actionpack (>= 5.2)
activemodel (>= 5.2)
simplecov (0.16.1)
Expand Down Expand Up @@ -1225,6 +1225,7 @@ DEPENDENCIES
secure_headers
selenium-webdriver
sentry-raven
simple_form (= 5.1.0)
simplecov
solr_wrapper (~> 2.0)
spring (~> 1.7)
Expand Down
19 changes: 19 additions & 0 deletions app/controllers/flipflop/strategies_controller_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

# OVERRIDE Flipflop v2.7.1 to allow for custom `Action` labels

module Flipflop
module StrategiesControllerDecorator
def enable?
values = StrategiesController::ENABLE_VALUES | ADDITIONAL_ENABLE_VALUES
values.include?(params[:commit])
end

def features_url(**_kargs)
hyrax.admin_features_path
end
ADDITIONAL_ENABLE_VALUES = FeaturesHelper::FEATURE_ACTION_LABELS.map { |_, v| v[:on] }.to_set.freeze
end
end

Flipflop::StrategiesController.prepend(Flipflop::StrategiesControllerDecorator)
15 changes: 15 additions & 0 deletions app/controllers/hyrax/admin/strategies_controller_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

# OVERRIDE Hyrax 2.9.6 to support Flipflop 2.7.1 features

module Hyrax
module Admin
module StrategiesControllerDecorator
def features_url(**_kargs)
hyrax.admin_features_path
end
end
end
end

Hyrax::Admin::StrategiesController.prepend(Hyrax::Admin::StrategiesControllerDecorator)
21 changes: 21 additions & 0 deletions app/helpers/features_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module FeaturesHelper
def status_for(feature)
status = @feature_set.status(feature)
label = @feature_set.status(feature) == :enabled ? :on : :off
FEATURE_ACTION_LABELS.fetch(feature.name.to_sym, label => status)[label]
end

def on(feature)
FEATURE_ACTION_LABELS[feature]&.[](:on) || 'on'
end

def off(feature)
FEATURE_ACTION_LABELS[feature]&.[](:off) || 'off'
end

FEATURE_ACTION_LABELS = {
default_pdf_viewer: { on: 'PDF.js', off: 'UV' }
}.freeze
end
48 changes: 48 additions & 0 deletions app/helpers/pdf_js_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

module PdfJsHelper
def pdf_js_url(file_set_presenter)
# assumes that the download path exists if the file set has been characterized
url = if file_set_presenter.mime_type
"/pdf.js/web/viewer.html?file=#{hyrax.download_path(file_set_presenter.id)}"
else
file_set_presenter.solr_document["import_url_ssim"].first
end

url + "##{query_param}"
end

def pdf_file_set_presenter(presenter)
# currently only supports one pdf per work, falls back to the first pdf file set in ordered members

# Commenting this line out because even PDFs that were not split will still have a representative media
# which will be used first in this logic, consider uncommenting once all imports finish
# representative_presenter(presenter) ||
external_pdf(presenter)
end

def external_pdf(presenter)
reader, archival = pdf_file_set_presenters(presenter.file_set_presenters).partition do |fsp|
fsp.solr_document["import_url_ssim"]&.first&.include? "READER"
end

reader.first || archival.first
end

def pdf_file_set_presenters(presenters)
presenters.select(&:pdf?).presence || presenters.select do |file_set_presenter|
file_set_presenter.solr_document["label_ssi"].downcase.end_with? ".pdf"
end
end

def representative_presenter(presenter)
presenter.file_set_presenters.find { |file_set_presenter| file_set_presenter.id == presenter.representative_id }
end

def query_param
search_params = current_search_session.try(:query_params) || {}
q = search_params['q'].presence || ''

"search=#{q}&phrase=true"
end
end
34 changes: 34 additions & 0 deletions app/indexers/hyrax/file_set_indexer_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

# OVERRIDE Hyrax 3.5.0 to add PDF text to solr document when using the default PDF viewer (PDF.js)

module Hyrax
module FileSetIndexerDecorator
def generate_solr_document
return super unless Flipflop.default_pdf_viewer?

super.tap do |solr_doc|
solr_doc['all_text_timv'] = solr_doc['all_text_tsimv'] = pdf_text
end
end

private

def pdf_text
return unless object.pdf?
return unless object.original_file&.content.is_a? String

text = IO.popen(['pdftotext', '-', '-'], 'r+b') do |pdftotext|
pdftotext.write(object.original_file.content)
pdftotext.close_write
pdftotext.read
end

text.tr("\n", ' ')
.squeeze(' ')
.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '') # remove non-UTF-8 characters
end
end
end

Hyrax::FileSetIndexer.prepend(Hyrax::FileSetIndexerDecorator)
17 changes: 17 additions & 0 deletions app/presenters/hyku/work_show_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ def video_embed_viewer
:video_embed_viewer
end

def pdf_viewer?
return unless Flipflop.default_pdf_viewer?
return unless file_set_presenters.any?(&:pdf?) || pdf_extension?

# If all of the member_presenters are file_set presenters, return true
# this also means that there are no child works
member_presenters.all? { |presenter| presenter.is_a? Hyrax::FileSetPresenter }
end

def pdf_extension?
file_set_presenters.any? { |fsp| fsp.label.downcase.end_with?('.pdf') }
end

def viewer?
iiif_viewer? || video_embed_viewer? || pdf_viewer?
end

private

def extract_from_identifier(rgx)
Expand Down
76 changes: 76 additions & 0 deletions app/views/hyrax/admin/features/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<%
# OVERRIDE Hyrax 2.9.6 to make the default PDF viewer switch more intuitive and some styling changes.
# Instead of saying on/off it says PDF.js/IIIF Print.
%>

<% provide :page_header do %>
<h1><span class="fa fa-wrench" aria-hidden="true"></span> <%= t('.header') %></h1>
<% end %>
<div class="flip row">
<div class="col-md-12">
<div class="panel">
<div class="panel-body">
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th></th>
<th class="name"><%= t('.feature') %></th>
<th class="description"><%= t('.description') %></th>
<th class="action"><%= t('.action') %></th>
</tr>
</thead>
<tbody>
<% @feature_set.grouped_features.each do |group, features| -%>
<% if @feature_set.grouped? -%>
<tr class="group">
<td></td>
<td class="name" colspan="<%= 2 + @feature_set.strategies.size -%>">
<h2>
<%= t(group ? group.name : :default, scope: [:flipflop, :groups], default: group ? group.title : nil) -%>
</h2>
</td>
</tr>
<% end -%>
<% features.each do |feature| %>
<tr data-feature="<%= feature.name.dasherize.parameterize %>">
<td class="status">
<span class="<%= @feature_set.status(feature) -%>"><%= status_for(feature) -%></span>
</td>
<td class="name"><%= feature.name.humanize -%></td>
<td class="description"><%= feature.description -%></td>

<% @feature_set.strategies.each do |strategy| -%>
<% next unless strategy.is_a? Flipflop::Strategies::ActiveRecordStrategy %>
<%# OVERRIDE to add min-width so Actions column can display all toggles on the same line %>
<%# adjust min-width as needed for future overrides %>
<td class="toggle" style="min-width: 110px;" data-strategy="<%= strategy.name.dasherize.parameterize %>">
<div class="toolbar">
<%= form_tag(hyrax.admin_feature_strategy_path(feature.key, strategy.key), method: :put) do -%>
<div class="btn-group">
<%# OVERRIDE to use helper, see FeaturesHelper %>
<%= submit_tag on(feature.name.to_sym),
type: "submit",
class: Flipflop.enabled?(feature.name.to_sym) ? 'active' : nil,
disabled: !strategy.switchable? -%>

<%# OVERRIDE to use helper, see FeaturesHelper %>
<%= submit_tag off(feature.name.to_sym),
type: "submit",
class: Flipflop.enabled?(feature.name.to_sym) ? nil : 'active',
disabled: !strategy.switchable? -%>
</div>
<% end -%>
</div>
</td>
<% end -%>
</tr>
<% end -%>
<% end -%>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
7 changes: 7 additions & 0 deletions app/views/hyrax/base/_pdf_js.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div class="viewer-wrapper">
<iframe id="pdf-viewer" aria-label="image view"
src="<%= pdf_js_url(file_set_presenter) %>"
allowfullscreen="true"
frameborder="0"
></iframe>
</div>
19 changes: 12 additions & 7 deletions app/views/hyrax/base/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@
<div class="panel-body">
<div class="row">
<%= render 'workflow_actions_widget', presenter: @presenter %>
<% if @presenter.universal_viewer? || @presenter.video_embed_viewer? %>
<% if @presenter.pdf_viewer? %>
<div class="col-sm-12">
<%= render 'pdf_js', file_set_presenter: pdf_file_set_presenter(@presenter) %>
</div>
<% elsif @presenter.iiif_viewer? || @presenter.video_embed_viewer? %>
<div class="col-sm-12">
<%= render 'representative_media', presenter: @presenter, viewer: true %>
</div>
<% end %>
<div class="col-sm-5 text-center">
<%= render 'representative_media', presenter: @presenter, viewer: false unless (@presenter.universal_viewer? || @presenter.video_embed_viewer?) %>
<%= render 'citations', presenter: @presenter %>
<%= render 'social_media' %>
</div>
<% else %>
<div class="col-sm-5 text-center">
<%= render 'representative_media', presenter: @presenter, viewer: false unless (@presenter.universal_viewer? || @presenter.video_embed_viewer?) %>
<%= render 'citations', presenter: @presenter %>
<%= render 'social_media' %>
</div>
<% end %>
<div class="col-sm-7">
<%= render 'metadata', presenter: @presenter %>
</div>
Expand Down
5 changes: 5 additions & 0 deletions config/features.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@
feature :show_share_button,
default: true,
description: "Shows the 'Share Your Work' button on the homepage."

# Flipflop.default_pdf_viewer? returning `true` means we use PDF.js and `false` means we use IIIF Print.
feature :default_pdf_viewer,
default: true,
description: "Choose PDF.js or Universal Viewer to render PDFs. UV uses IIIF Print and requires PDF spltting with OCR. Switching from PDF.js to the UV may require re-ingesting of the PDF."
end
Loading

0 comments on commit 5289af5

Please sign in to comment.