module Cloudphoto

Constants

ALBUMS_PREFIX
VERSION

Public Class Methods

cli(command = "help", *args) click to toggle source
# File lib/cloudphoto/cli.rb, line 9
def cli(command = "help", *args)
  return Cloudphoto.help unless Cloudphoto.respond_to?(command)

  options = {}
  OptionParser.new do |opts|
    opts.on("-a", "--album ALBUM", "Album name") do |album|
      options[:album] = album
    end

    opts.on("-p", "--path path", "Path to upload/download photos to/from") do |path|
      options[:path] = path
    end

    opts.on("-h", "--help", "Print this help") do
      return Cloudphoto.help(command)
    end
  end.parse!(args)

  Cloudphoto.send(command, **options) if options
rescue ArgumentError => e
  puts e
  Cloudphoto.help(command)
end
download(album:, path:) click to toggle source
# File lib/cloudphoto/download.rb, line 4
def download(album:, path:)
  photo_keys = Cloudphoto::List.list_keys("#{ALBUMS_PREFIX}/#{album}/")

  photos = photo_keys.map do |key|
    Cloudphoto::Aws.download_object(key: key, to: "#{path}/#{File.basename(key)}")
  end

end
help(command = nil) click to toggle source
# File lib/cloudphoto/help.rb, line 2
  def help(command = nil)
    puts <<~HELP
    Usage: cloudphoto <command> [options]
    Available commands:
      download - download album from cloud storage
      upload - upload photos to cloud storage
      list - show ablum names in cloud storage
    HELP

    puts Help.send("help_#{command}") if command && respond_to?(command)
  end
list(album: nil) click to toggle source
# File lib/cloudphoto/list.rb, line 4
def list(album: nil)
  list = if album.nil?
    List.list_albums
  else
    List.list_photos(album)
  end

  puts "total #{list.size}"
  puts list
end
upload(path:, album:) click to toggle source
# File lib/cloudphoto/upload.rb, line 4
def upload(path:, album:)
  photos = Dir["#{path}/*.jpeg"] + Dir["#{path}/*.jpg"]
  result = photos.map do |photo|
    uploaded = Cloudphoto::Aws.upload_object(
      photo,
      to: "#{ALBUMS_PREFIX}/#{album}/#{File.basename(photo)}"
    )
    [photo, uploaded]
  end

  result.each do |path, success|
    puts "Upload #{path} #{success ? 'done' : 'fail'}"
  end
end

Private Instance Methods

cli(command = "help", *args) click to toggle source
# File lib/cloudphoto/cli.rb, line 9
def cli(command = "help", *args)
  return Cloudphoto.help unless Cloudphoto.respond_to?(command)

  options = {}
  OptionParser.new do |opts|
    opts.on("-a", "--album ALBUM", "Album name") do |album|
      options[:album] = album
    end

    opts.on("-p", "--path path", "Path to upload/download photos to/from") do |path|
      options[:path] = path
    end

    opts.on("-h", "--help", "Print this help") do
      return Cloudphoto.help(command)
    end
  end.parse!(args)

  Cloudphoto.send(command, **options) if options
rescue ArgumentError => e
  puts e
  Cloudphoto.help(command)
end
download(album:, path:) click to toggle source
# File lib/cloudphoto/download.rb, line 4
def download(album:, path:)
  photo_keys = Cloudphoto::List.list_keys("#{ALBUMS_PREFIX}/#{album}/")

  photos = photo_keys.map do |key|
    Cloudphoto::Aws.download_object(key: key, to: "#{path}/#{File.basename(key)}")
  end

end
help(command = nil) click to toggle source
# File lib/cloudphoto/help.rb, line 2
  def help(command = nil)
    puts <<~HELP
    Usage: cloudphoto <command> [options]
    Available commands:
      download - download album from cloud storage
      upload - upload photos to cloud storage
      list - show ablum names in cloud storage
    HELP

    puts Help.send("help_#{command}") if command && respond_to?(command)
  end
list(album: nil) click to toggle source
# File lib/cloudphoto/list.rb, line 4
def list(album: nil)
  list = if album.nil?
    List.list_albums
  else
    List.list_photos(album)
  end

  puts "total #{list.size}"
  puts list
end
upload(path:, album:) click to toggle source
# File lib/cloudphoto/upload.rb, line 4
def upload(path:, album:)
  photos = Dir["#{path}/*.jpeg"] + Dir["#{path}/*.jpg"]
  result = photos.map do |photo|
    uploaded = Cloudphoto::Aws.upload_object(
      photo,
      to: "#{ALBUMS_PREFIX}/#{album}/#{File.basename(photo)}"
    )
    [photo, uploaded]
  end

  result.each do |path, success|
    puts "Upload #{path} #{success ? 'done' : 'fail'}"
  end
end