Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

install pdf.js and fits through builder images #1119

Merged
merged 4 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
58 changes: 39 additions & 19 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
##
FROM ruby:2.7.8-slim-bullseye as spot-base

RUN apt update && \
apt install -y --no-install-recommends ca-certificates curl gnupg && \
RUN apt-get clean && \
apt-get update && \
apt-get install -y --no-install-recommends ca-certificates curl gnupg && \
mkdir -p /etc/apt/keyrings && \
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg && \
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list && \
apt update && apt install -y --no-install-recommends \
apt-get update -y && apt-get install -y --no-install-recommends \
awscli \
build-essential \
coreutils \
Expand Down Expand Up @@ -53,6 +54,7 @@ CMD ["bundle", "exec", "rails", "server", "-b", "ssl://0.0.0.0:443?key=/spot/tmp

HEALTHCHECK CMD curl -skf https://localhost/healthcheck/default || exit 1


##
# Target: spot-asset-builder
# !! This is a builder image, do not use !!
Expand All @@ -63,6 +65,18 @@ COPY . /spot

RUN SECRET_KEY_BASE="$(bin/rake secret)" FEDORA_URL="http://fakehost:8080/rest" bundle exec rake assets:precompile

##
# TARGET: pdfjs-installer
# !! This is a builder image. Not for general use !!
##
FROM alpine:3 as pdfjs-installer
ARG PDFJS_VERSION="4.0.379"
ENV PDFJS_VERSION="${PDFJS_VERSION}"
ADD https://github.com/mozilla/pdf.js/releases/download/v${PDFJS_VERSION}/pdfjs-${PDFJS_VERSION}-legacy-dist.zip /tmp/pdfjs.zip
RUN unzip -d /tmp/pdfjs /tmp/pdfjs.zip
COPY config/pdfjs/web/viewer.html /tmp/pdfjs/web
COPY config/pdfjs/web/lafayette-viewer-enhancements.js /tmp/pdfjs/web


##
# TARGET: spot-web-development
Expand All @@ -75,12 +89,14 @@ RUN bundle config unset with &&\
bundle config set with "development:test" && \
bundle install --jobs "$(nproc)"
COPY . /spot/
COPY --from=pdfjs-installer /tmp/pdfjs /spot/public/pdf

ENV RAILS_ENV=development

ENTRYPOINT ["/spot/bin/spot-dev-entrypoint.sh"]
CMD ["bundle", "exec", "rails", "server", "-b", "ssl://0.0.0.0:443?key=/spot/tmp/ssl/application.key&cert=/spot/tmp/ssl/application.crt"]


##
# TARGET: spot-web-production
# Precompiles assets for production
Expand All @@ -90,6 +106,23 @@ ENV RAILS_ENV=production
COPY . /spot
COPY --from=spot-asset-builder /spot/public/assets /spot/public/assets
COPY --from=spot-asset-builder /spot/public/uv /spot/public/uv
COPY --from=pdfjs-installer /tmp/pdfjs /spot/public/pdf


##
# Target: fits-installer
# !! This is a builder image, do not use directly !!
#
# @see https://github.com/harvard-lts/fits
# @see https://github.com/samvera/hyrax/blob/3.x-stable/Dockerfile#L59-L65
##
FROM alpine:3 as fits-installer
ARG FITS_VERSION="1.6.0"
ENV FITS_VERSION="${FITS_VERSION}"
ADD https://github.com/harvard-lts/fits/releases/download/${FITS_VERSION}/fits-${FITS_VERSION}.zip /tmp/fits.zip

RUN unzip -d /tmp/fits /tmp/fits.zip && \
chmod a+x /tmp/fits/fits.sh


##
Expand All @@ -102,7 +135,7 @@ ENV MALLOC_ARENA_MAX=2
# We don't need the entrypoint script to generate an SSL cert
ENV SKIP_SSL_CERT=true

RUN apt update && apt install -y --no-install-recommends \
RUN apt-get update && apt-get install -y --no-install-recommends \
bash \
ffmpeg \
ghostscript \
Expand All @@ -120,21 +153,7 @@ RUN ln -s /usr/bin/python3 /usr/bin/python
# @see https://stackoverflow.com/questions/52998331/imagemagick-security-policy-pdf-blocking-conversion#comment110879511_59193253
RUN sed -i '/disable ghostscript format types/,+6d' /etc/ImageMagick-6/policy.xml

# Install FITS based on Hyrax's Dockerfile
#
# @see https://github.com/harvard-lts/fits
# @see https://github.com/samvera/hyrax/blob/3.x-stable/Dockerfile#L59-L65
ARG FITS_VERSION="1.6.0"
ENV FITS_VERSION="${FITS_VERSION}"

# (from https://github.com/samvera/hyrax/blob/3.x-stable/Dockerfile#L59-L65)
RUN mkdir -p /usr/local/fits && \
cd /usr/local/fits && \
curl -Ls -o fits.zip "https://github.com/harvard-lts/fits/releases/download/${FITS_VERSION}/fits-${FITS_VERSION}.zip" && \
unzip fits.zip && \
rm fits.zip && \
chmod a+x /usr/local/fits/fits.sh

COPY --from=fits-installer /tmp/fits /usr/local/fits
ENV PATH="${PATH}:/usr/local/fits"

CMD ["bundle", "exec", "sidekiq"]
Expand All @@ -149,6 +168,7 @@ RUN bundle config unset with &&\
bundle install --jobs "$(nproc)"
COPY . /spot/


##
# Target: spot-worker-production
# Copies compiled assets for use in production.
Expand Down
22 changes: 17 additions & 5 deletions app/helpers/pdf_viewer_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,34 @@ module PdfViewerHelper
# @param [String] path
# @return [String] URL to the viewer
def viewer_url(path)
"/web/viewer.html?file=#{path}##{query_param}"
"/pdf/web/viewer.html?file=#{path}##{query_param}"
end

# The search param used by PDFjs to prepopulate the search bar
# The search/page params used by PDFjs to prepopulate the search bar
#
# @return [String]
def query_param
return unless search_query
"search=#{search_query}&phrase=true"
return unless search_query || page_param
qp = {}
qp[:page] = page_param if page_param

if search_query
qp[:search] = search_query
qp[:phrase] = true
end

URI.encode_www_form(qp)
end

private

def page_param
search&.query_params && search&.query_params&.try(:[], :page)
end

# @return [String]
def search_query
search&.query_params && search.query_params[:q]
search&.query_params && search.query_params&.try(:[], :q)
end

def search
Expand Down
2 changes: 2 additions & 0 deletions config/initializers/mime_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
Mime::Type.register "application/ld+json", :jsonld
Mime::Type.register "text/turtle", :ttl
Mime::Type.register 'application/x-endnote-refer', :endnote

Rack::Mime::MIME_TYPES['.mjs'] = 'application/javascript'
18 changes: 18 additions & 0 deletions config/pdfjs/web/lafayette-viewer-enhancements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// fills in the findInput search box with the
// provided query term (when present), only on load
document.addEventListener('pagerendered', function (ev) {
var findInput = document.getElementById('findInput')
var app = PDFViewerApplication

app.eventBus.on('pagesloaded', function () {
var findController = app.findController || {}
var state = findController.state || {}
var query = state.query || null

if (!query) {
return
}

findInput.value = query
})
})
Loading