Skip to content

Commit

Permalink
Merge pull request #27 from pulibrary/browse_everything_pending_uploads
Browse files Browse the repository at this point in the history
Add PendingUploads
  • Loading branch information
escowles authored Jul 31, 2017
2 parents e68dac2 + e903a91 commit 61f6182
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 53 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ GIT

GIT
remote: https://github.com/samvera-labs/valkyrie.git
revision: 91376007fac86b7cbe0f2a61e97d29af80d2d7f5
revision: 40ddfc6871b8e4cd7af459ad0f149a5be711035b
specs:
valkyrie (0.1.0)
active-fedora
Expand Down
6 changes: 6 additions & 0 deletions app/change_set_persisters/plum_change_set_persister.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def save_all(change_sets:)
end
end

def buffer_into_index(&block)
metadata_adapter.persister.buffer_into_index do |buffered_adapter|
with(metadata_adapter: buffered_adapter, &block)
end
end

def with(metadata_adapter:)
yield self.class.new(metadata_adapter: metadata_adapter, storage_adapter: storage_adapter)
end
Expand Down
1 change: 1 addition & 0 deletions app/change_sets/scanned_resource_change_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ScannedResourceChangeSet < Valkyrie::ChangeSet
# Virtual Attributes
property :refresh_remote_metadata, virtual: true, multiple: false
property :files, virtual: true, multiple: true, required: false
property :pending_uploads, multiple: true, required: false

validates_with ViewingDirectionValidator
validates_with ViewingHintValidator
Expand Down
26 changes: 24 additions & 2 deletions app/controllers/scanned_resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,30 @@ class ScannedResourcesController < ApplicationController
)

def browse_everything_files
resource = find_resource(params[:id])
BrowseEverythingIngestJob.perform_later(resource.id.to_s, self.class.to_s, params[:selected_files].to_unsafe_h)
change_set_persister.buffer_into_index do |buffered_changeset_persister|
change_set.validate(pending_uploads: change_set.pending_uploads + selected_files)
change_set.sync
buffered_changeset_persister.save(change_set: change_set)
end
BrowseEverythingIngestJob.perform_later(resource.id.to_s, self.class.to_s, selected_files.map(&:id).map(&:to_s))
redirect_to Valhalla::ContextualPath.new(child: resource, parent_id: nil).file_manager
end

def selected_file_params
params[:selected_files].to_unsafe_h
end

def selected_files
@selected_files ||= selected_file_params.values.map do |x|
PendingUpload.new(x.symbolize_keys.merge(id: SecureRandom.uuid, created_at: Time.current.utc.iso8601))
end
end

def resource
find_resource(params[:id])
end

def change_set
@change_set ||= change_set_class.new(resource)
end
end
8 changes: 6 additions & 2 deletions app/decorators/valkyrie/resource_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ class Valkyrie::ResourceDecorator < ApplicationDecorator
self.display_attributes = [:internal_resource, :created_at, :updated_at]

def created_at
super.strftime("%D %r %Z")
output = super
return if output.blank?
output.strftime("%D %r %Z")
end

def updated_at
super.strftime("%D %r %Z")
output = super
return if output.blank?
output.strftime("%D %r %Z")
end

def header
Expand Down
44 changes: 9 additions & 35 deletions app/jobs/browse_everything_ingest_job.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,18 @@
# frozen_string_literal: true
class BrowseEverythingIngestJob < ApplicationJob
def perform(resource_id, controller_scope_string, selected_files)
def perform(resource_id, controller_scope_string, pending_upload_ids)
controller_scope = controller_scope_string.constantize
change_set_persister = controller_scope.change_set_persister
change_set_class = controller_scope.change_set_class
resource = change_set_persister.metadata_adapter.query_service.find_by(id: Valkyrie::ID.new(resource_id))

change_set = change_set_class.new(resource)
selected_files = selected_files.values.map do |x|
BrowseEverythingFile.new(x.symbolize_keys)
end
change_set.validate(files: selected_files)
change_set_persister.save(change_set: change_set)
end

class BrowseEverythingFile
def initialize(file_name:, file_size:, url:)
@file_name = file_name
@file_size = file_size
@url = url
end

def original_filename
@file_name
end

def content_type
'text/plain'
end

def path
copied_file_name
end

def copied_file_name
return @copied_file_name if @copied_file_name
BrowseEverything::Retriever.new.download("file_name" => @file_name, "file_size" => @file_size, "url" => @url) do |filename, _retrieved, _total|
@copied_file_name = filename
pending_upload_ids = pending_upload_ids.map { |x| Valkyrie::Types::ID[x] }
change_set_persister.buffer_into_index do |buffered_changeset_persister|
resource = buffered_changeset_persister.metadata_adapter.query_service.find_by(id: Valkyrie::ID.new(resource_id))
change_set = change_set_class.new(resource)
selected_files = resource.pending_uploads.select do |pending_upload|
pending_upload_ids.include?(pending_upload.id)
end
@copied_file_name
change_set.validate(files: selected_files)
buffered_changeset_persister.save(change_set: change_set)
end
end
end
29 changes: 29 additions & 0 deletions app/model/pending_upload.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true
class PendingUpload < Valkyrie::Resource
attribute :id, Valkyrie::Types::ID.optional
attribute :file_name
attribute :url
attribute :file_size, Valkyrie::Types::Set.member(Valkyrie::Types::Coercible::Int)

