Skip to content

Instantly share code, notes, and snippets.

@patrick99e99
Created September 18, 2009 18:09
Show Gist options
  • Save patrick99e99/189203 to your computer and use it in GitHub Desktop.
Save patrick99e99/189203 to your computer and use it in GitHub Desktop.
acts_as_fleximage do
image_directory "/media/master_images"
use_creation_date_based_directories true
# image_storage_format :jpg # the plugin is handling this now
require_image true
missing_image_message 'is required'
invalid_image_message 'was not a readable image'
# default_image_path 'public/images/image_not_available.png'
default_image nil
output_image_jpg_quality 85
end
def downloadable?(user)
true # for now...
end
class PhotosController < ApplicationController
FLEXI_METHODS.each {|flexi| caches_page flexi }
FLEXI_METHODS.each do |flexi|
define_method flexi do
@photo = Photo.find(params[:id])
render :template => "photos/flexi/#{flexi}"
end
end
SEND_FILE_METHOD = :default
def download
head(:not_found) and return if (photo = Photo.find_by_id(params[:id])).nil?
head(:forbidden) and return unless photo.downloadable?(current_user)
path = "#{RAILS_ROOT}/media/photos/#{params[:id]}/#{params[:action_name]}.#{params[:format]}"
head(:bad_request) and return unless File.exist?(path) && params[:format].to_s == File.extname(path).gsub(/^\.+/, '')
send_file_options = { :type => File.mime_type?(path) }
case SEND_FILE_METHOD
when :apache then send_file_options[:x_sendfile] = true
when :nginx then head(:x_accel_redirect => path.gsub(Rails.root, ''), :content_type => send_file_options[:type]) and return
end
send_file(path, send_file_options)
end
end
map.resources :photos, :has_many => :comments, :collection => { :destroy_album => :delete, :zip_import => :post, :builder_index => :get }, :member => FLEXI_METHODS.each_with_object({}){|string, hash| hash[string] = :get} # this returns {:tiny_thumb_cover => :get, :builder_show => :get, etc...}
map.connect 'images/:id/:action_name.:format', :controller => 'photos', :action => 'download', :conditions => { :method => :get }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment