class EmbedXMP::ImageFile

Basic image file representation.

Public Class Methods

new(input_image) click to toggle source
# File lib/embed_xmp/image_file.rb, line 11
def initialize(input_image)
  @image_data = read_io_or_string(input_image)
end

Public Instance Methods

insert_into_file(offset, data) click to toggle source

Insert a chunk of data at file_offset from the start of file_data.

# File lib/embed_xmp/image_file.rb, line 29
def insert_into_file(offset, data)
  @image_data = @image_data[0..offset - 1] + data + @image_data[offset..-1]
end
read_io_or_string(thing) click to toggle source

Read from a readable IO object or a String (file path or a thing).

# File lib/embed_xmp/image_file.rb, line 16
def read_io_or_string(thing)
  if thing.respond_to?(:encoding)
    return IO.binread(thing) if File.exist?(thing)

    return thing
  elsif thing.respond_to?(:read)
    return IO.binread(thing)
  end

  raise 'FileNotAnIOorString'
end
write(output_file, data: nil) click to toggle source

Write image to file (or return if argument is nil).

# File lib/embed_xmp/image_file.rb, line 34
def write(output_file, data: nil)
  data = @image_data if data.nil?
  return data if output_file.nil?

  written_bytes = 0
  File.open(output_file, 'wb') do |file|
    written_bytes = file.write(data)
  end

  written_bytes > 0 && written_bytes == data.b.length
end