Drop progressbar support and make the script calculate md5 and upload file in a fork.

master
Robert "ar" Gerus 2013-05-23 08:09:15 +02:00
parent 9a46f1473c
commit c63b4e291e
2 changed files with 34 additions and 32 deletions

32
up
View File

@ -1,32 +0,0 @@
#!env ruby
require 'rubygems'
require 'digest/md5'
require 'net/scp'
require 'open-uri'
require 'ruby-progressbar'
files = []
Net::SCP.start("i.am-a.cat", "arachnist") do |scp|
ARGV.each do |path|
ext = "." + path.sub(/.*[.]([^.]*)/, '\1') if path.match(/[^.]+[.][^.]+/)
content = open(path).read
files << {
:title => path.sub(/.*\//, ''),
:ext => ext,
:md5 => Digest::MD5.hexdigest(content),
:content => content,
:size => content.bytes.count
}
end
files.each do |f|
p = ProgressBar.create(:title => f[:title], :total => f[:size], :format => '%t | %E | %w>')
scp.upload! StringIO.new(f[:content]), "/home/arachnist/public_html/c/#{f[:md5]}#{f[:ext]}" do |ch, name, sent, total|
p.progress = sent
end
puts "\t=> https://arachnist.is-a.cat/c/#{f[:md5]}#{f[:ext]}"
end
end

34
up.rb Executable file
View File

@ -0,0 +1,34 @@
#!env ruby
require 'rubygems'
require 'digest/md5'
require 'net/scp'
require 'open-uri'
# set up all the constants here, in case we ever need to change them
UPLOAD_BASE_PATH = "/home/arachnist/public_html/c/"
LINK_BASE_PATH = "https://arachnist.is-a.cat/c/"
HOST = "i.am-a.cat"
USER = "arachnist"
ARGV.each do |path|
Process.fork {
# add the dot here, so that if we get a file without dot, we don't have
# to conditionally add the dot later on
ext = "." + path.sub(/.*[.]([^.]*)/, '\1') if path.match(/[^.]+[.][^.]+/)
content = open(path).read
md5 = Digest::MD5.hexdigest(content)
Net::SCP.start(HOST, USER) do |scp|
# basically a glorified syntax sugar for (ext.nil? ? "" : ext)
begin
scp.upload!(StringIO.new(content), UPLOAD_BASE_PATH + md5 + ext)
# we probably tried to add nil to a string ("ext" could possibly be nil)
rescue TypeError
scp.upload!(StringIO.new(content), UPLOAD_BASE_PATH + md5)
end
end
printf("%s\n\t=> %s%s%s\n", path, LINK_BASE_PATH, md5, ext)
}
end
Process.waitall