Skip to content

Instantly share code, notes, and snippets.

@adimania
Created March 2, 2013 10:43
Show Gist options
  • Save adimania/5070466 to your computer and use it in GitHub Desktop.
Save adimania/5070466 to your computer and use it in GitHub Desktop.
Simple ruby script to upload and view images.
require 'rubygems'
require 'sinatra'
get '/' do
"uploader up"
end
post '/image' do
puts params.inspect
File.open("upload-dir/#{params[:id]}", "w") do |f|
f.write(params[:photo][:tempfile].read)
end
end
get '/image' do
files = ""
Dir.foreach("upload-dir/") do |file|
files << "<a type='image/png' href='http://example.com/image/view/#{file}'>#{file}</a><br/>"
end
files
end
get '/image/:filename' do |filename|
send_file "./upload-dir/#{filename}", :filename => filename, :type => 'image/png'
end
get '/image/view/:filename' do |filename|
"<img src='http://example.com/image/#{filename}'>"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment