Skip to content

Commit

Permalink
Merge pull request #3924 from samvera/rename_to_file_metadata
Browse files Browse the repository at this point in the history
Rename FileNode to FileMetadata
  • Loading branch information
elrayle authored Aug 27, 2019
2 parents b2d3d13 + a4ddcd1 commit 277e2ca
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 87 deletions.
12 changes: 6 additions & 6 deletions app/actors/hyrax/actors/file_actor.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'wings/services/file_node_builder'
require 'wings/services/file_metadata_builder'

module Hyrax
module Actors
Expand Down Expand Up @@ -55,7 +55,7 @@ def related_file
# Persists file as part of file_set and records a new version.
# Also spawns an async job to characterize and create derivatives.
# @param [JobIoWrapper] io the file to save in the repository, with mime_type and original_name
# @return [FileNode, FalseClass] the created file node on success, false on failure
# @return [FileMetadata, FalseClass] the created file node on success, false on failure
# @todo create a job to monitor the temp directory (or in a multi-worker system, directories!) to prune old files that have made it into the repo
def perform_ingest_file(io, use_valkyrie: false)
use_valkyrie ? perform_ingest_file_through_valkyrie(io) : perform_ingest_file_through_active_fedora(io)
Expand All @@ -76,12 +76,12 @@ def perform_ingest_file_through_active_fedora(io)

def perform_ingest_file_through_valkyrie(io)
# Skip versioning because versions will be minted by VersionCommitter as necessary during save_characterize_and_record_committer.
unsaved_node = io.to_file_node
unsaved_node = io.to_file_metadata
unsaved_node.use = relation
begin
saved_node = node_builder.create(io_wrapper: io, node: unsaved_node, file_set: file_set)
rescue StandardError => e # Handle error persisting file node
Rails.logger.error("Failed to save file_node through valkyrie: #{e.message}")
Rails.logger.error("Failed to save file_metadata through valkyrie: #{e.message}")
return false
end
Hyrax::VersioningService.create(saved_node, user)
Expand All @@ -91,8 +91,8 @@ def perform_ingest_file_through_valkyrie(io)
end

def node_builder
Wings::FileNodeBuilder.new(storage_adapter: Hyrax.storage_adapter,
persister: Hyrax.persister)
Wings::FileMetadataBuilder.new(storage_adapter: Hyrax.storage_adapter,
persister: Hyrax.persister)
end