def original_filename
@file_name.first
end

def content_type
'text/plain'
end

def path
copied_file_name
end

private

def copied_file_name
return @copied_file_name if @copied_file_name
BrowseEverything::Retriever.new.download("file_name" => file_name.first, "file_size" => file_size.first, "url" => url.first) do |filename, _retrieved, _total|
@copied_file_name = filename
end
@copied_file_name
end
end
1 change: 1 addition & 0 deletions app/models/plum_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,5 +342,6 @@ def schema
PlumSchema.schema.each do |field|
attribute field
end
attribute :pending_uploads, Valkyrie::Types::Array.member(PendingUpload)
end
end
2 changes: 1 addition & 1 deletion app/services/file_appender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def append_to(resource)
return resource if files.blank?
file_sets = build_file_sets || file_nodes
resource.member_ids = resource.member_ids + file_sets.map(&:id)
resource
resource.pending_uploads = (resource.pending_uploads || []) - files
end

def build_file_sets
Expand Down
18 changes: 18 additions & 0 deletions spec/controllers/scanned_resources_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,29 @@
end
it "uploads files" do
resource = FactoryGirl.create_for_repository(:scanned_resource)
# Ensure that indexing is always safe and done at the end.
allow(Valkyrie::MetadataAdapter.find(:index_solr)).to receive(:persister).and_return(Valkyrie::MetadataAdapter.find(:index_solr).persister)
allow(Valkyrie::MetadataAdapter.find(:index_solr).persister).to receive(:save)

post :browse_everything_files, params: { id: resource.id, selected_files: params["selected_files"] }
reloaded = adapter.query_service.find_by(id: resource.id)

expect(reloaded.member_ids.length).to eq 1
expect(reloaded.pending_uploads).to be_empty
expect(Valkyrie::MetadataAdapter.find(:index_solr).persister).not_to have_received(:save)
end
it "tracks pending uploads" do
resource = FactoryGirl.create_for_repository(:scanned_resource)
allow(BrowseEverythingIngestJob).to receive(:perform_later).and_return(true)

post :browse_everything_files, params: { id: resource.id, selected_files: params["selected_files"] }
reloaded = adapter.query_service.find_by(id: resource.id)

pending_upload = reloaded.pending_uploads[0]
expect(pending_upload.file_name).to eq [File.basename(file.path)]
expect(pending_upload.url).to eq ["file://#{file.path}"]
expect(pending_upload.file_size).to eq [file.size]
expect(pending_upload.created_at).not_to be_blank
end
end
end
Expand Down
18 changes: 6 additions & 12 deletions valhalla/app/controllers/concerns/valhalla/resource_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ def create
if @change_set.validate(resource_params)
@change_set.sync
obj = nil
persister.buffer_into_index do |buffered_adapter|
change_set_persister.with(metadata_adapter: buffered_adapter) do |buffered_changeset_persister|
obj = buffered_changeset_persister.save(change_set: @change_set)
end
change_set_persister.buffer_into_index do |buffered_changeset_persister|
obj = buffered_changeset_persister.save(change_set: @change_set)
end
redirect_to contextual_path(obj, @change_set).show
else
Expand All @@ -33,10 +31,8 @@ def create
def destroy
@change_set = change_set_class.new(find_resource(params[:id]))
authorize! :destroy, @change_set.resource
persister.buffer_into_index do |buffered_adapter|
change_set_persister.with(metadata_adapter: buffered_adapter) do |persist|
persist.delete(change_set: @change_set)
end
change_set_persister.buffer_into_index do |persist|
persist.delete(change_set: @change_set)
end
flash[:alert] = "Deleted #{@change_set.resource}"
redirect_to root_path
Expand All @@ -53,10 +49,8 @@ def update
if @change_set.validate(resource_params)
@change_set.sync
obj = nil
persister.buffer_into_index do |buffered_adapter|
change_set_persister.with(metadata_adapter: buffered_adapter) do |persist|
obj = persist.save(change_set: @change_set)
end
change_set_persister.buffer_into_index do |persist|
obj = persist.save(change_set: @change_set)
end
redirect_to contextual_path(obj, @change_set).show
else
Expand Down
21 changes: 21 additions & 0 deletions valhalla/app/views/valhalla/base/_pending_uploads.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<table class="table table-striped">
<caption>Pending Uploads</caption>
<thead>
<tr>
<th>File Name</th>
<th>Uploaded</th>
</tr>
</thead>
<tbody>
<% uploads.map(&:decorate).each do |pending_upload| %>
<tr>
<td>
<%= pending_upload.original_filename %>
</td>
<td>
<%= pending_upload.created_at %>
</td>
</tr>
<% end %>
</tbody>
</table>
7 changes: 7 additions & 0 deletions valhalla/app/views/valhalla/base/_server_upload.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@
<% end %>
</div>
</div>
<% if @change_set.pending_uploads.present? %>
<div class="col-md-9">
<div class="well well-sm">
<%= render "pending_uploads", uploads: @change_set.pending_uploads %>
</div>
</div>
<% end %>
</div>

0 comments on commit 61f6182

Please sign in to comment.