Permalink
Browse files

font awesome added, some code cleaning

  • Loading branch information...
1 parent add4ef1 commit 240738c97f893cdc0fe49dda57c9e02f6be96b9b @zlorfi committed Mar 23, 2013
View
@@ -7,6 +7,7 @@ gem 'puma'
gem 'haml'
gem 'rack-cache', :require => 'rack/cache'
gem 'dragonfly'
+gem 'bson_ext'
gem 'mongoid'
gem 'rack-flash3'
View
@@ -7,6 +7,9 @@ GEM
activesupport (3.2.12)
i18n (~> 0.6)
multi_json (~> 1.0)
+ bson (1.8.2)
+ bson_ext (1.8.2)
+ bson (~> 1.8.2)
builder (3.0.4)
chunky_png (1.2.7)
coderay (1.0.9)
@@ -60,6 +63,7 @@ PLATFORMS
ruby
DEPENDENCIES
+ bson_ext
compass
dragonfly
haml
View
@@ -1,9 +1,12 @@
# Sinatra Gallery App
## Includes
+- [Sinatra](http://www.sinatrarb.com)
- [Zurb 4](https://github.com/zurb/foundation)
+- [Font-Awesome](http://fortawesome.github.com)
- [Dragonfly](https://github.com/markevans/dragonfly)
- [Mongoid](https://github.com/mongoid/mongoid)
+- [jQuery-File-Upload](https://github.com/blueimp/jQuery-File-Upload)
## !!WORK STILL IN PROGRESS!!
@@ -14,4 +17,5 @@
- `bundle exec shotgun config.ru` for development
- `bundle exec puma config.ru` for production
-
+## TODO
+- image handler exception
View
143 app.rb
@@ -1,16 +1,14 @@
require 'rubygems' unless RUBY_VERSION >= '1.9'
-require "bundler/setup"
-require "sinatra/base"
+require 'bundler/setup'
+require 'sinatra/base'
require 'compass'
-require "zurb-foundation"
+require 'zurb-foundation'
require 'haml'
require 'dragonfly'
require 'mongoid'
require 'rack-flash'
-require './helper/dragonfly_helper'
class App < Sinatra::Base
- helpers Sinatra::DragonflyHelper
configure :production, :development do
enable :logging
@@ -44,61 +42,162 @@ class App < Sinatra::Base
set :scss, Compass.sass_engine_options
set :server, :puma
enable :sessions
- use Rack::Flash, :sweep => true
+ use Rack::Flash#, :sweep => true
+
+ #necessary for the DELETE route when using
+ use Rack::MethodOverride
+
+ set :username,'gallery'
+ # make this a huge random number
+ # SecureRandom.urlsafe_base64(30, true)
+ set :token,'SzXdCtiS4hmt6gXhS4NIahrfL7iH7aUb0DXd-B35'
+ set :password,'gallery'
end
class Picture
include Mongoid::Document
field :image_uid
field :image_name
+ field :image_title
+ field :image_date
+ field :image_model
field :base_path
image_accessor :image
end
+ #before %r{\.(css)|(js)|(png)|(ico)} do
+ # response.headers['Cache-Control'] = 'public, max-age=604800'
+ #end
+
+ before do
+ @p = Picture.all
+ if @p.empty?
+ flash[:notice] = "No documents found!"
+ end
+ end
+
+
+ helpers do
+ def asset_stylesheet(stylesheet)
+ "/stylesheets/#{stylesheet}.css?" + File.mtime(File.join(Sinatra::Application.views, "sass", "#{stylesheet}.scss")).to_i.to_s
+ end
+
+ def asset_javascript(js)
+ "/javascripts/#{js}.js?" + File.mtime(File.join(Sinatra::Application.public_folder, "javascripts", "#{js}.js")).to_i.to_s
+ end
+
+ def admin?
+ request.cookies[settings.username] == settings.token
+ end
+
+ def protected!
+ halt [ 401, 'Not Authorized' ] unless admin?
+ end
+
+ def flash_display
+ response = ""
+ unless flash.nil?
+ flash
+ #flash.each do |name, msg|
+ # response = response + content_tag(:div, msg, :id => "flash_#{name}")
+ #end
+ #flash.discard
+ #response
+ end
+ end
+
+ def raw(text)
+ Rack::Utils.escape_html(text)
+ end
+
+ end
+
get "/stylesheets/*.css" do |path|
content_type "text/css", charset: "utf-8"
+ response['Expires'] = (Time.now + 60*60*24*356*3).httpdate
scss :"sass/#{path}"
end
+ post '/login' do
+ if params['username']==settings.username && params['password']==settings.password
+ response.set_cookie(settings.username, {:value => settings.token, :expires => Time.now + (60*60*24*2)})
+ flash[:success] = "Login succeeded!"
+ redirect '/'
+ else
+ flash[:alert] = "Login failed!"
+ redirect '/'
+ end
+ end
+
+ get '/logout' do
+ response.set_cookie(settings.username, false)
+ flash[:success] = "Logout successful!"
+ redirect '/'
+ end
+
get "/upload" do
- haml :upload
+ haml :upload, :layout => !request.xhr?
end
post "/upload" do
+ protected!
+ content_type 'application/json', :charset => 'utf-8' if request.xhr?
if params[:file]
filename = params[:file][:filename]
file = params[:file][:tempfile]
- image_uid = app.store(file, :meta => {:time => Time.now, :name => filename})
- picture = Picture.create(image_uid: image_uid, image_name: filename)
-
- flash[:success] = "Upload successful of #{filename}"
- redirect "/gallery/#{picture.id}"
+ prepared_image = app.fetch_file(file).process!(:resize, '800x800>')
+ image_uid = app.store(prepared_image, :meta => {:upload_time => Time.now, :name => filename})
+ picture = Picture.create(image_uid: image_uid,
+ image_name: filename,
+ image_title: params[:title],
+ image_date: Time.now
+ )
+
+ flash.now[:success] = "Upload successful of #{filename}"
+ redirect "/i/#{picture.id}" if !request.xhr?
+ #flash.keep unless request.xhr?
+ picture.to_json
else
- flash[:alert] = 'You have to choose a file first'
+ flash.now[:alert] = 'You have to choose a file first'
+ #flash.keep unless request.xhr?
redirect "/upload"
end
end
- get "/gallery" do
- @pictures = Picture.all
- haml :gallery
- end
-
- get '/gallery/:image_id' do |image_id|
+ get '/i/:image_id' do |image_id|
@image = Picture.find(image_id).image
+ @picture = Picture.find(image_id)
haml :show
end
- get '/d/:image_id' do |image_id|
- Picture.find(image_id).image.thumb("200x200#").to_response(env)
+ get '/t/:image_id' do |image_id|
+ Picture.find(image_id).image.thumb("400x400#").to_response(env)
end
- get "/" do
+ delete '/delete/:image_id' do
+ protected!
+ p = Picture.find(params[:image_id])
+ if p.delete
+ File.delete("upload/#{p.image_uid}")
+ flash[:success] = "Image #{p.image_name} has been deleted"
+ redirect '/'
+ else
+ flash[:alert] = "ERROR!"
+ end
+ end
+
+ get '/' do
+ @pictures = Picture.all.asc(:image_date)
haml :index
end
+ not_found do
+ flash[:notice] = "404 - Page not found"
+ redirect '/'
+ end
+
end
View
@@ -4,3 +4,5 @@ development:
database: sinatra_gallery
hosts:
- localhost:27017
+ options:
+ raise_not_found_error: false
View
@@ -1,49 +0,0 @@
-require 'sinatra'
-module Sinatra
- module DragonflyHelper
-
- # Cache Dragonfly thumbnail generation and produce friendly SEO URLs
- def thumbnail_tag(image, size, options={})
-
- if options.has_key?(:path)
- sub_dir = options[:path]
- options.delete(:path)
- else
- sub_dir = 'thumbnails'
- end
-
- path = File.join('/', sub_dir, size, image.name)
- resource = File.join('public', path)
-
- unless File.exists?(resource)
- image.thumb(size).to_file(resource)
- end
-
- attrs = ''
- options.each do |key, value|
- if key == :data
- value.each do |key, value|
- attrs << "data-#{key.to_s}='#{value.to_s}' "
- end
- else
- attrs << "#{key.to_s}='#{value.to_s}' "
- end
- end
-
- attrs << "alt='#{File.basename(image.name, '.*')}'" if options[:alt].blank?
-
- # Optionnal stuff : since resize params can be very tricky (ex: '400x300+50+100'),
- # make sure they are in simple 'widthxheight' format to pick up width/height params
- # If not, just forget it and leave the browser deal with image dimensions
- width, height = ''
- if(size.split('x')[0].to_i.to_s == size.split('x')[0] && size.split('x')[1].to_i.to_s == size.split('x')[1])
- width = "width='#{size.split('x')[0].to_i}px'"
- height = "height='#{size.split('x')[1].to_i}px'"
- end
-
- "<img src='#{path}' #{width} #{height} #{attrs} />".html_safe
- end
-
- end
- helpers DragonflyHelper
-end
View
Binary file not shown.
View
Binary file not shown.
Binary file not shown.
Oops, something went wrong.

0 comments on commit 240738c

Please sign in to comment.