carrierwave upload, specified in photo model. create account at startup. auto add to admin role

rails2
Espen Antonsen 2010-04-12 18:40:47 -04:00
parent efb21a1df1
commit f52d6ba791
12 changed files with 119 additions and 48 deletions

3
.gems
View File

@ -1 +1,2 @@
authlogic
authlogic
carrierwave

3
.gitignore vendored
View File

@ -9,4 +9,5 @@ config/deploy.rb
config/deploy
Capfile
photos
public/thumbs
public/thumbs
public/uploads

View File

@ -7,10 +7,16 @@ class ApplicationController < ActionController::Base
filter_parameter_logging :password, :password_confirmation
helper_method :current_user, :current_user_session
before_filter :setup
private
def setup
redirect_to new_account_path if User.all.length == 0
end
def check_public_access
require_user unless APP_CONFIG[:public]
end

View File

@ -76,7 +76,7 @@ class PhotosController < ApplicationController
respond_to do |format|
@photo = Photo.new(params[:photo])
if params[:Filedata]
@photo.swf_uploaded_data = params[:Filedata]
#@photo.swf_uploaded_data = params[:Filedata]
if @photo.save
format.html { render :text => "FILEID:" + @photo.path_modified_public("album") }
format.xml { render :nothing => true }

View File

@ -1,6 +1,7 @@
class UsersController < ApplicationController
before_filter :require_no_user, :only => [:new, :create]
before_filter :require_user, :only => [:show, :edit, :update, :destroy]
skip_filter :setup
def new
@user = User.new
@ -9,8 +10,11 @@ class UsersController < ApplicationController
def create
@user = User.new(params[:user])
if @user.save
if User.all.length == 1
@user.roles << Role.create(:name => 'admin')
end
flash[:notice] = "Account registered!"
redirect_back_or_default account_path
redirect_back_or_default new_collection_path
else
render :action => :new
end

View File

@ -6,13 +6,15 @@ class Photo < ActiveRecord::Base
has_many :photo_tags, :dependent => :destroy
has_many :tags, :through => :photo_tags
validates_uniqueness_of :path, :message => "Photo already exsists on disc"
validates_presence_of :title
mount_uploader :file, FileUploader
#validates_uniqueness_of :path, :message => "Photo already exsists on disc"
#validates_presence_of :title
before_validation :set_title
before_save :ensure_file
before_create :exif_read
after_create :create_thumbnails
#before_save :ensure_file
#before_create :exif_read
#after_create :create_thumbnails
#before_update :exif_write # should only write if tags are changed as images can be large and thus ExifTool will take a while to write to the file
before_destroy :destroy_file
@ -70,37 +72,6 @@ class Photo < ActiveRecord::Base
#end
end
# Map file extensions to mime types.
# Thanks to bug in Flash 8 the content type is always set to application/octet-stream.
# From: http://blog.airbladesoftware.com/2007/8/8/uploading-files-with-swfupload
def swf_uploaded_data=(data)
data.content_type = MIME::Types.type_for(data.original_filename)
self.title = data.original_filename
self.path = self.album.path + "/" + data.original_filename.parameterize
File.open(APP_CONFIG[:photos_path] + self.path, 'wb') { |f| f.write(data.read) }
end
def create_thumbnails
# TODO: thumbnails size should be set in settings.yml
return if File.exists?(APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_collection" + self.extension)
ImageScience.with_image(self.path_original) do |img|
img.cropped_thumbnail(200) do |thumb|
thumb.save APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_collection" + self.extension
end
img.cropped_thumbnail(100) do |thumb|
thumb.save APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_album" + self.extension
end
img.thumbnail(210) do |thumb|
thumb.save APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_preview" + self.extension
end
img.thumbnail(950) do |thumb|
thumb.save APP_CONFIG[:thumbs_path] + self.album.path + "/" + self.id.to_s + "_single" + self.extension
end
end
end
protected
def extension
@ -118,11 +89,12 @@ class Photo < ActiveRecord::Base
private
def set_title
self.title = File.basename( self.path ).gsub( self.extension, "" ).titleize unless self.title
self.title = "tesitn"
#self.title = self.file.filename.titleize unless self.title
end
def ensure_file
self.destroy if !File.exists?( APP_CONFIG[:photos_path] + self.path )
#self.destroy if !File.exists?( APP_CONFIG[:photos_path] + self.path )
end
def exif_read

View File

@ -0,0 +1,58 @@
# encoding: utf-8
class FileUploader < CarrierWave::Uploader::Base
# Include RMagick or ImageScience support
include CarrierWave::RMagick
# include CarrierWave::ImageScience
# Choose what kind of storage to use for this uploader
storage :file
# storage :s3
# Override the directory where uploaded files will be stored
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
# "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
"uploads/files/#{model.album.path}"
end
# Provide a default URL as a default if there hasn't been a file uploaded
# def default_url
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded.
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files
# two firsts should be cropped
version :collection do
process :scale => [200, 200]
end
version :album do
process :scale => [100, 100]
end
version :preview do
process :scale => [210, 210]
end
version :single do
process :scale => [950, 950]
end
# Add a white list of extensions which are allowed to be uploaded,
# for images you might use something like this:
# def extension_white_list
# %w(jpg jpeg gif png)
# end
# Override the filename of the uploaded files
# def filename
# "something.jpg" if original_filename
# end
end

View File

@ -4,8 +4,8 @@
<%= form.label :description %><br />
<%= form.text_area :description %><br />
<% unless @collection.albums.empty? %>
<%= form.label :albums %><br />
<div id="collection_albums">
<% for album in @collection.albums %>
<% form.fields_for :album_list do |album_fields| %>
@ -32,4 +32,6 @@ grouped_options = [
grouped_options_for_select(grouped_options)
%>
<%= select_tag 'available_albums', grouped_options_for_select(grouped_options) %>
</p>
</p>
<% end %>

View File

@ -3,7 +3,7 @@
<script type="text/javascript">
$(document).ready(function() {
$('#photo').fileUpload({
$('#photo_file').fileUpload({
uploader:'/javascripts/jquery.uploadify-v1.6.2.mit/uploader.swf',
script:'<%= photos_path %>',
scriptData: {
@ -29,7 +29,7 @@ $(document).ready(function() {
</script>
<% end %>
<form>
<input type="file" id="photo" name="photo" />
<input type="file" id="photo_file" name="photo[file]" />
<br />
<div id="thumbs"></div>
</form>

View File

@ -11,8 +11,10 @@ Rails::Initializer.run do |config|
config.gem "authlogic"
config.gem 'mime-types', :lib => 'mime/types'
config.gem "image_science"
#config.gem "image_science"
#config.gem "mini_exiftool"
#
config.gem "carrierwave"
config.load_paths += %W( #{RAILS_ROOT}/app/middleware )

View File

@ -0,0 +1,9 @@
class AddFileToPhoto < ActiveRecord::Migration
def self.up
add_column :photos, :file, :string
end
def self.down
remove_column :photos, :file
end
end

View File

@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20090604202930) do
ActiveRecord::Schema.define(:version => 20100412220801) do
create_table "albums", :force => true do |t|
t.string "title", :null => false
@ -23,6 +23,8 @@ ActiveRecord::Schema.define(:version => 20090604202930) do
t.text "note"
end
add_index "albums", ["id"], :name => "index_albums_on_id", :unique => true
create_table "collection_albums", :force => true do |t|
t.integer "collection_id"
t.integer "album_id"
@ -30,6 +32,9 @@ ActiveRecord::Schema.define(:version => 20090604202930) do
t.datetime "updated_at"
end
add_index "collection_albums", ["album_id"], :name => "index_collection_albums_on_album_id"
add_index "collection_albums", ["collection_id"], :name => "index_collection_albums_on_collection_id"
create_table "collections", :force => true do |t|
t.string "title", :null => false
t.string "description"
@ -37,6 +42,8 @@ ActiveRecord::Schema.define(:version => 20090604202930) do
t.datetime "updated_at"
end
add_index "collections", ["id"], :name => "index_collections_on_id", :unique => true
create_table "permissions", :force => true do |t|
t.integer "permissible_id"
t.string "permissible_type"
@ -53,6 +60,9 @@ ActiveRecord::Schema.define(:version => 20090604202930) do
t.datetime "updated_at"
end
add_index "photo_tags", ["photo_id"], :name => "index_photo_tags_on_photo_id"
add_index "photo_tags", ["tag_id"], :name => "index_photo_tags_on_tag_id"
create_table "photos", :force => true do |t|
t.string "title", :null => false
t.text "description"
@ -62,8 +72,12 @@ ActiveRecord::Schema.define(:version => 20090604202930) do
t.text "path"
t.float "longitude"
t.float "latitude"
t.string "file"
end
add_index "photos", ["album_id"], :name => "index_photos_on_album_id"
add_index "photos", ["id"], :name => "index_photos_on_id", :unique => true
create_table "role_memberships", :force => true do |t|
t.integer "roleable_id"
t.string "roleable_type"
@ -84,6 +98,8 @@ ActiveRecord::Schema.define(:version => 20090604202930) do
t.datetime "updated_at"
end
add_index "tags", ["id"], :name => "index_tags_on_id", :unique => true
create_table "users", :force => true do |t|
t.string "email", :null => false
t.string "crypted_password", :null => false