Skip to content

Commit

Permalink
Handle ingest of File.
Browse files Browse the repository at this point in the history
Allow use of File in FileSetActor create_content.
Don't depend on original_filename and content_type.
Use AddFileToFileSet to avoid extra calls to save from IngestFileJob.
Update yard docs to indicate File as valid param type.
  • Loading branch information
grosscol committed Nov 13, 2015
1 parent 60f5d2c commit c23c2dc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,18 @@ def create_metadata(upload_set_id, work, file_set_params = {})
# Simultaneously moving a preservation copy to the repostiory.
# TODO: create a job to monitor this directory and prune old files that
# have made it to the repo
# @param [ActionDigest::HTTP::UploadedFile, Tempfile] file the file uploaded by the user.
# @param [File, ActionDigest::HTTP::UploadedFile, Tempfile] file the file uploaded by the user.
def create_content(file)
file_set.label ||= file.original_filename
# Assign label and title of File Set is necessary.
file_set.label ||= file.respond_to?(:original_filename) ? file.original_filename : ::File.basename(file)
file_set.title = [file_set.label] if file_set.title.blank?

# Need to save the file_set in order to get an id
return false unless file_set.save

working_file = copy_file_to_working_directory(file, file_set.id)
IngestFileJob.perform_later(file_set.id, working_file, file.content_type, user.user_key)
mime_type = file.respond_to?(:content_type) ? file.content_type : nil
IngestFileJob.perform_later(file_set.id, working_file, mime_type, user.user_key)
make_derivative(file_set.id, working_file)
true
end
Expand Down Expand Up @@ -107,11 +111,12 @@ def make_derivative(file_set_id, working_file)
CharacterizeJob.perform_later(file_set_id, working_file)
end

# @param [ActionDispatch::Http::UploadedFile] file
# @param [File, ActionDispatch::Http::UploadedFile] file
# @param [String] id the identifer
# @return [String] path of the working file
def copy_file_to_working_directory(file, id)
copy_stream_to_working_directory(id, file.original_filename, file)
name = file.respond_to?(:original_filename) ? file.original_filename : ::File.basename(file)
copy_stream_to_working_directory(id, name, file)
end

# @param [FileSet] file_set the resource
Expand Down
20 changes: 15 additions & 5 deletions curation_concerns-models/app/jobs/ingest_file_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@ class IngestFileJob < ActiveJob::Base

def perform(file_set_id, filename, mime_type, user_key)
file_set = FileSet.find(file_set_id)
file = Hydra::Derivatives::IoDecorator.new(File.open(filename, "rb"))
file.mime_type = mime_type
file.original_name = File.basename(filename)

# Tell UploadFileToGenericFile service to skip versioning because versions will be minted by VersionCommitter (called by save_characterize_and_record_committer) when necessary
Hydra::Works::UploadFileToFileSet.call(file_set, file, versioning: false)
file = File.open(filename, "rb")
# If mime-type is known, wrap in an IO decorator
# Otherwise allow Hydra::Works service to determine mime_type
if mime_type
file = Hydra::Derivatives::IoDecorator.new(file)
file.mime_type = mime_type
file.original_name = File.basename(filename)
end

# Tell AddFileToFileSet service to skip versioning because versions will be minted by VersionCommitter (called by save_characterize_and_record_committer) when necessary
Hydra::Works::AddFileToFileSet.call(file_set, file, :original_file, versioning: false)

# Persist changes to the file_set
file_set.save!

# Do post file ingest actions
user = User.find_by_user_key(user_key)
CurationConcerns::VersioningService.create(file_set.original_file, user)
CurationConcerns.config.callback.run(:after_create_content, file_set, user)
Expand Down

0 comments on commit c23c2dc

Please sign in to comment.