From f52d6ba7915c523bc58d5e1bd590624885857754 Mon Sep 17 00:00:00 2001 From: Espen Antonsen Date: Mon, 12 Apr 2010 18:40:47 -0400 Subject: [PATCH] carrierwave upload, specified in photo model. create account at startup. auto add to admin role --- .gems | 3 +- .gitignore | 3 +- app/controllers/application_controller.rb | 6 ++ app/controllers/photos_controller.rb | 2 +- app/controllers/users_controller.rb | 6 +- app/models/photo.rb | 48 ++++----------- app/uploaders/file_uploader.rb | 58 +++++++++++++++++++ app/views/collections/_form.html.erb | 6 +- app/views/photos/upload.html.erb | 4 +- config/environment.rb | 4 +- .../20100412220801_add_file_to_photo.rb | 9 +++ db/schema.rb | 18 +++++- 12 files changed, 119 insertions(+), 48 deletions(-) create mode 100644 app/uploaders/file_uploader.rb create mode 100644 db/migrate/20100412220801_add_file_to_photo.rb diff --git a/.gems b/.gems index c66edb2..99acd23 100644 --- a/.gems +++ b/.gems @@ -1 +1,2 @@ -authlogic \ No newline at end of file +authlogic +carrierwave diff --git a/.gitignore b/.gitignore index a62e6d0..152b567 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ config/deploy.rb config/deploy Capfile photos -public/thumbs \ No newline at end of file +public/thumbs +public/uploads diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6ea93a2..41d7565 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index 89fa849..36cd50b 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -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 } diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 4ed6f42..7c8c2e1 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 diff --git a/app/models/photo.rb b/app/models/photo.rb index 6270195..7ebfb3f 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -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 diff --git a/app/uploaders/file_uploader.rb b/app/uploaders/file_uploader.rb new file mode 100644 index 0000000..1e1e6c4 --- /dev/null +++ b/app/uploaders/file_uploader.rb @@ -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 diff --git a/app/views/collections/_form.html.erb b/app/views/collections/_form.html.erb index 72f7297..19afc66 100644 --- a/app/views/collections/_form.html.erb +++ b/app/views/collections/_form.html.erb @@ -4,8 +4,8 @@ <%= form.label :description %>
<%= form.text_area :description %>
+<% unless @collection.albums.empty? %> <%= form.label :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) %> -

\ No newline at end of file +

+ +<% end %> diff --git a/app/views/photos/upload.html.erb b/app/views/photos/upload.html.erb index 055a89d..e8ab10a 100644 --- a/app/views/photos/upload.html.erb +++ b/app/views/photos/upload.html.erb @@ -3,7 +3,7 @@ <% end %>
- +
diff --git a/config/environment.rb b/config/environment.rb index 248db0c..575c2cd 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -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 ) diff --git a/db/migrate/20100412220801_add_file_to_photo.rb b/db/migrate/20100412220801_add_file_to_photo.rb new file mode 100644 index 0000000..131153e --- /dev/null +++ b/db/migrate/20100412220801_add_file_to_photo.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index dba0b1f..5f587d0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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