Skip to content

Instantly share code, notes, and snippets.

@jessykate
Created March 15, 2012 23:13
Show Gist options
  • Save jessykate/2047552 to your computer and use it in GitHub Desktop.
Save jessykate/2047552 to your computer and use it in GitHub Desktop.
post an image to a random short-ish url on your server via scp
#!/usr/bin/ruby
# jessy kate schingler
# @jessykate | jessykate.com
# public domain
# assumes ssh key access to server
# usage:
# $ postimg path/to/img
# returns url for access
#
# requires:
# server to be setup to host files at this url and interpret the url (without
# .html extension) as the correct mime-type.
#### EDIT THESE FOR YOUR OWN SERVER #####
POST_BASE_URL = "http://scratch.jessykate.com/"
USERNAME = "jessykate"
SERVER = "scratch.jessykate.com"
SERVER_DIR = "/srv/www/jessykate.com/scratch/"
FILE_DIR = "files/" # empty or a subdirectory of SERVER_DIR
#### STOP EDITING ####
require 'fileutils'
upload = ARGV[0]
# generate a random short-ish url and post an image to it
o = [('a'..'z'),('A'..'Z')].map{|i| i.to_a}.flatten
rand_string = (0..10).map{ o[rand(o.length)] }.join
url = "#{POST_BASE_URL}#{rand_string}"
filename = File.basename(upload)
# upload the file
# scp should prompt for ssh key password if it exists
%x[scp #{upload} #{USERNAME}@#{SERVER}:#{SERVER_DIR}#{FILE_DIR}]
HTML = <<EOF
<html>
<head>
<title>Scratch Space</title>
</head>
</head>
<body>
<div>
<img src="#{POST_BASE_URL}#{FILE_DIR}#{filename}" />
</div>
</body>
EOF
tmpfile = "/tmp/#{rand_string}"
tmp = File.open(tmpfile, 'w')
tmp.write(HTML)
tmp.close
%x[scp #{tmpfile} #{USERNAME}@#{SERVER}:#{SERVER_DIR}]
FileUtils.rm tmpfile
puts "Visit your file at #{POST_BASE_URL}#{rand_string}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment