Ruby gems


mini_exiftool

More Details

From PR that introduced this gem:

I'm storing the “other” metadata (metadata not automatically captured by Rails/ActiveRecord/ActiveStorage) in the new “other_metadata” company_file field.

I think I’ll keep the raw extraction value for each “metadata of interest” field that we collect, each stored as a key/value pair in the “other_metadata” object.

Additionally, I’ll create a new field with “Formatted” appended to the end of the field name for any field that we want to always try to have consistent data type & format for (e.g. latitude/longitude fields)

Example: latitude/longitude

  • HEIC, HEIF files

    • the lat/long values are in the keys GPSLatitude/GPSLongitude and in the “DMS” format (as shown in attached screenshots), but this format isn’t super usable (nor the same data type or format as our project lat/lon data), so I have a method to convert it to decimal degrees and store it in the GPSLatitudeFormatted/GPSLongitudeFormatted fields

  • …other file types

    • Maybe other file types also have latitude and longitude fields (maybe they are also called GPSLatitude/GPSLongitude, or maybe something else), possibly as different data types or formats as well…. we can always still depend on the GPSLatitudeFormatted/GPSLongitudeFormatted fields being present in the correct “decimal degrees” format (because we will handle the conversions of whatever latitude/longitude types we come across, adding new methods to the controller to convert them to decimal degrees format)

(Note: it seems that GPSLatitude/GPSLongitude seem to be consistent among file types so far, if they have latitude/longitude data present, but I think it’s a safe idea to keep the same sort of system — never modifying the actual metadata values (storing them as is) + creating a “xFormatted” field as needed for data type consistency/dependability)

# company_files_controller

# within :create action (within "if file_params[:attachments] ... end" block)
# read and log metadata (use this when wanting to see available metadata fields on an attachment)
file_params[:attachments].each do |attachment|
    read_and_log_metadata(attachment)
end


# private method
def read_and_log_metadata(attachment)
  begin
    file = attachment.tempfile
    puts "Reading file: #{file.path}"
    puts "Using Exiftool command: #{MiniExiftool.command}"
    if File.readable?(file.path)
      metadata = MiniExiftool.new(file.path)
      metadata.to_hash.each do |key, value|
        puts "#{key}: #{value}"
      end
      puts "Metadata errors: #{metadata.errors.join(', ')}" unless metadata.errors.empty?
    else
      puts "File not readable: #{file.path}"
    end
  rescue => e
    Rails.logger.error "Error reading metadata for file #{attachment.original_filename}: #{e}"
  end
end

mini_magick

  • requires imagemagick to be locally installed

Last updated