/
generic_file.rb
246 lines (210 loc) · 8.42 KB
/
generic_file.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
require 'fileutils'
class GenericFile < ActiveFedora::Base
include Sufia::GenericFile
include Open3
include GenericFileConcerns::Ftp
include PbcoreExport
has_metadata 'ffprobe', type: FfmpegDatastream
has_metadata 'descMetadata', type: MediaAnnotationDatastream
has_file_datastream "content", type: FileContentDatastream, control_group: 'E'
delegate_to 'descMetadata', [:has_location, :program_title, :series_title, :item_title,
:episode_title, :has_event, :event_location, :production_location, :filming_event,
:production_event, :date_portrayed, :has_event_attributes, :source, :source_reference,
:rights_holder, :rights_summary, :release_date, :review_date,:aspect_ratio,
:frame_rate, :cc, :physical_location, :nola_code, :tape_id, :barcode, :notes,
:originating_department,
:creator_attributes, :contributor_attributes, :publisher_attributes,
:has_location_attributes, :description_attributes, :title_attributes,
:identifier_attributes]
delegate_to 'properties', [:unarranged, :applied_template_id], unique: true
attr_accessible :part_of, :contributor_attributes, :creator_attributes, :title_attributes,
:description_attributes, :publisher_attributes, :date_created, :date_uploaded,
:date_modified, :subject, :language, :rights, :resource_type, :identifier, :event_location,
:production_location, :date_portrayed, :source, :source_reference, :rights_holder,
:rights_summary, :release_date, :review_date, :aspect_ratio, :frame_rate, :cc,
:physical_location, :metadata_filename, :identifier_attributes, :notes,
:originating_department, :tag, :related_url, :permissions
before_destroy :remove_content
def metadata_filename
descMetadata.filename
end
def metadata_filename= val
descMetadata.filename= val
end
def [](key)
if key == :metadata_filename
metadata_filename
else
super
end
end
def []=(key, value)
if key == :metadata_filename
self.metadata_filename = value
else
super
end
end
def remove_content
content.run_callbacks :destroy
end
# overriding this method to initialize more complex RDF assertions (b-nodes)
def initialize_fields
publisher.build if publisher.empty?
contributor.build if contributor.empty?
creator.build if creator.empty?
identifier.build if identifier.empty?
description.build if description.empty?
super
end
def remove_blank_assertions
publisher.select { |p| p.name.first == '' && p.role.first == ''}.each(&:destroy)
contributor.select { |p| p.name.first == '' && p.role.first == ''}.each(&:destroy)
creator.select { |p| p.name.first == '' && p.role.first == ''}.each(&:destroy)
# events (filming events and production events specifically) must have locations
has_event.each do |event|
event.has_location.each do |location|
location.destroy if location.location_name.first == ''
end
end
description.select { |p| p.value.first == '' && p.type.first == ''}.each(&:destroy)
title.select { |p| p.value.first == '' && p.title_type.first == ''}.each(&:destroy)
super
end
# Overridden to write the file into the external store instead of a datastream
def add_file(file, dsid, file_name)
return add_external_file(file, dsid, file_name) if dsid == 'content'
super
end
def add_external_file(file, dsid, file_name)
path = File.join(directory, file_name)
if file.respond_to? :read
File.open(path, 'wb') do |f|
f.write file.read
end
else
# it's a filename.
FileUtils.move(file, path)
end
content.dsLocation = URI.escape("file://#{path}")
mime = MIME::Types.type_for(path).first
content.mimeType = mime.content_type if mime # mime can't always be detected by filename
title = self.title.build(value: file_name, title_type: 'Program')
self.label = file_name
save!
end
# Overridden to check that mxf actually has video tracks
def video?
if mime_type == 'application/mxf'
ffprobe.codec_type.any? {|type| type == 'video'}
else
super
end
end
# If the mxf has no video tracks return true
def audio?
if mime_type == 'application/mxf'
!ffprobe.codec_type.any? {|type| type == 'video'}
else
super
end
end
# Overridden to load the original image from an external datastream
def load_image_transformer
Magick::ImageList.new(content.filename)
end
def directory
dir_parts = noid.scan(/.{1,2}/)
dir = File.join(Rails.configuration.external_store_base, dir_parts)
puts "Making #{dir}"
FileUtils.mkdir_p(dir) unless Dir.exists?(dir)
dir
end
def log_events
TrackingEvent.where(pid: pid)
end
def views
log_events.where(event: 'view')
end
def downloads
log_events.where(event: 'download')
end
def terms_for_editing
terms_for_display -
[:part_of, :date_modified, :date_uploaded, :format] # I'm not sure why resource_type would be excluded#, :resource_type]
end
def terms_for_display
[ :part_of, :contributor, :creator, :title, :description, :event_location, :production_location,
:date_portrayed, :source, :source_reference, :rights_holder, :rights_summary, :publisher,
:date_created, :release_date, :review_date, :aspect_ratio, :frame_rate, :cc,
:physical_location, :identifier, :metadata_filename, :notes, :originating_department,
:date_uploaded, :date_modified, :subject, :language, :rights, :resource_type, :tag,
:related_url]
end
## Extract the metadata from the content datastream and record it in the characterization datastream
def characterize
fits_xml, ffprobe_xml = self.content.extract_metadata
self.characterization.ng_xml = fits_xml
fix_mxf_characterization!
self.ffprobe.ng_xml = ffprobe_xml
self.append_metadata
self.filename = self.label
save unless self.new_object?
end
# The present version of fits.sh (0.6.1) doesn't set a mime-type for MXF files
# this method rectifies that until a fixed version of fits.sh is released.
def fix_mxf_characterization!
self.characterization.mime_type = 'application/mxf' if mime_type == 'application/octet-stream' && format_label == ["Material Exchange Format"]
end
### Map location[].locationName -> based_near[]
def based_near
descMetadata.has_location #.map(&:location_name).flatten
end
# Necessary because parts of sufia call creator= with a string.
### Map creator[] -> creator[].name
# @param [Array,String] creator_properties a list of hashes with role and name or just names
def creator=(args)
unless args.is_a?(String) || args.is_a?(Array)
raise ArgumentError, "You must provide a string or an array. You provided #{args.inspect}"
end
args = Array(args)
if args.first.is_a?(String)
return if args == ['']
self.creator_attributes = [{name: args, role: "Uploader"}]
else
descMetadata.creator = args
end
end
# Necessary because parts of sufia call title= with a string.
### Map title[] -> title[].value
# @param [Array,String] title_properties a list of hashes with type and value
def title=(args)
unless args.is_a?(String) || args.is_a?(Array)
raise ArgumentError, "You must provide a string or an array. You provided #{args.inspect}"
end
args = Array(args)
if args.first.is_a?(String)
return if args == ['']
self.title_attributes = [{name: args, title_type: "Program"}]
else
descMetadata.title=args
end
end
# normally if you want to remove exising nested params you pass:
# {:_delete => true, :id => '_:g1231011230128'}
# since the editor doesn't know about that, we just delete
# all nested objects if they will be replaced.
def destroy_existing_nested_nodes(params)
self.creator.each { |c| c.destroy } if params[:creator_attributes]
self.contributor.each { |c| c.destroy } if params[:contributor_attributes]
self.producer.each { |c| c.destroy } if params[:producer_attributes]
self.publisher.each { |c| c.destroy } if params[:publisher_attributes]
self.title.each { |c| c.destroy } if params[:title_attributes]
self.event.each { |c| c.destroy } if params[:event_attributes]
self.description.each { |c| c.destroy } if params[:description_attributes]
end
def to_s
val = [program_title.first, series_title.first].compact.join(' | ')
val.empty? ? label : val
end
end