2012-11-01 17:36:36 +00:00
|
|
|
#!/usr/bin/env ruby
|
2012-12-20 11:53:25 +00:00
|
|
|
# argv[0] user token; argv[1] url to file-store
|
2012-11-01 17:36:36 +00:00
|
|
|
require 'json'
|
|
|
|
require 'rest-client'
|
|
|
|
|
2012-11-09 14:14:26 +00:00
|
|
|
abf_yml, new_sources = '.abf.yml', []
|
2012-11-01 17:36:36 +00:00
|
|
|
old_sources = if File.exist? abf_yml
|
|
|
|
File.read(abf_yml).split("\n").reject {|line| line =~ /sources/}
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2012-11-12 09:17:25 +00:00
|
|
|
#MAX_SIZE = 2 * 1024 * 1024 # 2.megabytes
|
2012-12-20 11:53:25 +00:00
|
|
|
url = "#{ARGF.argv[1]}/api/v1"
|
2012-11-02 17:45:40 +00:00
|
|
|
rclient = RestClient::Resource.new(url, :user => ARGF.argv[0]) # user auth token
|
2012-11-01 17:36:36 +00:00
|
|
|
|
2012-11-19 09:37:15 +00:00
|
|
|
Dir.glob("*.{bz2,rar,gz,tar,tbz2,tgz,zip,Z,7z,xz,lzma}").uniq.sort.each do |file|
|
2012-11-01 17:36:36 +00:00
|
|
|
begin
|
2012-11-12 09:17:25 +00:00
|
|
|
#next if File.size(file) < MAX_SIZE
|
2012-11-01 17:36:36 +00:00
|
|
|
|
|
|
|
sha1 = Digest::SHA1.file(file).hexdigest
|
2012-12-20 11:53:25 +00:00
|
|
|
resp = JSON(RestClient.get "#{url}/file_stores", :params => {:hash => sha1})
|
2012-11-01 17:36:36 +00:00
|
|
|
if resp[0].respond_to?('[]') && resp[0]['file_name'] && resp[0]['sha1_hash']
|
|
|
|
# file already exists at file-store
|
|
|
|
new_sources << " \"#{file}\": #{sha1}"
|
|
|
|
FileUtils.rm_rf file
|
2012-11-09 14:14:26 +00:00
|
|
|
puts " file \"#{file}\" already exists in the file-store"
|
2012-11-01 17:36:36 +00:00
|
|
|
elsif resp == []
|
|
|
|
# try to put file at file-store
|
2012-12-20 11:53:25 +00:00
|
|
|
resp = JSON `curl --user #{ARGF.argv[0]}: -POST -F "file_store[file]=@#{file}" #{url}/upload`
|
2012-11-01 17:36:36 +00:00
|
|
|
unless resp['sha1_hash'].nil?
|
2012-11-09 14:14:26 +00:00
|
|
|
new_sources << " \"#{file}\": #{sha1}"
|
2012-11-01 17:36:36 +00:00
|
|
|
FileUtils.rm_rf file
|
2012-11-12 09:17:25 +00:00
|
|
|
p " upload file \"#{file}\" to the file-store"
|
|
|
|
else
|
|
|
|
p " !Failed to upload file \"#{file}\" to the file-store!"
|
2012-11-01 17:36:36 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
raise "Response unknown!\n #{resp}"
|
|
|
|
end
|
|
|
|
|
|
|
|
#rescue => e
|
|
|
|
# e.response
|
|
|
|
end
|
|
|
|
end
|
2012-11-09 14:14:26 +00:00
|
|
|
sources = (old_sources | new_sources)
|
2012-11-13 18:06:49 +00:00
|
|
|
unless new_sources.empty?
|
2012-11-12 09:17:25 +00:00
|
|
|
File.open(abf_yml, 'w') do |abf|
|
|
|
|
abf.puts 'sources:'
|
|
|
|
(old_sources | new_sources).sort.each {|line| abf.puts line}
|
2012-11-09 14:14:26 +00:00
|
|
|
end
|
2012-11-01 17:36:36 +00:00
|
|
|
end
|