Permalink
Browse files

Added portraits importing and resizing so that portraits are served l…

…ocally with optimal performance.
  • Loading branch information...
1 parent 31cd6ed commit 2d4983fc6f7fcb5ba6dc96ba54ed0a0ab1a62cb7 @fredwu committed Jan 6, 2011
Showing with 125 additions and 14 deletions.
  1. +2 −0 .gitignore
  2. +9 −7 Gemfile
  3. +7 −0 Gemfile.lock
  4. +15 −2 app/models/hero.rb
  5. +1 −0 app/models/portrait.rb
  6. +1 −1 app/views/heroes/_portrait.html.slim
  7. +1 −0 db/migrate/005_create_portraits.rb
  8. BIN db/rubycommitters_production.db
  9. +1 −0 db/schema.rb
  10. +86 −0 lib/portrait_uploader.rb
  11. +2 −0 lib/tasks/import.rake
  12. BIN public/images/portraits/aamine_3.jpg
  13. BIN public/images/portraits/akira_4.jpg
  14. BIN public/images/portraits/akr_6.jpg
  15. BIN public/images/portraits/arton_8.jpg
  16. BIN public/images/portraits/arton_9.jpg
  17. BIN public/images/portraits/azav_10.jpg
  18. BIN public/images/portraits/dave_11.jpg
  19. BIN public/images/portraits/davidflanagan_12.jpg
  20. BIN public/images/portraits/dblack_13.jpg
  21. BIN public/images/portraits/drbrain_14.jpg
  22. BIN public/images/portraits/duerst_15.jpg
  23. BIN public/images/portraits/gotoken_17.jpg
  24. BIN public/images/portraits/gotoyuzo_18.jpg
  25. BIN public/images/portraits/jeg2_20.jpg
  26. BIN public/images/portraits/jim_21.jpg
  27. BIN public/images/portraits/jim_22.jpg
  28. BIN public/images/portraits/keiju_26.jpg
  29. BIN public/images/portraits/knu_27.jpg
  30. BIN public/images/portraits/ko1_28.jpg
  31. BIN public/images/portraits/kosaki_29.jpg
  32. BIN public/images/portraits/kou_31.jpg
  33. BIN public/images/portraits/kouji_32.jpg
  34. BIN public/images/portraits/luislavena_34.png
  35. BIN public/images/portraits/luislavena_35.jpg
  36. BIN public/images/portraits/marcandre_37.jpg
  37. BIN public/images/portraits/matz_1.jpg
  38. BIN public/images/portraits/mrkn_40.jpg
  39. BIN public/images/portraits/nobu_45.jpg
  40. BIN public/images/portraits/seki_49.jpg
  41. BIN public/images/portraits/shugo_52.jpg
  42. BIN public/images/portraits/shyouhei_53.jpg
  43. BIN public/images/portraits/takano32_56.jpg
  44. BIN public/images/portraits/tenderlove_60.jpg
  45. BIN public/images/portraits/wew_67.jpg
  46. BIN public/images/portraits/why_68.jpg
  47. BIN public/images/portraits/yugui_71.jpg
  48. BIN public/images/portraits/zsombor_72.jpg
  49. +0 −4 public/stylesheets/base.css
