|
UsingImageUpload
Images upload example
Gemfile # Critical default settings: disable_system_gems disable_rubygems bundle_path ".gems/bundler_gems" gem "dm-appengine" gem "sinatra" config.ru require 'appengine-rack' require 'image-upload' AppEngine::Rack.configure_app( :application => 'image-upload', :precompilation_enabled => true, :version => 1) run Sinatra::Application image-upload.rb #Author: Trung Pham
require 'sinatra'
require 'appengine-apis/images'
require 'dm-core'
DataMapper.setup(:default, "appengine://auto")
class ImageFile
include DataMapper::Resource
property :id, Serial # required for DataMapper
property :data, Blob
end
helpers do
include Rack::Utils
alias_method :h, :escape_html
end
get '/' do
images = ImageFile.all
image_tags = images.collect do |i|
"<img src=\"/images/#{i.id}.jpg\" alt=\"#{i.id}\"/>"
end
"<HTML
<form action=\"/upload\" enctype=\"multipart/form-data\" method=\"post\">
<label>File: </label>
<input type=\"file\"/ name=\"img\"/>
<input type=\"submit\" value=\"Upload\"/>
</form>
<br />
#{image_tags}
<br />
Source files: <a href=\"/testupload.rb.txt\">testupload.rb</a>, <a href=\"/Gemfile.txt\">Gemfile.rb</a>, <a href=\"/config.ru.txt\">config.ru</a>
</HTML>"
end
post '/upload' do
image = AppEngine::Images.load(params[:img][:tempfile].read)
file = ImageFile.new({})
file.data = image.resize(100,100).data
if file.save
redirect "/"
else
'Failed to save'
end
end
get '/images/:id.jpg' do
file = ImageFile.get(params[:id])
content_type :jpeg
file.data
end
|
► Sign in to add a comment
I can't get this to work, got an error about the "Blob" type with DM: "org.jruby.rack.RackInitializationException?: uninitialized constant ImageFile?::Blob"
I have updated all my gems (sdk 1.3, google-appengine-0.0.7, dm-appengine-0.0.7, dm-core-0.10.2...), does anyone have the same problem or managed to run this Image Upload example ?
Thanks !
Got it: "require 'dm-appengine/types'" is needed :)
If you get error from deep down the api when resing the image, try increasing the memory with "dev_appserver . --jvm_flag=-Xmx1024m".