Skip to content

Instantly share code, notes, and snippets.

@46bit
Created November 8, 2012 06:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 46bit/4037208 to your computer and use it in GitHub Desktop.
Save 46bit/4037208 to your computer and use it in GitHub Desktop.
My shell file uploader. SCPs files but allows for deleting afterwards, randomising names, etc. Ideal for screenshot uploading.
#!/usr/bin/env ruby
require 'optparse'
require 'digest/md5'
require 'net/scp'
require "fileutils"
options = { }
optparse = OptionParser.new do |opts|
opts.banner = "Usage: b_upload [-p|--preserve|-d|--delete-local] file"
# Configure your server details here
options[:scp_host] = "your-server.example.com"
options[:scp_user] = "user_on_your_server"
options[:scp_folder] = "/folder_for_saving_file_on_server/"
options[:scp_folder_url] = "http://url_to_access_folder_containing_files_on_server/"
options[:preserve] = false
opts.on('-p', '--preserve', 'Preserve current filename') do
options[:preserve] = true
end
options[:delete_local] = false
opts.on("-d", "--delete-local", "Delete local file after upload") do
options[:delete_local] = true
end
opts.on('-h', '--help', 'Display this screen') do
puts opts
exit
end
end
optparse.parse!
file = ARGV[0]
if (file == nil)
puts "No file provided as argument. Use `b_upload -h` for help."
exit
end
original_name = file.split('/').last
if (!options[:preserve])
parts = original_name.split('.')
ext = (parts.length > 1) ? "." + parts.last : ""
name = Digest::MD5.hexdigest(original_name + String(rand())) + ext
else
name = original_name
end
Net::SCP.upload! options[:scp_host], options[:scp_user], file, "#{options[:scp_folder]}#{name}"
puts "#{options[:scp_folder_url]}#{name}"
if options[:delete_local]
if File.exist? File.expand_path("~/.Trash")
# On OSX, and can move file to Trash instead of normal rm (this is safer for users)
# Due to issues with rb-appscript etc, for now this isn't the 'proper' Finder way
FileUtils.mv file, File.expand_path("~/.Trash/")
else
# Not OSX, or no Trash. Just delete it.
FileUtils.rm file
end
end
@dpk
Copy link

dpk commented Nov 11, 2012

THIS IS CRAZIER THAN THE CRAZIEST THING EVAR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment