From 5e8a6384c173a78f7cfe7030f0075f3b5f2d6f82 Mon Sep 17 00:00:00 2001 From: Michi Date: Sun, 17 Mar 2013 09:17:22 +0100 Subject: [PATCH] dragonfly middleware added --- app.rb | 30 +++++++++++++++++++++--------- config.ru | 9 ++++----- views/show.haml | 7 +++++++ 3 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 views/show.haml diff --git a/app.rb b/app.rb index c5fb8cb..91b58f5 100644 --- a/app.rb +++ b/app.rb @@ -7,20 +7,22 @@ require 'dragonfly' require 'mongoid' -#configure { set :server, :puma } - class App < Sinatra::Base configure :production, :development do enable :logging end + use Dragonfly::Middleware, :images + app = Dragonfly[:images].configure_with(:imagemagick) app.datastore = Dragonfly::DataStorage::FileDataStore.new app.define_macro_on_include(Mongoid::Document, :image_accessor) - app.datastore.configure do |d| - d.root_path = File.join('upload') + app.configure do |d| + d.datastore.root_path = File.join('upload') + d.datastore.server_root = File.join('upload') + d.url_format = '/images/:job/:basename.:format' end Mongoid.load!('config/mongoid.yml', ENV['RACK_ENV'] ) @@ -55,6 +57,7 @@ class Picture field :image_uid field :image_name + field :image_width field :base_path image_accessor :image @@ -65,10 +68,6 @@ class Picture scss :"sass/#{path}" end - get "/" do - haml :index - end - get "/upload" do haml :upload end @@ -78,7 +77,7 @@ class Picture filename = params[:file][:filename] file = params[:file][:tempfile] - image_uid = app.store(file) + image_uid = app.store(file, :meta => {:time => Time.now, :name => filename}) picture = Picture.create(image_uid: image_uid, image_name: filename) flash "Upload successful of #{filename}" @@ -89,4 +88,17 @@ class Picture redirect '/upload' end + get '/:image_id' do |image_id| + @image = Picture.find(image_id).image + haml :show + end + + get '/d/:image_id' do |image_id| + Picture.find(image_id).image.thumb("200x200#").to_response(env) + end + + get "/" do + haml :index + end + end diff --git a/config.ru b/config.ru index b35631a..04297fe 100644 --- a/config.ru +++ b/config.ru @@ -3,12 +3,11 @@ require 'sinatra' require 'rack/cache' require "./app" -#use Rack::Cache, -# :metastore => 'file:./tmp/meta', -# :entitystore => 'file:./tmp/body', +use Rack::Cache, + :metastore => 'file:./tmp/meta', + :entitystore => 'file:./tmp/body', # #:metastore => 'memcached://localhost:11211/meta', # #:entitystore => 'memcached://localhost:11211/body', -# :verbose => true + :verbose => true -#run Sinatra::Application run App diff --git a/views/show.haml b/views/show.haml new file mode 100644 index 0000000..f12db08 --- /dev/null +++ b/views/show.haml @@ -0,0 +1,7 @@ +.row + .large-8.columns + =# @image.path + = @image.remote_url + = @image.url + %img{:src => "#{@image.thumb('600x600').url}"}/ + %img{:src => "#{@image.thumb('100x100').url}"}/