Module: Cloudphoto

Defined in:
lib/cloudphoto.rb,
lib/cloudphoto/cli.rb,
lib/cloudphoto/help.rb,
lib/cloudphoto/list.rb,
lib/cloudphoto/upload.rb,
lib/cloudphoto/aws/aws.rb,
lib/cloudphoto/version.rb,
lib/cloudphoto/download.rb

Defined Under Namespace

Modules: Aws, Help, List

Constant Summary collapse

ALBUMS_PREFIX =
".albums".freeze
VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.cli(command = "help", *args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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:) ⇒ Object



4
5
6
7
8
9
10
11
# 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) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/cloudphoto/help.rb', line 2

def help(command = nil)
  puts "  Usage: cloudphoto <command> [options]\n  Available commands:\n    download - download album from cloud storage\n    upload - upload photos to cloud storage\n    list - show ablum names in cloud storage\n  HELP\n\n  puts Help.send(\"help_\#{command}\") if command && respond_to?(command)\nend\n"

.list(album: nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# 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:) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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