View
@@ -1,3 +1,5 @@
.bundle
+db/rubycommitters_development.db
+db/rubycommitters_test.db
log/*.log
tmp/**/*
View
16 Gemfile
@@ -3,17 +3,19 @@ source :rubygems
# Project requirements
gem 'rake'
gem 'rack-flash'
-gem 'thin' # or mongrel
+gem 'thin', :group => :development
# Component requirements
-gem 'slim', '>= 0.8.3'
-gem 'sqlite3-ruby', :require => 'sqlite3'
+gem 'slim', '>= 0.8.3'
+gem 'sqlite3-ruby', :require => 'sqlite3'
gem 'activerecord', '~> 3.0.3', :require => 'active_record'
-gem 'rack-cache', '~> 1.0', :require => 'rack/cache'
+gem 'rack-cache', '~> 1.0', :require => 'rack/cache'
+gem 'mini_magick'
+gem 'carrierwave'
# Test requirements
-gem 'rack-test', :group => :test, :require => 'rack/test'
+gem 'rack-test', :require => 'rack/test', :group => :test
# Padrino
-gem 'sinatra', '1.1.0', :require => 'sinatra/base'
-gem 'padrino', '0.9.19'
+gem 'sinatra', '1.1.0', :require => 'sinatra/base'
+gem 'padrino', '0.9.19'
View
@@ -13,6 +13,8 @@ GEM
activesupport (3.0.3)
arel (2.0.6)
builder (2.1.2)
+ carrierwave (0.5.1)
+ activesupport (~> 3.0)
daemons (1.1.0)
eventmachine (0.12.10)
git (1.2.5)
@@ -26,6 +28,8 @@ GEM
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.16)
+ mini_magick (3.1)
+ subexec (~> 0.0.4)
padrino (0.9.19)
padrino-admin (= 0.9.19)
padrino-core (= 0.9.19)
@@ -67,6 +71,7 @@ GEM
temple (~> 0.1.6)
tilt (~> 1.1)
sqlite3-ruby (1.3.2)
+ subexec (0.0.4)
temple (0.1.6)
thin (1.2.7)
daemons (>= 1.0.9)
@@ -85,6 +90,8 @@ PLATFORMS
DEPENDENCIES
activerecord (~> 3.0.3)
+ carrierwave
+ mini_magick
padrino (= 0.9.19)
rack-cache (~> 1.0)
rack-flash
View
@@ -8,6 +8,7 @@ class Hero < ActiveRecord::Base
def self.import(io)
require 'yaml'
+ require 'open-uri'
entries = YAML.load(io)
entries.each do |entry|
@@ -30,8 +31,20 @@ def self.import(io)
)
end
- (entry['portraits'] || []).each do |portrait|
- hero.portraits.create!(:url => portrait)
+ if entry['portraits']
+ entry['portraits'].each do |url|
+ portrait = hero.portraits.create!(:url => url)
+ begin
+ portrait.remote_file_url = URI.unescape(url) # caters for utf-8 strings
+ portrait.save!
+ rescue OpenURI::HTTPError => e
+ puts e.message
+ puts "Error retrieving #{url}"
+ end
+ end
+ else
+ # creates a dummy portrait entry so that Carrierwave could fallback
+ hero.portraits.create!(:url => "")
end
(entry['services'] || []).each do |name, key|
View
@@ -1,3 +1,4 @@
class Portrait < ActiveRecord::Base
+ mount_uploader :file, PortraitUploader
belongs_to :hero
end
@@ -1 +1 @@
-li== image_tag portrait.url
+li== image_tag portrait.file.url
@@ -2,6 +2,7 @@ class CreatePortraits < ActiveRecord::Migration
def self.up
create_table :portraits do |t|
t.string :url
+ t.text :file
t.references :hero
t.timestamps
end
Binary file not shown.
View
@@ -41,6 +41,7 @@
create_table "portraits", :force => true do |t|
t.string "url"
+ t.text "file"
t.integer "hero_id"
t.datetime "created_at"
t.datetime "updated_at"
View
@@ -0,0 +1,86 @@
+class PortraitUploader < CarrierWave::Uploader::Base
+
+ ##
+ # Image manipulator library:
+ #
+ # include CarrierWave::RMagick
+ # include CarrierWave::ImageScience
+ include CarrierWave::MiniMagick
+
+ ##
+ # Storage type
+ #
+ storage :file
+ #
+ # configure do |config|
+ # config.s3_access_key_id = 'xxxx'
+ # config.s3_secret_access_key = 'xxxx'
+ # config.s3_bucket = 'assets-web'
+ # end
+ #
+ # storage :right_s3
+
+
+ ## Manually set root
+ def root; File.join(Padrino.root,"public/"); end
+
+ ##
+ # Directory where uploaded files will be stored (default is /public/uploads)
+ #
+ def store_dir
+ 'images/portraits/'
+ end
+
+ ##
+ # Directory where uploaded temp files will be stored (default is [root]/tmp)
+ #
+ def cache_dir
+ Padrino.root("tmp")
+ end
+
+ ##
+ # Default URL as a default if there hasn't been a file uploaded
+ #
+ def default_url
+ 'http://placehold.it/100x100'
+ end
+
+ ##
+ # Process files as they are uploaded.
+ #
+ process :resize_to_fit => [100, 100]
+ # version :thumb do
+ # process :resize_to_fill => [100, 100]
+ # end
+ #
+ # def scale(width, height)
+ # # do something
+ # end
+
+ ##
+ # Create different versions of your uploaded files
+ #
+ # version :header do
+ # process :resize_to_fill => [940, 250]
+ # version :thumb do
+ # process :resize_to_fill => [230, 85]
+ # end
+ # end
+ ##
+ # White list of extensions which are allowed to be uploaded:
+ #
+ # def extension_white_list
+ # %w(jpg jpeg gif png)
+ # end
+
+ ##
+ # Override the filename of the uploaded files
+ #
+ def filename
+ if original_filename
+ original_extname = File.extname(original_filename)
+ "#{model.hero.username}_#{model.id}#{original_extname.blank? ? '.jpg' : original_extname}"
+ end
+ end
+ #
+end
View
@@ -1,5 +1,7 @@
desc "import the list of ruby committers"
task :import => ['ar:drop', 'ar:migrate'] do
require 'open-uri'
+ require 'fileutils'
+ FileUtils.rm_rf File.join(Padrino.root, 'public', 'images', 'portraits')
Hero.import open('http://svn.coderepos.org/share/lang/ruby/ruby-committers/ruby-committers.yml')
end
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
@@ -1,8 +1,4 @@
th {
text-align: right;
vertical-align: top;
-}
-
-img {
- height: 100px;
}

1 comment on commit 2d4983f

Awesome! Excited to see this Padrino version of rubycommiters. Thanks for doing this!

Please sign in to comment.