Skip to content

Commit

Permalink
Added portraits importing and resizing so that portraits are served l…
Browse files Browse the repository at this point in the history
…ocally with optimal performance.
  • Loading branch information
fredwu committed Jan 6, 2011
1 parent 31cd6ed commit 2d4983f
Show file tree
Hide file tree
Showing 49 changed files with 125 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.bundle
db/rubycommitters_development.db
db/rubycommitters_test.db
log/*.log
tmp/**/*
16 changes: 9 additions & 7 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -85,6 +90,8 @@ PLATFORMS

DEPENDENCIES
activerecord (~> 3.0.3)
carrierwave
mini_magick
padrino (= 0.9.19)
rack-cache (~> 1.0)
rack-flash
Expand Down
17 changes: 15 additions & 2 deletions app/models/hero.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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|
Expand Down
1 change: 1 addition & 0 deletions app/models/portrait.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class Portrait < ActiveRecord::Base
mount_uploader :file, PortraitUploader
belongs_to :hero
end
2 changes: 1 addition & 1 deletion app/views/heroes/_portrait.html.slim
Original file line number Diff line number Diff line change
@@ -1 +1 @@
li== image_tag portrait.url
li== image_tag portrait.file.url
1 change: 1 addition & 0 deletions db/migrate/005_create_portraits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file modified db/rubycommitters_production.db
Binary file not shown.
1 change: 1 addition & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
86 changes: 86 additions & 0 deletions lib/portrait_uploader.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions lib/tasks/import.rake
Original file line number Diff line number Diff line change
@@ -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
Binary file added public/images/portraits/aamine_3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/akira_4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/akr_6.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/arton_8.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/arton_9.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/azav_10.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/dave_11.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/davidflanagan_12.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/dblack_13.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/drbrain_14.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/duerst_15.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/gotoken_17.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/gotoyuzo_18.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/jeg2_20.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/jim_21.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/jim_22.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/keiju_26.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/knu_27.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/ko1_28.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/kosaki_29.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/kou_31.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/kouji_32.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/luislavena_34.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/luislavena_35.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/marcandre_37.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/portraits/matz_1.jpg
Binary file added public/images/portraits/mrkn_40.jpg
Binary file added public/images/portraits/nobu_45.jpg
Binary file added public/images/portraits/seki_49.jpg
Binary file added public/images/portraits/shugo_52.jpg
Binary file added public/images/portraits/shyouhei_53.jpg
Binary file added public/images/portraits/takano32_56.jpg
Binary file added public/images/portraits/tenderlove_60.jpg
Binary file added public/images/portraits/wew_67.jpg
Binary file added public/images/portraits/why_68.jpg
Binary file added public/images/portraits/yugui_71.jpg
Binary file added public/images/portraits/zsombor_72.jpg
4 changes: 0 additions & 4 deletions public/stylesheets/base.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
th {
text-align: right;
vertical-align: top;
}

img {
height: 100px;
}

1 comment on commit 2d4983f

@nesquena
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.