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 "Usage: smug upload [<options>]\nUpload files to SmugMug.\n\nOptions:\n        END\n        opt :timeout,\n            \"Timeout to upload one file in seconds\",\n            :short => :t,\n            :default => 24*3600\n    end\n\n    opts = Trollop::with_standard_exception_handling(optparser) do\n        optparser.parse(ARGV)\n    end\n\n    authenticate\n\n    status = StatusCommand.new.current_dir_status\n    Dir::chdir(Config::root_dir)\n\n    modified_albums = Set.new\n\n    status.each_with_index do |album_status, i|\n        if album_status[:status] == :not_uploaded\n            # create album\n            puts \"Creating album \#{album_status[:album]}\"\n            result = smugmug_albums_create(\n                \"Title\" => album_status[:album]\n            )\n            raise \"Cannot create album\" unless result[\"stat\"] == \"ok\"\n\n            # refresh cache for the newly created album\n            album = smugmug_albums_getInfo(\n                :AlbumID => result[\"Album\"][\"id\"],\n                :AlbumKey => result[\"Album\"][\"Key\"]\n            )[\"Album\"]\n\n            FetchCommand.new.refresh_cache([album])\n            @albums = nil # TODO: hack to force reparsing of cache file\n        elsif album_status[:status] == :not_downloaded\n            next\n        end\n\n        album = albums.find { |a| a[\"Title\"] == album_status[:album] }\n\n        files_to_upload = album_status[:images].find_all { |i| i[:status] == :not_uploaded }.map { |i| \"\#{album[\"Title\"]}/\#{i[:image]}\" }\n\n        File.open(\"upload.log\", \"w+\") { |f| f.puts \"start\" }\n        num_files = files_to_upload.length\n        files_to_upload.each_with_index do |filename, i|\n            modified_albums << album\n\n            begin\n                with_timeout(opts[:timeout]) do\n                    puts \"Uploading \#{filename} (\#{Time.now.to_s}) (\#{i}/\#{num_files})\"\n\n                    data = File.open(filename, \"rb\") { |f| f.read }\n\n                    req = {}\n                    req['Content-Length'] = File.size(filename).to_s\n                    req['Content-MD5'] = Digest::MD5.hexdigest(data)\n                    req['X-Smug-AlbumID'] = album[\"id\"].to_s\n                    req['X-Smug-Version'] = '1.3.0'\n                    req['X-Smug-FileName'] = filename\n                    req['X-Smug-ResponseType'] = \"JSON\"\n\n                    results = put(\"http://upload.smugmug.com/\#{filename}\", data, req)\n                    puts \" => \#{results[\"stat\"]} (\#{Time.now.to_s})\"\n                end\n            rescue Timeout::Error\n                puts \" => timed out\"\n                File.open(\"upload.log\", \"a\") { |f| f.puts filename }\n\n                # TODO: delete image from server\n            rescue Exception => e\n                puts \" => error \#{e.message}\"\n                puts e.backtrace.join(\"\\n\")\n                File.open(\"upload.log\", \"a\") { |f| f.puts filename }\n\n                # TODO: delete image from server\n            end\n        end\n    end\n\n    # refresh cache for modified albums only\n    puts \"Refreshing albums cache\"\n    FetchCommand.new.refresh_cache(modified_albums, :force => true)\nend\n"