avm-example-app/lib/avm_example.rb

74 lines
1.5 KiB
Ruby
Raw Permalink Normal View History

2011-03-23 18:42:54 +00:00
require 'sinatra/base'
require 'sinatra/flash'
require 'sinatra/content_for'
require 'avm/image'
require 'coderay'
require 'pp'
2011-03-30 14:22:59 +00:00
require 'base64'
2011-03-23 18:42:54 +00:00
class AVMExample < Sinatra::Base
use Rack::Session::Cookie, :key => 'avm.session',
:path => '/',
:expire_after => 60
2011-03-23 19:01:44 +00:00
configure do
2011-03-23 19:03:04 +00:00
set :root, File.expand_path('../..', __FILE__)
2011-03-23 19:01:44 +00:00
end
2011-03-23 19:01:14 +00:00
2011-03-23 18:42:54 +00:00
register Sinatra::Flash
helpers Sinatra::ContentFor
helpers do
def uploaded?
@is_uploaded ||= ensure_uploaded_xmp
end
def tempfile
ensure_uploaded_xmp
@tempfile
end
def filename
ensure_uploaded_xmp
@filename
end
end
post '/upload' do
redirect_with_warn("No file provided!") if !uploaded?
2011-03-30 14:22:59 +00:00
data = params[:file][:tempfile].read
if settings.environment == :test
data = Base64.decode64(data)
end
2011-03-23 18:42:54 +00:00
begin
2011-03-30 14:22:59 +00:00
@image = AVM::Image.from_xml(data)
2011-03-23 18:42:54 +00:00
redirect_with_warn("Bad XMP file!") if !@image.valid?
rescue StandardError => e
redirect_with_warn("Error reading XMP file!")
$stderr.puts e.to_s
end
flash[:info] = %{File processed! Data structure is <a href="#data">below</a>.}
haml :print_data
end
2011-03-23 18:54:57 +00:00
get %r{^/?$} do
2011-03-23 18:42:54 +00:00
haml :index
end
private
def redirect_with_warn(warning)
flash[:warn] = warning
redirect '/'
end
def ensure_uploaded_xmp
return false if !params[:file]
return false if !(@tempfile = params[:file][:tempfile])
return false if !(@filename = params[:file][:filename])
true
end
end