Class: Smug::UploadCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/smugsync/upload.rb

Instance Method Summary collapse

Methods included from Utils

#method_missing, #status_message

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Smug::Utils

Instance Method Details

#execObject

current upload algorithm:

  1. get the list not uploaded stuff starting from current dir and below

  2. chdir to root (for simplicity)

  3. for each album

3.1. create it if it doesn't exist
3.2. upload files


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/smugsync/upload.rb', line 17

def exec
    optparser = Trollop::Parser.new do
        banner <<-END
Usage: smug upload [<options>]
Upload files to SmugMug.

Options:
        END
        opt :timeout,
            "Timeout to upload one file in seconds",
            :short => :t,
            :default => 24*3600
    end

    opts = Trollop::with_standard_exception_handling(optparser) do
        optparser.parse(ARGV)
    end

    authenticate

    status = StatusCommand.new.current_dir_status
    Dir::chdir(Config::root_dir)

    modified_albums = Set.new

    status.each_with_index do |album_status, i|
        if album_status[:status] == :not_uploaded
            # create album
            puts "Creating album #{album_status[:album]}"
            result = smugmug_albums_create(
                "Title" => album_status[:album]
            )
            raise "Cannot create album" unless result["stat"] == "ok"

            # refresh cache for the newly created album
            album = smugmug_albums_getInfo(
                :AlbumID => result["Album"]["id"],
                :AlbumKey => result["Album"]["Key"]
            )["Album"]

            FetchCommand.new.refresh_cache([album])
            @albums = nil # TODO: hack to force reparsing of cache file
        elsif album_status[:status] == :not_downloaded
            next
        end

        album = albums.find { |a| a["Title"] == album_status[:album] }

        files_to_upload = album_status[:images].find_all { |i| i[:status] == :not_uploaded }.map { |i| "#{album["Title"]}/#{i[:image]}" }

        File.open("upload.log", "w+") { |f| f.puts "start" }
        num_files = files_to_upload.length
        files_to_upload.each_with_index do |filename, i|
            modified_albums << album

            begin
                with_timeout(opts[:timeout]) do
                    puts "Uploading #{filename} (#{Time.now.to_s}) (#{i}/#{num_files})"

                    data = File.open(filename, "rb") { |f| f.read }

                    req = {}
                    req['Content-Length'] = File.size(filename).to_s
                    req['Content-MD5'] = Digest::MD5.hexdigest(data)
                    req['X-Smug-AlbumID'] = album["id"].to_s
                    req['X-Smug-Version'] = '1.3.0'
                    req['X-Smug-FileName'] = filename
                    req['X-Smug-ResponseType'] = "JSON"

                    results = put("http://upload.smugmug.com/#{filename}", data, req)
                    puts " => #{results["stat"]} (#{Time.now.to_s})"
                end
            rescue Timeout::Error
                puts " => timed out"
                File.open("upload.log", "a") { |f| f.puts filename }

                # TODO: delete image from server
            rescue Exception => e
                puts " => error #{e.message}"
                puts e.backtrace.join("\n")
                File.open("upload.log", "a") { |f| f.puts filename }

                # TODO: delete image from server
            end
        end
    end

    # refresh cache for modified albums only
    puts "Refreshing albums cache"
    FetchCommand.new.refresh_cache(modified_albums, :force => true)
end