def normalize_relation(relation, use_valkyrie: false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Hyrax
class FileNode < Valkyrie::Resource
class FileMetadata < Valkyrie::Resource
attribute :file_identifiers, ::Valkyrie::Types::Set # id of the file stored by the storage adapter
attribute :alternate_id, ::Valkyrie::Types::Set # id of the Hydra::PCDM::File which holds metadata and the file in ActiveFedora
attribute :file_set_id, ::Valkyrie::Types::ID # id of parent file set resource
Expand Down
12 changes: 6 additions & 6 deletions app/models/job_io_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ def file_actor
Hyrax::Actors::FileActor.new(file_set, relation.to_sym, user)
end

# @return [Hyrax::FileNode, FalseClass] the created file node on success, false on failure
# @return [Hyrax::FileMetadata, FalseClass] the created file node on success, false on failure
def ingest_file
file_actor.ingest_file(self)
end

def to_file_node
Hyrax::FileNode.new(label: original_name,
original_filename: original_name,
mime_type: mime_type,
use: [Valkyrie::Vocab::PCDMUse.OriginalFile])
def to_file_metadata
Hyrax::FileMetadata.new(label: original_name,
original_filename: original_name,
mime_type: mime_type,
use: [Valkyrie::Vocab::PCDMUse.OriginalFile])
end

# The magic that switches *once* between local filepath and CarrierWave file
Expand Down
16 changes: 8 additions & 8 deletions app/services/hyrax/versioning_service.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
require 'wings/services/file_node_builder'
require 'wings/services/file_metadata_builder'

module Hyrax
class VersioningService
class << self
# Make a version and record the version committer
# @param [ActiveFedora::File | Hyrax::FileNode] content
# @param [ActiveFedora::File | Hyrax::FileMetadata] content
# @param [User, String] user
def create(content, user = nil)
use_valkyrie = content.is_a? Hyrax::FileNode
use_valkyrie = content.is_a? Hyrax::FileMetadata
perform_create(content, user, use_valkyrie)
end

# @param [ActiveFedora::File | Hyrax::FileNode] content
# @param [ActiveFedora::File | Hyrax::FileMetadata] content
def latest_version_of(file)
file.versions.last
end

# Record the version committer of the last version
# @param [ActiveFedora::File | Hyrax::FileNode] content
# @param [ActiveFedora::File | Hyrax::FileMetadata] content
# @param [User, String] user_key
def record_committer(content, user_key)
user_key = user_key.user_key if user_key.respond_to?(:user_key)
version = latest_version_of(content)
return if version.nil?
version_id = content.is_a?(Hyrax::FileNode) ? version.id.to_s : version.uri
version_id = content.is_a?(Hyrax::FileMetadata) ? version.id.to_s : version.uri
Hyrax::VersionCommitter.create(version_id: version_id, committer_login: user_key)
end

# TODO: WINGS - Copied from valkyrie6 branch. Need to explore whether this is needed?
# # @param [FileSet] file_set
# # @param [Hyrax::FileNode] content
# # @param [Hyrax::FileMetadata] content
# # @param [String] revision_id
# # @param [User, String] user
# def restore_version(file_set, content, revision_id, user = nil)
# found_version = content.versions.find { |x| x.label == Array.wrap(revision_id) }
# return unless found_version
# node = Wings::FileNodeBuilder.new(storage_adapter: nil, persister: indexing_adapter.persister).attach_file_node(node: found_version, file_set: file_set)
# node = Wings::FileMetadataBuilder.new(storage_adapter: nil, persister: indexing_adapter.persister).attach_file_metadata(node: found_version, file_set: file_set)
# create(node, user)
# end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# TODO: This should live in Hyrax::AddFileNodeToFileSet service and should work for all valkyrie adapters.
# TODO: This should live in Hyrax::AddFileMetadataToFileSet service and should work for all valkyrie adapters.
module Wings::Works
class AddFileNodeToFileSet
class AddFileMetadataToFileSet
# Adds a file to the file_set
# @param file_set [Valkyrie::Resource] adding file to this file set
# @param file_node [Valkyrie::Resource] uploaded file and its metadata
# @param file_metadata [Hyrax::FileMetadata] uploaded file and its metadata
# @param update_existing [Boolean] whether to update an existing file if there is one. When set to true, performs a create_or_update.
# When set to false, always creates a new file within file_set.files.
# @param versioning [Boolean] whether to create new version entries (only applicable if file_node's +type+ corresponds to a versionable file)
# @param versioning [Boolean] whether to create new version entries (only applicable if file_metadata's +type+ corresponds to a versionable file)

def self.call(file_set:, file_node:, file:, update_existing: true, versioning: true)
def self.call(file_set:, file_metadata:, file:, update_existing: true, versioning: true)
raise ArgumentError, 'supplied object must be a file set' unless file_set.file_set?
raise ArgumentError, 'supplied object must be a file node' unless file_node.is_a? Hyrax::FileNode
raise ArgumentError, 'supplied object must be a file node' unless file_metadata.is_a? Hyrax::FileMetadata
raise ArgumentError, 'supplied file must respond to read' unless file.respond_to? :read

# TODO: required as a workaround for https://github.com/samvera/active_fedora/pull/858
Expand All @@ -19,19 +19,19 @@ def self.call(file_set:, file_node:, file:, update_existing: true, versioning: t
af_file_set = Wings::ActiveFedoraConverter.new(resource: file_set).convert

updater_class = versioning ? VersioningUpdater : Updater
updater = updater_class.new(af_file_set, file_node, file, update_existing)
updater = updater_class.new(af_file_set, file_metadata, file, update_existing)
status = updater.update
status ? file_set : false
end

class Updater
attr_reader :af_file_set, :file_node, :file, :current_file
attr_reader :af_file_set, :file_metadata, :file, :current_file

def initialize(af_file_set, file_node, file, update_existing)
def initialize(af_file_set, file_metadata, file, update_existing)
@af_file_set = af_file_set
@file_node = file_node
@file_metadata = file_metadata
@file = file
@current_file = find_or_create_file(association_type(file_node.use), update_existing)
@current_file = find_or_create_file(association_type(file_metadata.use), update_existing)
end

# @param [#read] file object that will be interrogated using the methods: :path, :original_name, :original_filename, :mime_type, :content_type
Expand Down Expand Up @@ -63,14 +63,14 @@ def persist
else
current_file.save
end
file_node.file_identifiers = [current_file.id.split('/')[-1]]
file_metadata.file_identifiers = [current_file.id.split('/')[-1]]
end

def attach_attributes
current_file.content = file
current_file.original_name = file_node.original_filename.first
current_file.mime_type = file_node.mime_type.first
set_metadata_node_values(current_file.metadata_node, attributes_from_file_node)
current_file.original_name = file_metadata.original_filename.first
current_file.mime_type = file_metadata.mime_type.first
set_metadata_node_values(current_file.metadata_node, attributes_from_file_metadata)
persist
end

Expand All @@ -83,11 +83,11 @@ def set_metadata_node_values(metadata_node, attributes)
metadata_node.original_checksum = attributes[:checksum]
metadata_node.file_size = attributes[:size]
metadata_node.file_name = attributes[:original_filename]
# TODO: May need to add others. FileNode class definition has the full list of attrs for metadata_node
# TODO: May need to add others. FileMetadata class definition has the full list of attrs for metadata_node
end

def attributes_from_file_node
attrs = file_node.attributes.dup
def attributes_from_file_metadata
attrs = file_metadata.attributes.dup
attrs.delete(:file_identifiers)
attrs.delete(:id)
attrs.delete(:alternate_ids)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true
require 'wings/hydra/works/services/add_file_node_to_file_set'
require 'wings/hydra/works/services/add_file_metadata_to_file_set'

# TODO: The file_node resource and the file_node_builder should be in Hyrax as they will be needed for non-wings valkyrie implementations too.
# TODO: The file_metadata resource and the file_metadata_builder should be in Hyrax as they will be needed for non-wings valkyrie implementations too.

module Wings
# Stores a file and an associated Hyrax::FileNode
class FileNodeBuilder
# Stores a file and an associated Hyrax::FileMetadata
class FileMetadataBuilder
include Hyrax::Noid

attr_reader :storage_adapter, :persister
Expand All @@ -15,9 +15,9 @@ def initialize(storage_adapter:, persister:)
end

# @param io_wrapper [JobIOWrapper] with details about the uploaded file
# @param node [Hyrax::FileNode] the metadata to represent the file
# @param node [Hyrax::FileMetadata] the metadata to represent the file
# @param file_set [Valkyrie::Resouce, Hydra::Works::FileSet] the associated FileSet # TODO: WINGS - Remove Hydra::Works::FileSet as a potential type when valkyrization is complete.
# @return [Hyrax::FileNode] the persisted metadata node that represents the file
# @return [Hyrax::FileMetadata] the persisted metadata node that represents the file
def create(io_wrapper:, node:, file_set:)
io_wrapper = build_file(io_wrapper, node.use)
file_set.save unless file_set.persisted?
Expand All @@ -29,17 +29,17 @@ def create(io_wrapper:, node:, file_set:)
resource: node,
resource_uri_transformer: Hyrax.config.resource_id_to_uri_transformer)
node.file_identifiers = [stored_file.id]
attach_file_node(node: node, file_set: file_set)
attach_file_metadata(node: node, file_set: file_set)
end

# @param node [Hyrax::FileNode] the metadata to represent the file
# @param node [Hyrax::FileMetadata] the metadata to represent the file
# @param file_set [Valkyrie::Resouce, Hydra::Works::FileSet] the associated FileSet # TODO: WINGS - Remove Hydra::Works::FileSet as a potential type when valkyrization is complete.
# @return [Hyrax::FileNode] the persisted metadata node that represents the file
def attach_file_node(node:, file_set:)
file_set.is_a?(::Valkyrie::Resource) ? attach_file_node_to_valkyrie_file_set(node, file_set) : node
# @return [Hyrax::FileMetadata] the persisted metadata node that represents the file
def attach_file_metadata(node:, file_set:)
file_set.is_a?(::Valkyrie::Resource) ? attach_file_metadata_to_valkyrie_file_set(node, file_set) : node
end

def attach_file_node_to_valkyrie_file_set(node, file_set)
def attach_file_metadata_to_valkyrie_file_set(node, file_set)
# This is for storage adapters other than wings. The wings storage adapter already attached the file to the file_set.
# This process is a no-op for wings. # TODO: WINGS - May need to verify this is a no-op for wings once file_set is passed in as a resource.
# TODO: WINGS - Need to test this against other adapters once they are available for use.
Expand Down
6 changes: 3 additions & 3 deletions lib/wings/valkyrie/persister.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ def initialize(adapter:)
# @param [Valkyrie::Resource] resource
# @return [Valkyrie::Resource] the persisted/updated resource
def save(resource:)
return save_file(file_node: resource) if resource.is_a? Hyrax::FileNode
return save_file(file_metadata: resource) if resource.is_a? Hyrax::FileMetadata
af_object = resource_factory.from_resource(resource: resource)
af_object.save!
resource_factory.to_resource(object: af_object)
rescue ActiveFedora::RecordInvalid => err
raise FailedSaveError.new(err.message, obj: af_object)
end

def save_file(file_node:)
def save_file(file_metadata:)
# This is a no-op when the file is being created or updated through the FileActor.
# There may be other scenarios where something needs to happen here.

# TODO: SKIP for now, but potentially need to...
# find existing af File
# convert file_node resource into af File metadata
# convert file_metadata resource into af File metadata
# save af File
end

Expand Down
8 changes: 4 additions & 4 deletions lib/wings/valkyrie/storage/active_fedora.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'wings/hydra/works/services/add_file_node_to_file_set'
require 'wings/hydra/works/services/add_file_metadata_to_file_set'

# frozen_string_literal: true
module Wings::Storage
Expand All @@ -12,15 +12,15 @@ class ActiveFedora < Valkyrie::Storage::Fedora
# @param extra_arguments [Hash] additional arguments which may be passed to other adapters
# @return [Valkyrie::StorageAdapter::StreamFile]
def upload(file:, original_filename:, resource:, resource_uri_transformer: default_resource_uri_transformer, **_extra_arguments) # rubocop:disable Lint/UnusedMethodArgument
Wings::Works::AddFileNodeToFileSet.call(file_set: file_set(resource), file_node: resource, file: file)
Wings::Works::AddFileMetadataToFileSet.call(file_set: file_set(resource), file_metadata: resource, file: file)
identifier = resource_uri_transformer.call(resource, base_url)
find_by(id: Valkyrie::ID.new(identifier.to_s.sub(/^.+\/\//, PROTOCOL)))
end

private

def file_set(file_node)
file_set_id = file_node.file_set_id
def file_set(file_metadata)
file_set_id = file_metadata.file_set_id
Hyrax.query_service.find_by(id: file_set_id)
end
end
Expand Down
Loading

0 comments on commit 277e2ca

Please sign in to comment.