Created
November 8, 2012 06:28
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
THIS IS CRAZIER THAN THE CRAZIEST THING EVAR