-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhoardr.rb
239 lines (212 loc) · 7.5 KB
/
hoardr.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
require 'digest'
require 'zip'
require 'flickraw'
require 'open-uri'
require 'gibberish'
require 'shellwords'
class Hoardr
attr_accessor :flickr_api_key
attr_accessor :flickr_api_secret
ARCHIVE_FILE = "hoardr_archive.zip"
def initialize(source_path, destination_path, gif_path, flickr_api_key, flickr_api_secret)
@source_path = source_path
@destination_path = destination_path
@gif_path = gif_path
@flickr_api_key = flickr_api_key
@flickr_api_secret = flickr_api_secret
@MAX_SIZE = 200 - (File.size(gif_path).to_f / 2**20)
@filesizes = {}
setup_flickr
end
def run_backup
build_filesizes
files_to_backup = accumulate_files
ids = []
until files_to_backup.empty? do
create_archive(files_to_backup)
combined_file = build_backup
hashed_filenames = files_to_backup.map{|file| Digest::SHA1.hexdigest(file)}
archive_id = do_upload(@destination_path+'/'+combined_file+'.gif',combined_file, hashed_filenames.join(" ") )
system("rm -rf #{@destination_path}/#{combined_file}.gif")
log_archive(archive_id, hashed_filenames, files_to_backup)
ids << archive_id
files_to_backup = accumulate_files
end
system("rm -rf #{@destination_path}/#{ARCHIVE_FILE}")
puts "uploads complete"
puts ids
end
def read_archives
return unless File.exists?(File.dirname(__FILE__)+'/hoardr-archive.txt')
File.open(File.dirname(__FILE__)+'/hoardr-archive.txt', 'r').each do |line|
archived_file = Marshal.load(line)
puts archived_file.inspect
end
end
def archived_files
return unless File.exists?(File.dirname(__FILE__)+'/hoardr-archive.txt')
File.open(File.dirname(__FILE__)+'/hoardr-archive.txt', 'r').each do |line|
archived_file = Marshal.load(line)
puts archived_file.filename
end
end
def download_raw_archive(archive_id)
info = flickr.photos.getInfo(:photo_id => archive_id)
photo_url = FlickRaw.url_o(info)
open("#{archive_id}.gif", 'wb') do |file|
file << open(photo_url).read
end
system("mv #{archive_id}.gif #{archive_id}.zip | unzip #{archive_id}.zip -d #{@destination_path}#{archive_id}")
system("rm -rf #{archive_id}.zip")
end
def retrieve_archive(archive_id)
download_raw_archive(archive_id)
puts "Enter your decryption key: "
key = gets.chomp
cipher = Gibberish::AES.new(key)
Dir.glob("#{@destination_path}#{archive_id}/**/*").each do |file|
next if File.directory?(file)
cipher.decrypt_file(file, file.sub('.enc', ''))
system("rm %s" % Shellwords.escape(file))
end
end
# retrieves a single archived file
def download_raw_file(filename)
return unless file_is_archived?(filename)
archive_id = get_file_archive_id(filename)
download_archive(archive_id)
if( File.directory?(@destination_path+"downloaded_files/"))
system("cp #{archive_id}/#{filename} ../downloaded_files")
else
system("mkdir #{destination_path}downloaded_files | cp #{archive_id}/#{filename} #{@destination_path}downloaded_files")
end
end
def retrieve_archived_file(filename)
download_raw_file(filename)
puts "Enter your decryption key: "
key = gets.chomp
cipher = Gibberish::AES.new(key)
cipher.decrypt_file("#{@destination_path}downloaded_files/#{filename}.enc", filename)
system("rm -rf #{archive_id}")
system("rm %s" % Shellwords.escape("#{filename}.enc"))
end
def find_file_on_flickr(filename)
flickr.photos.search(:tags => Digest::SHA1.hexdigest(filename), :user_id => "me")
end
def all_hashed_filenames
flickr.tags.getListUser["tags"].inspect
end
def file_is_archived?(filename)
open(File.dirname(__FILE__)+'/hoardr-archive.txt'){ |f| return f.grep(/#{filename}/).size > 0 }
end
def get_file_archive_id(filename)
return unless file_is_archived?(filename)
file = nil
open('hoardr-archive.txt'){ |f| file = Marshal.load(f.grep(/#{filename}/)[0]) }
file.archive_id
end
private
def setup_flickr
FlickRaw.api_key = @flickr_api_key
FlickRaw.shared_secret = @flickr_api_secret
if File.exists?(File.dirname(__FILE__)+'/flickr-tokens.txt')
line = nil
File.open(File.dirname(__FILE__)+'/flickr-tokens.txt', 'r'){ |file| line = file.readline }
tokens = line.split
flickr.access_token = tokens[0]
flickr.access_secret = tokens[1]
else
token = flickr.get_request_token
auth_url = flickr.get_authorize_url(token['oauth_token'], :perms => 'delete')
puts "Navigate to this url to authorize your account: "
puts auth_url
puts "enter confirmation #: "
verify = gets.strip
flickr.get_access_token(token['oauth_token'], token['oauth_token_secret'], verify)
end
end
# build hash of filenames mapped to their size in megabytes
def build_filesizes
Dir.foreach(@source_path) do |file|
next if file == '.' || file == '..' || file == '.DS_Store'
filesize = File.size("#{@source_path}/#{file}").to_f / 2**20
next if filesize > @MAX_SIZE
@filesizes[file] = filesize
end
end
# returns array of available files whose cumulative sizes are under the max_upload_size
def accumulate_files
files = []
temp_size = 0
not_done = true
while temp_size <= @MAX_SIZE && not_done
@filesizes.each do |key,value|
next if temp_size+value > @MAX_SIZE
temp_size+=value
files << key
@filesizes.delete(key)
end
not_done = false
end
files
end
def create_archive(files)
puts "Enter your encryption key: "
key = gets.chomp
cipher = Gibberish::AES.new(key)
File.delete(@destination_path+ARCHIVE_FILE) if File.exists?(@destination_path+'/'+ARCHIVE_FILE)
Zip::File.open(@destination_path+ARCHIVE_FILE, Zip::File::CREATE) do |zipfile|
Dir.glob(@source_path+'**/*').each do |file|
next if File.directory?(file)
zipfile.add(file.sub(@source_path,'')+'.enc',cipher.encrypt_file(file, file+'.enc'))
end
end
Dir.glob(@source_path+'**/*').each do |file|
next unless File.extname(file) == '.enc'
File.delete(file)
end
end
def add_to_zip( zipfile, file, &block)
zipfile.add(file.sub(@source_path,'')+'.enc',file+'.enc')
block.call(file)
end
def build_backup
combined_file = Time.now.to_i.to_s
system("cat #{@gif_path} #{@destination_path}/#{ARCHIVE_FILE} > #{@destination_path}/#{combined_file}.gif")
combined_file
end
def do_upload(combined_image, title='stuffr-backup', tags)
begin
return flickr.upload_photo(combined_image, :title => title, :tags => tags, :is_public => 0)
rescue FlickRaw::FailedResponse => e
puts "Authentication failed : #{e.msg}"
end
end
# retrieves a single archived file
def download_raw_file(filename)
return unless file_is_archived?(filename)
archive_id = get_file_archive_id(filename)
download_archive(archive_id)
if( File.directory?(@destination_path+"downloaded_files/"))
system("cp #{archive_id}/#{filename} ../downloaded_files")
else
system("mkdir #{destination_path}downloaded_files | cp #{archive_id}/#{filename} #{@destination_path}downloaded_files")
end
end
def log_archive(archive_id, hashed_filenames, raw_filenames)
File.open(File.dirname(__FILE__)+'/hoardr-archive.txt', 'a') do |archive_log|
hashed_filenames.each_with_index do |f, i|
archive_log.write( Marshal.dump(ArchivedFile.new(raw_filenames[i],f, archive_id)))
archive_log.write("\n")
end
end
end
end
class ArchivedFile
attr_accessor :filename, :hashed_filename, :archive_id
def initialize(filename, hashed_filename, archive_id )
@filename = filename
@hashed_filename = hashed_filename
@archive_id = archive_id
end
end