From 7d235a0a77451db604f6093f453b38a7e38ffa5d Mon Sep 17 00:00:00 2001 From: Jeremy Friesen Date: Mon, 27 Mar 2023 13:57:35 -0400 Subject: [PATCH] Adding JPG strategy from IIIF Print This is an almost direct copy of IIIF Print's existing code Related to: - https://github.com/scientist-softserv/iiif_print/issues/194 - https://github.com/samvera-labs/bulkrax/issues/760 - https://github.com/scientist-softserv/utk-hyku/issues/343 - https://github.com/scientist-softserv/adventist-dl/issues/330 --- lib/space_stone/pdf_splitter/strategies.rb | 1 + .../pdf_splitter/strategies/base.rb | 4 +++ .../pdf_splitter/strategies/jpg.rb | 14 ++++++++ .../pdf_splitter/strategies/base_spec.rb | 11 ++++++ .../pdf_splitter/strategies/jpg_spec.rb | 34 +++++++++++++++++++ 5 files changed, 64 insertions(+) create mode 100644 lib/space_stone/pdf_splitter/strategies/jpg.rb create mode 100644 spec/space_stone/pdf_splitter/strategies/jpg_spec.rb diff --git a/lib/space_stone/pdf_splitter/strategies.rb b/lib/space_stone/pdf_splitter/strategies.rb index c0c92a4..4bbf7cd 100644 --- a/lib/space_stone/pdf_splitter/strategies.rb +++ b/lib/space_stone/pdf_splitter/strategies.rb @@ -9,3 +9,4 @@ module Strategies end require 'space_stone/pdf_splitter/strategies/base' +require 'space_stone/pdf_splitter/strategies/jpg' diff --git a/lib/space_stone/pdf_splitter/strategies/base.rb b/lib/space_stone/pdf_splitter/strategies/base.rb index 73df7f5..1bbd123 100644 --- a/lib/space_stone/pdf_splitter/strategies/base.rb +++ b/lib/space_stone/pdf_splitter/strategies/base.rb @@ -22,6 +22,8 @@ class Base # What is the image quality we're using? class_attribute :quality, default: nil + class_attribute :gsdevice, instance_accessor: false + ## # @param path [String] the path to the source PDF that we're processing. # @param baseid [String] used for creating a unique identifier @@ -93,6 +95,8 @@ def gsconvert # rubocop:enable Metrics/MethodLength def gsdevice + return self.class.gsdevice if self.class.gsdevice + raise NotImplementedError end diff --git a/lib/space_stone/pdf_splitter/strategies/jpg.rb b/lib/space_stone/pdf_splitter/strategies/jpg.rb new file mode 100644 index 0000000..5f8fb92 --- /dev/null +++ b/lib/space_stone/pdf_splitter/strategies/jpg.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module SpaceStone + module PdfSplitter + module Strategies + # The purpose of this class is to split the PDF into constituent jpg files. + class Jpg < Strategies::Base + self.image_extension = 'jpg' + self.quality = '50' + self.gsdevice = 'jpeg' + end + end + end +end diff --git a/spec/space_stone/pdf_splitter/strategies/base_spec.rb b/spec/space_stone/pdf_splitter/strategies/base_spec.rb index bc173f6..480b05b 100644 --- a/spec/space_stone/pdf_splitter/strategies/base_spec.rb +++ b/spec/space_stone/pdf_splitter/strategies/base_spec.rb @@ -6,4 +6,15 @@ # Becasue the described class is an abstract class, we want to verify its public interface. it { is_expected.to be_a(Enumerable) } + + describe '.gsdevice' do + subject { described_class.gsdevice } + it { is_expected.to be_nil } + end + + describe '#gsdevice' do + it "expects that you will have set .gsdevice in the subclass" do + expect { subject.send(:gsdevice) }.to raise_error(NotImplementedError) + end + end end diff --git a/spec/space_stone/pdf_splitter/strategies/jpg_spec.rb b/spec/space_stone/pdf_splitter/strategies/jpg_spec.rb new file mode 100644 index 0000000..34c941a --- /dev/null +++ b/spec/space_stone/pdf_splitter/strategies/jpg_spec.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe SpaceStone::PdfSplitter::Strategies::Jpg do + let(:path) { __FILE__ } + let(:pdf_pages_summary) { double(SpaceStone::PdfSplitter::PdfPagesSummary) } + let(:splitter) { described_class.new(path, pdf_pages_summary: pdf_pages_summary) } + + describe '.gsdevice' do + subject { described_class.gsdevice } + it { is_expected.to eq('jpeg') } + end + + describe '#gsdevice' do + subject { splitter.send(:gsdevice) } + it { is_expected.to eq('jpeg') } + end + + describe '#quality' do + subject { splitter.quality } + it { is_expected.to eq(described_class.quality) } + end + + describe '#quality?' do + subject { splitter.quality? } + it { is_expected.to be_truthy } + end + + describe '#image_extension' do + subject { splitter.image_extension } + it { is_expected.to eq('jpg') } + end +end