Module: FilePipeline::FileOperations::ExifManipulable
- Included in:
- ExifRedaction, ExifRestoration, VersionedFile
- Defined in:
- lib/file_pipeline/file_operations/exif_manipulable.rb
Overview
Mixin with methods to facilitate work with Exif metadata.
Class Method Summary collapse
-
.file_tags ⇒ Object
Returns an Array of tags to be ignored during comparison.
-
.parse_tag_error(message) ⇒ Object
:nodoc:.
-
.strip_path(str) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#delete_tags(out_file, tags_to_delete) ⇒ Object
Redacts (deletes) all
tags_to_deleteinout_file. -
#missing_exif_fields(this_exif, other_exif) ⇒ Object
Compares to hashes with exif tags and values and returns a hash with the tags that are present in
other_exifbut absent inthis_exif. -
#parse_exif_errors(errs, values) ⇒ Object
:args: error_messages, exif.
-
#read_exif(*files) ⇒ Object
Reads exif information for one or more
files. -
#write_exif(out_file, values) ⇒ Object
Writes
values(a hash with exif tags as keys) toout_file.
Class Method Details
.file_tags ⇒ Object
Returns an Array of tags to be ignored during comparison. These can be merged with an ExifManipulable including FileOperation’s options to skip tags (e.g. the skip_tags option in ExifRestoration).
12 13 14 15 |
# File 'lib/file_pipeline/file_operations/exif_manipulable.rb', line 12 def self. %w[FileSize FileModifyDate FileAccessDate FileInodeChangeDate FilePermissions FileType FileTypeExtension MIMEType] end |
.parse_tag_error(message) ⇒ Object
:nodoc:
17 18 19 20 |
# File 'lib/file_pipeline/file_operations/exif_manipulable.rb', line 17 def self.parse_tag_error() # :nodoc: /Warning: Sorry, (?<tag>\w+) is not writable/ .match() { |match| match[:tag] } end |
.strip_path(str) ⇒ Object
:nodoc:
22 23 24 |
# File 'lib/file_pipeline/file_operations/exif_manipulable.rb', line 22 def self.strip_path(str) # :nodoc: str.sub(%r{ - \/?(\/|[-:.]+|\w+)+\.\w+$}, '') end |
Instance Method Details
#delete_tags(out_file, tags_to_delete) ⇒ Object
Redacts (deletes) all tags_to_delete in out_file.
27 28 29 30 31 32 33 |
# File 'lib/file_pipeline/file_operations/exif_manipulable.rb', line 27 def (out_file, ) exif, = read_exif out_file values = exif.select { |tag| .include? tag } values_to_delete = values.transform_values { nil } log, = write_exif out_file, values_to_delete [log, values] end |
#missing_exif_fields(this_exif, other_exif) ⇒ Object
Compares to hashes with exif tags and values and returns a hash with the tags that are present in other_exif but absent in this_exif.
38 39 40 41 42 |
# File 'lib/file_pipeline/file_operations/exif_manipulable.rb', line 38 def missing_exif_fields(this_exif, other_exif) other_exif.delete_if do |tag, _| this_exif.key?(tag) || [:skip_tags].include?(tag) end end |
#parse_exif_errors(errs, values) ⇒ Object
:args: error_messages, exif
Takes an array of error_messages and a hash (exif) with tags and their values and parses errors where tags could not be written.
Returns an array with a log (any messages that were not errors where a tag could not be written) and data (a hash with any tags that could not be written, and the associated values from exif)
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/file_pipeline/file_operations/exif_manipulable.rb', line 52 def parse_exif_errors(errs, values) errs.each_with_object(LogDataParser.template) do |, info| errors, data = info tag = ExifManipulable.parse_tag_error() if tag data.store tag, values[tag] next info end errors << ExifManipulable.strip_path() info end end |
#read_exif(*files) ⇒ Object
Reads exif information for one or more files. Returns an array of hashes, one for each file, with tags and their values.
67 68 69 70 71 72 73 |
# File 'lib/file_pipeline/file_operations/exif_manipulable.rb', line 67 def read_exif(*files) file_paths = files.map { |f| File.(f) } results, errors = MultiExiftool.read file_paths raise 'Error reading Exif' unless errors.empty? results.map(&:to_h) end |
#write_exif(out_file, values) ⇒ Object
Writes values (a hash with exif tags as keys) to out_file.
Returns an array with a log (an array of messages - strings) and a hash with all tags/values that could not be written.
79 80 81 82 83 84 85 86 87 |
# File 'lib/file_pipeline/file_operations/exif_manipulable.rb', line 79 def write_exif(out_file, values) writer = MultiExiftool::Writer.new writer.filenames = Dir[File.(out_file)] writer.overwrite_original = true writer.values = values return if writer.write parse_exif_errors(writer.errors, values) end |