-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PDF ingest upload Actor/Job implementation
Fixes #33. * Independent method to create child NewspaperPage works with TIFF for NewspaperIssueIngest, purpose of which is to be called by job triggered by Hyrax upload, which is already handling attachment of PDF to issue. * NewspaperIssueIngest only saves once and without validation for all child pages added. * CreateIssuePagesJob and NewspaperWorksUploadActor that calls it, injected into actor stack before OOTB Hyrax upload actor. * Admin set of NewspaperIssue work, or default admin set, saved to created child NewspaperPage objects on PDF upload. * Factories for upload (pdf), ability, user. * Add faraday as a testing/development dependency, useful for checking Fedora in RSpec tests. * Use :async not :sidekiq as presumed default in tests when disabling inline after temporary use. * get Travis-CI to install FITS
- Loading branch information
1 parent
017462d
commit 998498e
Showing
14 changed files
with
189 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
app/actors/newspaper_works/actors/newspaper_works_upload_actor.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
module NewspaperWorks | ||
module Actors | ||
class NewspaperWorksUploadActor < Hyrax::Actors::BaseActor | ||
def create(env) | ||
# If NewspaperIssue, we might have a PDF to split... | ||
handle_issue_upload(env) if env.curation_concern.class == NewspaperIssue | ||
# pass to next actor | ||
next_actor.create(env) | ||
end | ||
|
||
def update(env) | ||
handle_issue_upload(env) if env.curation_concern.class == NewspaperIssue | ||
# pass to next actor | ||
next_actor.update(env) | ||
end | ||
|
||
def default_admin_set | ||
AdminSet.find_or_create_default_admin_set_id | ||
end | ||
|
||
def queue_job(work, paths, user, admin_set_id) | ||
NewspaperWorks::CreateIssuePagesJob.perform_later( | ||
work, | ||
paths, | ||
user, | ||
admin_set_id | ||
) | ||
end | ||
|
||
def handle_issue_upload(env) | ||
return unless env.attributes.keys.include? 'uploaded_files' | ||
upload_ids = filter_file_ids(env.attributes['uploaded_files']) | ||
return if upload_ids.empty? | ||
uploads = Hyrax::UploadedFile.find(upload_ids) | ||
paths = uploads.map(&method(:upload_path)) | ||
paths = paths.select { |path| path.end_with?('.pdf') } | ||
return if paths.empty? | ||
work = env.curation_concern | ||
# must persist work to serialize job using it | ||
work.save!(validate: false) | ||
user = env.current_ability.current_user.user_key | ||
env.attributes[:admin_set_id] ||= default_admin_set | ||
queue_job(work, paths, user, env.attributes[:admin_set_id]) | ||
end | ||
|
||
# Given Hyrax::Upload object, return path to file on local filesystem | ||
def upload_path(upload) | ||
# so many layers to this onion: | ||
upload.file.file.file | ||
end | ||
|
||
def filter_file_ids(input) | ||
Array.wrap(input).select(&:present?) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module NewspaperWorks | ||
# Create child page works for issue | ||
class CreateIssuePagesJob < NewspaperWorks::ApplicationJob | ||
def perform(work, pdf_paths, user, admin_set_id) | ||
# we will need depositor set on work, if it is nil | ||
work.depositor ||= user | ||
# if we do not have admin_set_id yet, set it on the issue work: | ||
work.admin_set_id ||= admin_set_id | ||
# create child pages for each page within each PDF uploaded: | ||
pdf_paths.each do |path| | ||
adapter = NewspaperWorks::Ingest::NewspaperIssueIngest.new(work) | ||
adapter.load(path) | ||
adapter.create_child_pages | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
spec/actors/newspaper_works/actors/newspaper_works_upload_actor_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require 'faraday' | ||
require 'spec_helper' | ||
|
||
RSpec.describe NewspaperWorks::Actors::NewspaperWorksUploadActor do | ||
let(:issue) { build(:newspaper_issue) } | ||
let(:ability) { build(:ability) } | ||
let(:uploaded_pdf_file) { create(:uploaded_pdf_file) } | ||
let(:uploaded_file_ids) { [uploaded_pdf_file.id] } | ||
let(:attributes) { { uploaded_files: uploaded_file_ids } } | ||
let(:terminator) { Hyrax::Actors::Terminator.new } | ||
let(:env) { Hyrax::Actors::Environment.new(issue, ability, attributes) } | ||
let(:middleware) do | ||
stack = ActionDispatch::MiddlewareStack.new.tap do |middleware| | ||
middleware.use described_class | ||
end | ||
stack.build(terminator) | ||
end | ||
|
||
let(:uploaded_issue) do | ||
Rails.application.config.active_job.queue_adapter = :inline | ||
middleware.public_send(:create, env) | ||
Rails.application.config.active_job.queue_adapter = :async | ||
# return work, reloaded, because env.curation_concern will be stale after | ||
# running actor. | ||
NewspaperIssue.find(env.curation_concern.id) | ||
end | ||
|
||
describe "NewspaperIssue upload of PDF" do | ||
# we over-burden one example, because sadly RSpec does not do well with | ||
# shared state across examples (without use of `before(:all)` which is | ||
# mutually exclusive with `let` in practice, and ruffles rubocop's | ||
# overzealous sense of moral duty, speaking of which: | ||
# rubocop:disable RSpec/ExampleLength | ||
it "correctly creates child pages for issue" do | ||
pages = uploaded_issue.members.select { |w| w.class == NewspaperPage } | ||
expect(pages.size).to eq 2 | ||
pages.each_with_index do |page| | ||
# Page needs correct admin set: | ||
expect(page.admin_set_id).to eq 'admin_set/default' | ||
file_sets = page.members.select { |v| v.class == FileSet } | ||
expect(file_sets.size).to eq 1 | ||
files = file_sets[0].files | ||
url = files[0].uri.to_s | ||
# fetch the thing from Fedora Commons: | ||
response = Faraday.get(url) | ||
stored_size = response.body.length | ||
expect(stored_size).to be > 0 | ||
end | ||
end | ||
# rubocop:enable RSpec/ExampleLength | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FactoryBot.define do | ||
factory :ability do | ||
user | ||
initialize_with { new(user) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
FactoryBot.define do | ||
factory :uploaded_pdf_file, class: Hyrax::UploadedFile do | ||
initialize_with do | ||
base = File.join(NewspaperWorks::GEM_PATH, 'spec', 'fixtures', 'files') | ||
pdf_path = File.join(base, 'sample-color-newsletter.pdf') | ||
new(file: File.open(pdf_path), user: create(:user)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FactoryBot.define do | ||
factory :user do | ||
id "skroob" | ||
email "spaceballs@example.com" | ||
password "password_is_12345" | ||
initialize_with do | ||
User.find_or_create_by(id: id) do |user| | ||
user.email = email | ||
user.password = password | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters