diff --git a/up b/up deleted file mode 100755 index 33631ee..0000000 --- a/up +++ /dev/null @@ -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 diff --git a/up.rb b/up.rb new file mode 100755 index 0000000..2484e20 --- /dev/null +++ b/up.rb @@ -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 +