scottmotte-amazonavatar 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +117 -0
- data/Rakefile +48 -0
- data/VERSION +1 -0
- data/amazonavatar.gemspec +47 -0
- data/lib/amazonavatar.rb +124 -0
- data/lib/amazonavatar/default.png +0 -0
- data/spec/amazonavatar_spec.rb +7 -0
- data/spec/spec_helper.rb +9 -0
- metadata +65 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 scottmotte
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
= AmazonAvatar
|
2
|
+
|
3
|
+
Upload avatars to amazonS3 on a User (or other) model. Simple and opinionated.
|
4
|
+
|
5
|
+
== Notes
|
6
|
+
|
7
|
+
* I'm using it with merb and mongomapper.
|
8
|
+
* Does resizing using minimagick
|
9
|
+
* Uploads the images to your s3 account at /avatars/user_id/[original.png,thumb.png,mini.png]
|
10
|
+
* Resizes a thumb to 48x48, and mini to 24x24. I told you it was opinionated.
|
11
|
+
|
12
|
+
== Dependencies
|
13
|
+
|
14
|
+
* right_aws
|
15
|
+
* mini_magick
|
16
|
+
|
17
|
+
== Requirements
|
18
|
+
|
19
|
+
You must have ImageMagick installed.
|
20
|
+
|
21
|
+
On leopard
|
22
|
+
|
23
|
+
sudo port install tiff -macosx #disables the linkage with Apple's open gl
|
24
|
+
sudo port install ImageMagick
|
25
|
+
|
26
|
+
On ubuntu
|
27
|
+
|
28
|
+
sudo apt-get install build-essential libmagickcore-dev imagemagick libpcre3 libfcgi-dev libfcgi0ldbl libxml2-dev libxslt1-dev -y
|
29
|
+
|
30
|
+
== Installation
|
31
|
+
|
32
|
+
On your computer
|
33
|
+
|
34
|
+
gem sources -a http://gems.github.com
|
35
|
+
sudo gem install scottmotte-amazonavatar
|
36
|
+
|
37
|
+
Or for bundling
|
38
|
+
|
39
|
+
dependency 'scottmotte-amazonavatar'
|
40
|
+
|
41
|
+
== Configuration
|
42
|
+
|
43
|
+
Tell AmazonAvatar your S3 information.
|
44
|
+
|
45
|
+
AmazonAvatar.access_key_id = "your_access_key_id"
|
46
|
+
AmazonAvatar.secret_access_key = "your_secret_access_key"
|
47
|
+
AmazonAvatar.bucket_name = "yourdatabase_development"
|
48
|
+
# I put these in init.rb in the after_app_loads block for Merb. Probably put them in your environment.rb file for Rails.
|
49
|
+
|
50
|
+
In user.rb model
|
51
|
+
|
52
|
+
class User
|
53
|
+
include AmazonAvatar::Uploader
|
54
|
+
...
|
55
|
+
end
|
56
|
+
|
57
|
+
In your users.rb controller put use the put_avatar instance method. @user.put_avatar.
|
58
|
+
|
59
|
+
def update_avatar(avatar)
|
60
|
+
@user = @current_user
|
61
|
+
raise NotFound unless @user
|
62
|
+
if @user.put_avatar(avatar)
|
63
|
+
redirect resource(@user, :edit), :message => {:notice => "Avatar was updated"}
|
64
|
+
else
|
65
|
+
redirect resource(@user, :edit), :message => {:error => "Avatar failed to be uploaded"}
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
In global_helpers.rb
|
70
|
+
|
71
|
+
module Merb
|
72
|
+
module GlobalHelpers
|
73
|
+
include AmazonAvatar::Helpers
|
74
|
+
...
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
This will give you the avatar_path helper which you can use with rails', sinatra's, merb's, etc's image_tag helper. Just pass in the id of the user to render his avatar.
|
79
|
+
|
80
|
+
Also, you should add a default avatar to each user when they signup. The Paperclip gem does this by pointing to the missing.png avatar by storing the path to the avatar in the user table. This simple gem does not store the path in the database. What's the point. Instead you have to generate the default thumb for each user. There's an instance method for this.
|
81
|
+
|
82
|
+
@user.generate_default_avatar
|
83
|
+
|
84
|
+
You can use it like so:
|
85
|
+
|
86
|
+
def create(user)
|
87
|
+
# session.abandon!
|
88
|
+
@user = User.new(user)
|
89
|
+
if @user.save
|
90
|
+
@user.generate_default_avatar
|
91
|
+
send_mail(UserMailer, :signup, { :from => AppConfig.site.email, :to => @user.email, :subject => "Twinstang welcome" }, { :user => @user })
|
92
|
+
redirect '/login', :message => {:notice => "Signup successful. Log in."}
|
93
|
+
else
|
94
|
+
message[:error] = "Signup failed"
|
95
|
+
render :new
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
Better yet wrap it in a run_later method in merb or put it in a background job in rails.
|
100
|
+
|
101
|
+
...
|
102
|
+
if @user.save
|
103
|
+
run_later do
|
104
|
+
@user.generate_default_avatar
|
105
|
+
send_mail(UserMailer, :signup, { :from => AppConfig.site.email, :to => @user.email, :subject => "Twinstang welcome" }, { :user => @user })
|
106
|
+
end
|
107
|
+
end
|
108
|
+
...
|
109
|
+
|
110
|
+
|
111
|
+
== Usage
|
112
|
+
|
113
|
+
AmazonAvatar.access_key_id = AppConfig.s3.access_key_id # set your key
|
114
|
+
AmazonAvatar.secret_access_key = AppConfig.s3.secret_access_key # set your secret
|
115
|
+
AmazonAvatar.bucket_name = AppConfig.s3.bucket # set the bucket
|
116
|
+
@user.put_avatar(avatar) # where avatar is the file_field value
|
117
|
+
@user.generate_default_avatar # for generating the default avatar from amazonavatar/default.png
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "amazonavatar"
|
8
|
+
gem.summary = %Q{Upload avatars to amazonS3 on a User (or other) model. Simple and opinionated.}
|
9
|
+
gem.email = "scott@scottmotte.com"
|
10
|
+
gem.homepage = "http://github.com/scottmotte/amazonavatar"
|
11
|
+
gem.authors = ["scottmotte"]
|
12
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
13
|
+
end
|
14
|
+
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'spec/rake/spectask'
|
20
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
21
|
+
spec.libs << 'lib' << 'spec'
|
22
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
23
|
+
end
|
24
|
+
|
25
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
26
|
+
spec.libs << 'lib' << 'spec'
|
27
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
28
|
+
spec.rcov = true
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
task :default => :spec
|
33
|
+
|
34
|
+
require 'rake/rdoctask'
|
35
|
+
Rake::RDocTask.new do |rdoc|
|
36
|
+
if File.exist?('VERSION.yml')
|
37
|
+
config = YAML.load(File.read('VERSION.yml'))
|
38
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
39
|
+
else
|
40
|
+
version = ""
|
41
|
+
end
|
42
|
+
|
43
|
+
rdoc.rdoc_dir = 'rdoc'
|
44
|
+
rdoc.title = "amazonavatar #{version}"
|
45
|
+
rdoc.rdoc_files.include('README*')
|
46
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
47
|
+
end
|
48
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{amazonavatar}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["scottmotte"]
|
9
|
+
s.date = %q{2009-07-12}
|
10
|
+
s.email = %q{scott@scottmotte.com}
|
11
|
+
s.extra_rdoc_files = [
|
12
|
+
"LICENSE",
|
13
|
+
"README.rdoc"
|
14
|
+
]
|
15
|
+
s.files = [
|
16
|
+
".document",
|
17
|
+
".gitignore",
|
18
|
+
"LICENSE",
|
19
|
+
"README.rdoc",
|
20
|
+
"Rakefile",
|
21
|
+
"VERSION",
|
22
|
+
"amazonavatar.gemspec",
|
23
|
+
"lib/amazonavatar.rb",
|
24
|
+
"lib/amazonavatar/default.png",
|
25
|
+
"spec/amazonavatar_spec.rb",
|
26
|
+
"spec/spec_helper.rb"
|
27
|
+
]
|
28
|
+
s.homepage = %q{http://github.com/scottmotte/amazonavatar}
|
29
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
30
|
+
s.require_paths = ["lib"]
|
31
|
+
s.rubygems_version = %q{1.3.4}
|
32
|
+
s.summary = %q{Upload avatars to amazonS3 on a User (or other) model. Simple and opinionated.}
|
33
|
+
s.test_files = [
|
34
|
+
"spec/amazonavatar_spec.rb",
|
35
|
+
"spec/spec_helper.rb"
|
36
|
+
]
|
37
|
+
|
38
|
+
if s.respond_to? :specification_version then
|
39
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
40
|
+
s.specification_version = 3
|
41
|
+
|
42
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
43
|
+
else
|
44
|
+
end
|
45
|
+
else
|
46
|
+
end
|
47
|
+
end
|
data/lib/amazonavatar.rb
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'right_aws'
|
3
|
+
gem 'mini_magick'
|
4
|
+
require 'right_aws'
|
5
|
+
require 'mini_magick'
|
6
|
+
|
7
|
+
# Instructions: include in your model with include S3Avatar
|
8
|
+
# then you can use it like @user.put_avatar(avatar)
|
9
|
+
|
10
|
+
module AmazonAvatar
|
11
|
+
|
12
|
+
def self.dir
|
13
|
+
@@dir ||= Pathname(__FILE__).dirname.expand_path + 'amazonavatar'
|
14
|
+
end
|
15
|
+
|
16
|
+
# AmazonAvatar.access_key_id = "typeyouraccesskeyid"
|
17
|
+
def self.access_key_id=(key)
|
18
|
+
@@access_key_id ||= key
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.access_key_id
|
22
|
+
@@access_key_id
|
23
|
+
end
|
24
|
+
|
25
|
+
# AmaazonAvatar.secret_access_key = "typeyoursecretaccesskey"
|
26
|
+
def self.secret_access_key=(secret)
|
27
|
+
@@secret_access_key ||= secret
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.secret_access_key
|
31
|
+
@@secret_access_key
|
32
|
+
end
|
33
|
+
|
34
|
+
# Defin connection using RightAws
|
35
|
+
def self.connection
|
36
|
+
@@connection ||= RightAws::S3.new(access_key_id, secret_access_key)
|
37
|
+
end
|
38
|
+
|
39
|
+
# AmazonAvatar.bucket = "bucket_name"
|
40
|
+
def self.bucket_name=(bucket)
|
41
|
+
@@bucket_name ||= bucket
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.bucket_name
|
45
|
+
@@bucket_name
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
module Helpers
|
50
|
+
def avatar_path(id, style = :original)
|
51
|
+
"http://s3.amazonaws.com/#{AmazonAvatar.bucket_name}/avatars/#{id}/#{style.to_s}.png"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Adds model methods
|
56
|
+
# usage:
|
57
|
+
# class User
|
58
|
+
# include AmazonAvatar::Uploader
|
59
|
+
# end
|
60
|
+
# now you can use @user.put_avatar(avatar)
|
61
|
+
module Uploader
|
62
|
+
module InstanceMethods
|
63
|
+
|
64
|
+
# class variables
|
65
|
+
@@avatars ||= {
|
66
|
+
:original => {:filename => "original.png", :content_type => "image/png"},
|
67
|
+
:thumb => {:filename => "thumb.png", :dimensions => "48x48", :content_type => "image/png"},
|
68
|
+
:mini => {:filename => "mini.png", :dimensions => "24x24", :content_type => "image/png"}
|
69
|
+
}
|
70
|
+
|
71
|
+
# Connect to bucket
|
72
|
+
def bucket
|
73
|
+
@bucket ||= AmazonAvatar.connection.bucket(AmazonAvatar.bucket_name, true, 'public-read')
|
74
|
+
end
|
75
|
+
|
76
|
+
# upload to s3
|
77
|
+
def put_avatar(avatar)
|
78
|
+
# Original
|
79
|
+
key1 = bucket.key("avatars/#{id}/#{@@avatars[:original][:filename]}")
|
80
|
+
key1.put(avatar[:tempfile].read, 'public-read')
|
81
|
+
|
82
|
+
# data
|
83
|
+
data = MiniMagick::Image.from_file(avatar[:tempfile].path)
|
84
|
+
# thumb
|
85
|
+
key2 = bucket.key("avatars/#{id}/#{@@avatars[:thumb][:filename]}")
|
86
|
+
key2.put(resize_and_crop(data, @@avatars[:thumb][:dimensions]).to_blob, 'public-read')
|
87
|
+
# mini
|
88
|
+
key3 = bucket.key("avatars/#{id}/#{@@avatars[:mini][:filename]}")
|
89
|
+
key3.put(resize_and_crop(data, @@avatars[:mini][:dimensions]).to_blob, 'public-read')
|
90
|
+
end
|
91
|
+
|
92
|
+
# upload default start avatars. this needs to be replaced with a default image somehow
|
93
|
+
def generate_default_avatar
|
94
|
+
# Original
|
95
|
+
key1 = bucket.key("avatars/#{id}/#{@@avatars[:original][:filename]}")
|
96
|
+
key1.put(File.new("#{AmazonAvatar.dir}/default.png").read, 'public-read')
|
97
|
+
|
98
|
+
# image
|
99
|
+
image = MiniMagick::Image.from_file("#{AmazonAvatar.dir}/default.png")
|
100
|
+
# thumb
|
101
|
+
key2 = bucket.key("avatars/#{id}/#{@@avatars[:thumb][:filename]}")
|
102
|
+
key2.put(resize_and_crop(image, @@avatars[:thumb][:dimensions]).to_blob, 'public-read')
|
103
|
+
# mini
|
104
|
+
key3 = bucket.key("avatars/#{id}/#{@@avatars[:mini][:filename]}")
|
105
|
+
key3.put(resize_and_crop(image, @@avatars[:mini][:dimensions]).to_blob, 'public-read')
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
def resize_and_crop(image, size)
|
110
|
+
if image[:width] < image[:height]
|
111
|
+
remove = ((image[:height] - image[:width])/2).round
|
112
|
+
image.shave("0x#{remove}")
|
113
|
+
elsif image[:width] > image[:height]
|
114
|
+
remove = ((image[:width] - image[:height])/2).round
|
115
|
+
image.shave("#{remove}x0")
|
116
|
+
end
|
117
|
+
image.resize("#{size}x#{size}")
|
118
|
+
return image
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
include InstanceMethods
|
123
|
+
end
|
124
|
+
end
|
Binary file
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: scottmotte-amazonavatar
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- scottmotte
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-12 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: scott@scottmotte.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- .document
|
27
|
+
- .gitignore
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
- Rakefile
|
31
|
+
- VERSION
|
32
|
+
- amazonavatar.gemspec
|
33
|
+
- lib/amazonavatar.rb
|
34
|
+
- lib/amazonavatar/default.png
|
35
|
+
- spec/amazonavatar_spec.rb
|
36
|
+
- spec/spec_helper.rb
|
37
|
+
has_rdoc: false
|
38
|
+
homepage: http://github.com/scottmotte/amazonavatar
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options:
|
41
|
+
- --charset=UTF-8
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.2.0
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: Upload avatars to amazonS3 on a User (or other) model. Simple and opinionated.
|
63
|
+
test_files:
|
64
|
+
- spec/amazonavatar_spec.rb
|
65
|
+
- spec/spec_helper.rb
|