Simplify the code by using a ternary operator.
parent
6feefbe592
commit
c5b5c9b775
10
up.rb
10
up.rb
|
@ -15,15 +15,11 @@ ARGV.each do |path|
|
||||||
# add the dot here, so that if we get a file without dot, we don't have
|
# add the dot here, so that if we get a file without dot, we don't have
|
||||||
# to conditionally add the dot later on
|
# to conditionally add the dot later on
|
||||||
ext = "." + path.sub(/.*[.]([^.]*)/, '\1') if path.match(/[^.]+[.][^.]+/)
|
ext = "." + path.sub(/.*[.]([^.]*)/, '\1') if path.match(/[^.]+[.][^.]+/)
|
||||||
|
# make sure ext isn't nil
|
||||||
|
ext = ext.nil? ? "" : ext
|
||||||
content = open(path).read
|
content = open(path).read
|
||||||
md5 = Digest::MD5.hexdigest(content)
|
md5 = Digest::MD5.hexdigest(content)
|
||||||
# basically a glorified syntax sugar for (ext.nil? ? "" : ext)
|
Net::SCP.upload!(HOST, USERNAME, StringIO.new(content), UPLOAD_BASE_PATH + md5 + ext)
|
||||||
begin
|
|
||||||
Net::SCP.upload!(HOST, USERNAME, StringIO.new(content), UPLOAD_BASE_PATH + md5 + ext)
|
|
||||||
# we probably tried to add nil to a string ("ext" could possibly be nil)
|
|
||||||
rescue TypeError
|
|
||||||
Net::SCP.upload!(HOST, USERNAME, StringIO.new(content), UPLOAD_BASE_PATH + md5)
|
|
||||||
end
|
|
||||||
printf("%s\t=> %s%s%s\n", path, LINK_BASE_PATH, md5, ext)
|
printf("%s\t=> %s%s%s\n", path, LINK_BASE_PATH, md5, ext)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue