Created
February 9, 2012 07:13
-
-
Save ProfAvery/1778047 to your computer and use it in GitHub Desktop.
Storing pictures in Redis
This file contains 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 'sinatra' | |
require 'redis' | |
configure do | |
REDIS = Redis.new | |
end | |
get '/' do | |
erb :index | |
end | |
get '/images/:filename' do | |
filename = params[:filename] | |
data = REDIS.get "image:#{filename}" | |
if data.nil? | |
halt 404 | |
end | |
content_type REDIS.get "content-type:#{filename}" | |
data | |
end | |
post '/' do | |
file_info = params[:file] | |
redirect '/' if file_info.nil? | |
@filename = file_info[:filename] | |
REDIS.set "image:#{@filename}", file_info[:tempfile].read | |
REDIS.set "content-type:#{@filename}", file_info[:type] | |
erb :index | |
end | |
__END__ | |
@@index | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Image Upload Test</title> | |
</head> | |
<body> | |
<h1>Upload an image</h1> | |
<form method="POST" action="/" enctype="multipart/form-data" /> | |
<input type="file" size="50" name="file" /> | |
<input type="submit" /> | |
</form> | |
<% if @filename %> | |
<img src="/images/<%= @filename %>" /> | |
<% end %> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment