Skip to content

Commit

Permalink
dragonfly middleware added
Browse files Browse the repository at this point in the history
  • Loading branch information
zlorfi committed Mar 17, 2013
1 parent 3b003af commit 5e8a638
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
30 changes: 21 additions & 9 deletions app.rb
Expand Up @@ -7,20 +7,22 @@
require 'dragonfly' require 'dragonfly'
require 'mongoid' require 'mongoid'


#configure { set :server, :puma }

class App < Sinatra::Base class App < Sinatra::Base
configure :production, :development do configure :production, :development do
enable :logging enable :logging
end end


use Dragonfly::Middleware, :images

app = Dragonfly[:images].configure_with(:imagemagick) app = Dragonfly[:images].configure_with(:imagemagick)


app.datastore = Dragonfly::DataStorage::FileDataStore.new app.datastore = Dragonfly::DataStorage::FileDataStore.new
app.define_macro_on_include(Mongoid::Document, :image_accessor) app.define_macro_on_include(Mongoid::Document, :image_accessor)


app.datastore.configure do |d| app.configure do |d|
d.root_path = File.join('upload') d.datastore.root_path = File.join('upload')
d.datastore.server_root = File.join('upload')
d.url_format = '/images/:job/:basename.:format'
end end


Mongoid.load!('config/mongoid.yml', ENV['RACK_ENV'] ) Mongoid.load!('config/mongoid.yml', ENV['RACK_ENV'] )
Expand Down Expand Up @@ -55,6 +57,7 @@ class Picture


field :image_uid field :image_uid
field :image_name field :image_name
field :image_width
field :base_path field :base_path


image_accessor :image image_accessor :image
Expand All @@ -65,10 +68,6 @@ class Picture
scss :"sass/#{path}" scss :"sass/#{path}"
end end


get "/" do
haml :index
end

get "/upload" do get "/upload" do
haml :upload haml :upload
end end
Expand All @@ -78,7 +77,7 @@ class Picture
filename = params[:file][:filename] filename = params[:file][:filename]
file = params[:file][:tempfile] 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) picture = Picture.create(image_uid: image_uid, image_name: filename)


flash "Upload successful of #{filename}" flash "Upload successful of #{filename}"
Expand All @@ -89,4 +88,17 @@ class Picture
redirect '/upload' redirect '/upload'
end 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 end
9 changes: 4 additions & 5 deletions config.ru
Expand Up @@ -3,12 +3,11 @@ require 'sinatra'
require 'rack/cache' require 'rack/cache'
require "./app" require "./app"


#use Rack::Cache, use Rack::Cache,
# :metastore => 'file:./tmp/meta', :metastore => 'file:./tmp/meta',
# :entitystore => 'file:./tmp/body', :entitystore => 'file:./tmp/body',
# #:metastore => 'memcached://localhost:11211/meta', # #:metastore => 'memcached://localhost:11211/meta',
# #:entitystore => 'memcached://localhost:11211/body', # #:entitystore => 'memcached://localhost:11211/body',
# :verbose => true :verbose => true


#run Sinatra::Application
run App run App
7 changes: 7 additions & 0 deletions 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}"}/

0 comments on commit 5e8a638

Please sign in to comment.