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

Add padding with zeros #171

Merged
merged 1 commit into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions lib/iiif_print/jobs/child_works_from_pdf_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def perform(parent_work, pdf_paths, user, admin_set_id, prior_pdfs)

# handle each input pdf
pdf_paths.each_with_index do |path, pdf_idx|
split_pdf(path, pdf_idx, user, prior_pdfs, child_model)
split_pdf(path, pdf_idx, user, prior_pdfs, child_model, number_of_pdfs: pdf_paths.size)
end

# Link newly created child works to the parent
Expand All @@ -34,12 +34,13 @@ def perform(parent_work, pdf_paths, user, admin_set_id, prior_pdfs)

private

def split_pdf(path, pdf_idx, user, prior_pdfs_count, child_model)
# rubocop:disable Metrics/ParameterLists
def split_pdf(path, pdf_idx, user, prior_pdfs_count, child_model, number_of_pdfs:)
image_files = @parent_work.iiif_print_config.pdf_splitter_service.new(path).to_a
return if image_files.blank?

pdf_sequence = pdf_idx + prior_pdfs_count
prepare_import_data(pdf_sequence, image_files, user)
prepare_import_data(pdf_sequence, image_files, user, number_of_pdfs: number_of_pdfs)

# submit the job to create all the child works for one PDF
# @param [User] user
Expand All @@ -59,13 +60,19 @@ def split_pdf(path, pdf_idx, user, prior_pdfs_count, child_model)
attributes.merge!(model: child_model.to_s).with_indifferent_access,
operation)
end
# rubocop:enable Metrics/ParameterLists

def prepare_import_data(pdf_sequence, image_files, user)
# rubocop:disable Metrics/MethodLength
def prepare_import_data(pdf_sequence, image_files, user, number_of_pdfs:)
@uploaded_files = []
@child_work_titles = {}
image_files.each_with_index do |image_path, idx|
file_id = create_uploaded_file(user, image_path).to_s
file_title = set_title(@parent_work.title.first, pdf_sequence, idx)
file_title = set_title(@parent_work.title.first,
pdf_sequence,
idx,
pdf_pad_zero: number_of_pdfs.size.to_s.length,
page_pad_zero: image_files.size.to_s.length)
@uploaded_files << file_id
@child_work_titles[file_id] = file_title
# save child work info to create the member relationships
Expand All @@ -74,6 +81,7 @@ def prepare_import_data(pdf_sequence, image_files, user)
child_order: sort_order(pdf_sequence, idx))
end
end
# rubocop:enable Metrics/MethodLength

def sort_order(pdf_sequence, idx)
"#{pdf_sequence} #{idx}"
Expand All @@ -87,9 +95,12 @@ def create_uploaded_file(user, path)
uf.id
end

def set_title(title, pdf_sequence, idx)
pdf_index = "Pdf Nbr #{pdf_sequence + 1}"
page_number = "Page #{idx + 1}"
def set_title(title, pdf_sequence, idx, pdf_pad_zero:, page_pad_zero:)
# TODO: prior_pdfs may cause issues in the future
pdf_nbr = (pdf_sequence + 1).to_s.rjust(pdf_pad_zero, "0")
page_nbr = (idx + 1).to_s.rjust(page_pad_zero, "0")
pdf_index = "Pdf #{pdf_nbr}"
page_number = "Page #{page_nbr}"
"#{title}: #{pdf_index}, #{page_number}"
end

Expand Down
9 changes: 7 additions & 2 deletions spec/iiif_print/jobs/child_works_from_pdf_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

RSpec.describe IiifPrint::Jobs::ChildWorksFromPdfJob do
# TODO: add specs
let(:work) { WorkWithIiifPrintConfig.new(title: ['required title']) }
let(:work) { WorkWithIiifPrintConfig.new(title: ['required title'], id: '123') }
let(:my_user) { build(:user) }
let(:uploaded_pdf_file) { create(:uploaded_pdf_file) }
let(:uploaded_file_ids) { [uploaded_pdf_file.id] }
Expand All @@ -15,7 +15,7 @@
let(:admin_set_id) { "admin_set/default" }
let(:prior_pdfs) { 0 }

let(:subject) { described_class.perform(work, paths, user, admin_set_id, prior_pdfs) }
let(:subject) { described_class.perform_now(work, pdf_paths, my_user, admin_set_id, prior_pdfs) }

describe '#perform' do
xit 'calls pdf splitter service with path' do
Expand All @@ -26,5 +26,10 @@

xit 'submits IiifPrint::Jobs::CreateRelationshipsJob' do
end

context 'with more than 9 pages' do
xit 'pads the page number with a zero' do
end
end
end
end