-
Notifications
You must be signed in to change notification settings - Fork 25
/
persist_basic_contained_output_file_service.rb
68 lines (63 loc) · 2.95 KB
/
persist_basic_contained_output_file_service.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# frozen_string_literal: true
module Hydra::Derivatives
# This Service is an implementation of the Hydra::Derivatives::PeristOutputFileService
# It supports basic contained files, which is the behavior associated with Fedora 3 file datastreams that were migrated to Fedora 4
# and, at the time that this class was authored, corresponds to the behavior of ActiveFedora::Base.attach_file and ActiveFedora::Base.attached_files
### Rename this
class PersistBasicContainedOutputFileService < PersistOutputFileService
# This method conforms to the signature of the .call method on Hydra::Derivatives::PeristOutputFileService
# * Persists the file within the object at destination_name
#
# NOTE: Uses basic containment. If you want to use direct containment (ie. with PCDM) you must use a different service (ie. Hydra::Works::AddFileToGenericFile Service)
#
# @param [IO,String] content the data to be persisted
# @param [Hash] directives directions which can be used to determine where to persist to.
# @option directives [String] url This can determine the path of the object.
# @option directives [String] format The file extension (e.g. 'jpg')
def self.call(content, directives)
file = io(content, directives)
remote_file = retrieve_remote_file(directives)
remote_file.content = file
remote_file.mime_type = determine_mime_type(file)
remote_file.original_name = determine_original_name(file)
remote_file.save
end
# Override this implementation if you need a remote file from a different location
# @return [ActiveFedora::File]
def self.retrieve_remote_file(directives)
uri = URI(directives.fetch(:url))
raise ArgumentError, "#{uri} is not an http uri" unless uri.scheme == 'http'
ActiveFedora::File.new(uri.to_s)
end
private_class_method :retrieve_remote_file
# @param [IO,String] content the data to be persisted
# @param [Hash] directives directions which can be used to determine where to persist to.
# @return [Hydra::Derivatives::IoDecorator]
def self.io(content, directives)
charset = charset(content) if directives[:format] == 'txt' || !directives.fetch(:binary, true)
Hydra::Derivatives::IoDecorator.new(content, new_mime_type(directives.fetch(:format), charset))
end
private_class_method :io
def self.new_mime_type(extension, charset = nil)
fmt = mime_format(extension)
fmt += "; charset=#{charset}" if charset
fmt
end
# Strings (from FullText) have encoding. Retrieve it
def self.charset(content)
content.encoding.name if content.respond_to?(:encoding)
end
private_class_method :charset
def self.mime_format(extension)
case extension
when 'mp4'
'video/mp4' # default is application/mp4
when 'webm'
'video/webm' # default is audio/webm
else
MIME::Types.type_for(extension).first.to_s
end
end
private_class_method :mime_format
end
end