Class: Piwigo::Images::ImageUploader

Inherits:
Object
  • Object
show all
Defined in:
lib/piwigo/image_uploader.rb

Constant Summary collapse

MEGABYTE =
1024 * 1024

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil) ⇒ ImageUploader

Create a new ImageUploader



21
22
23
# File 'lib/piwigo/image_uploader.rb', line 21

def initialize(logger: nil)
  @logger = logger || Logger.new(STDOUT)
end

Instance Method Details

#upload(session, filename, name, album: nil) ⇒ Boolean

Add a photo to Piwigo

Parameters:

  • session (<Type>)
  • filename (<Type>)

    of the file to upload

  • name (<Type>)

    of the image

Returns:

  • (Boolean)

    True if successful



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/piwigo/image_uploader.rb', line 32

def upload(session, filename, name, album: nil)
  @session = session
  raise 'Invalid session' if @session.uri.nil?

  unless File.exist? filename
    @Logger.error "No such file: #{filename}"
    return false
  end
  chunk_num = 0
  image_content = File.binread(filename)
  original_sum = Digest::MD5.hexdigest(image_content)
  encoded_content = image_content
  io = StringIO.new(encoded_content)
  until io.eof?
    chunk = io.read(MEGABYTE)
    addChunk(chunk, original_sum, chunk_num)
    chunk_num += 1
  end
  attributes = sniff_attributes(filename, album: album)
  add(original_sum, filename, name, attributes: attributes)
end