carrierwave-aliyun 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ uploads
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour --format nested
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://ruby.taobao.org"
2
+ gem "rake"
3
+
4
+ group :test do
5
+ gem 'rails'
6
+ gem 'sqlite3-ruby', :require => 'sqlite3'
7
+ gem 'carrierwave'
8
+ gem 'mini_magick'
9
+ gem 'rest-client'
10
+ gem 'rspec','~> 2.6.0'
11
+ gem 'mocha','0.10.0'
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,114 @@
1
+ GEM
2
+ remote: http://ruby.taobao.org/
3
+ specs:
4
+ actionmailer (3.2.1)
5
+ actionpack (= 3.2.1)
6
+ mail (~> 2.4.0)
7
+ actionpack (3.2.1)
8
+ activemodel (= 3.2.1)
9
+ activesupport (= 3.2.1)
10
+ builder (~> 3.0.0)
11
+ erubis (~> 2.7.0)
12
+ journey (~> 1.0.1)
13
+ rack (~> 1.4.0)
14
+ rack-cache (~> 1.1)
15
+ rack-test (~> 0.6.1)
16
+ sprockets (~> 2.1.2)
17
+ activemodel (3.2.1)
18
+ activesupport (= 3.2.1)
19
+ builder (~> 3.0.0)
20
+ activerecord (3.2.1)
21
+ activemodel (= 3.2.1)
22
+ activesupport (= 3.2.1)
23
+ arel (~> 3.0.0)
24
+ tzinfo (~> 0.3.29)
25
+ activeresource (3.2.1)
26
+ activemodel (= 3.2.1)
27
+ activesupport (= 3.2.1)
28
+ activesupport (3.2.1)
29
+ i18n (~> 0.6)
30
+ multi_json (~> 1.0)
31
+ arel (3.0.0)
32
+ builder (3.0.0)
33
+ carrierwave (0.5.8)
34
+ activesupport (~> 3.0)
35
+ diff-lcs (1.1.3)
36
+ erubis (2.7.0)
37
+ hike (1.2.1)
38
+ i18n (0.6.0)
39
+ journey (1.0.1)
40
+ json (1.6.5)
41
+ mail (2.4.1)
42
+ i18n (>= 0.4.0)
43
+ mime-types (~> 1.16)
44
+ treetop (~> 1.4.8)
45
+ metaclass (0.0.1)
46
+ mime-types (1.17.2)
47
+ mini_magick (3.4)
48
+ subexec (~> 0.2.1)
49
+ mocha (0.10.0)
50
+ metaclass (~> 0.0.1)
51
+ multi_json (1.0.4)
52
+ polyglot (0.3.3)
53
+ rack (1.4.1)
54
+ rack-cache (1.1)
55
+ rack (>= 0.4)
56
+ rack-ssl (1.3.2)
57
+ rack
58
+ rack-test (0.6.1)
59
+ rack (>= 1.0)
60
+ rails (3.2.1)
61
+ actionmailer (= 3.2.1)
62
+ actionpack (= 3.2.1)
63
+ activerecord (= 3.2.1)
64
+ activeresource (= 3.2.1)
65
+ activesupport (= 3.2.1)
66
+ bundler (~> 1.0)
67
+ railties (= 3.2.1)
68
+ railties (3.2.1)
69
+ actionpack (= 3.2.1)
70
+ activesupport (= 3.2.1)
71
+ rack-ssl (~> 1.3.2)
72
+ rake (>= 0.8.7)
73
+ rdoc (~> 3.4)
74
+ thor (~> 0.14.6)
75
+ rake (0.9.2.2)
76
+ rdoc (3.12)
77
+ json (~> 1.4)
78
+ rest-client (1.6.7)
79
+ mime-types (>= 1.16)
80
+ rspec (2.6.0)
81
+ rspec-core (~> 2.6.0)
82
+ rspec-expectations (~> 2.6.0)
83
+ rspec-mocks (~> 2.6.0)
84
+ rspec-core (2.6.4)
85
+ rspec-expectations (2.6.0)
86
+ diff-lcs (~> 1.1.2)
87
+ rspec-mocks (2.6.0)
88
+ sprockets (2.1.2)
89
+ hike (~> 1.2)
90
+ rack (~> 1.0)
91
+ tilt (~> 1.1, != 1.3.0)
92
+ sqlite3 (1.3.5)
93
+ sqlite3-ruby (1.3.3)
94
+ sqlite3 (>= 1.3.3)
95
+ subexec (0.2.1)
96
+ thor (0.14.6)
97
+ tilt (1.3.3)
98
+ treetop (1.4.10)
99
+ polyglot
100
+ polyglot (>= 0.3.1)
101
+ tzinfo (0.3.31)
102
+
103
+ PLATFORMS
104
+ ruby
105
+
106
+ DEPENDENCIES
107
+ carrierwave
108
+ mini_magick
109
+ mocha (= 0.10.0)
110
+ rails
111
+ rake
112
+ rest-client
113
+ rspec (~> 2.6.0)
114
+ sqlite3-ruby
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # CarrierWave for Aliyun OSS
2
+
3
+ This gem adds support for [Aliyun OSS](http://oss.aliyun.com) to [CarrierWave](https://github.com/jnicklas/carrierwave/)
4
+
5
+ ## Installation
6
+
7
+ gem install carrierwave-aliyun
8
+
9
+ ## Using Bundler
10
+
11
+ gem 'rest-client'
12
+ gem 'carrierwave-aliyun'
13
+
14
+ ## Configuration
15
+
16
+ You'll need to configure the to use this in config/initializes/carrierwave.rb
17
+
18
+ ```ruby
19
+ CarrierWave.configure do |config|
20
+ config.storage = :aliyun
21
+ config.aliyun_access_id = "xxxxxx"
22
+ config.aliyun_access_key = 'xxxxxx'
23
+ # you need create this bucket first!
24
+ config.aliyun_bucket = "simple"
25
+ end
26
+ ```
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "carrierwave-aliyun"
6
+ s.version = "0.1.0"
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Jason Lee"]
9
+ s.email = ["huacnlee@gmail.com"]
10
+ s.homepage = "https://github.com/nowa/carrierwave-aliyun"
11
+ s.summary = %q{Aliyun OSS support for Carrierwave}
12
+ s.description = %q{Aliyun OSS support for Carrierwave}
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ s.require_paths = ["lib"]
17
+
18
+ s.add_dependency "carrierwave", [">= 0.5.7"]
19
+ s.add_dependency "rest-client", [">= 1.6.7"]
20
+ s.add_development_dependency "rspec", ["~> 2.6"]
21
+ s.add_development_dependency "rake", ["~> 0.9"]
22
+ end
@@ -0,0 +1,6 @@
1
+ require "carrierwave/storage/aliyun"
2
+ require "carrierwave/aliyun/configuration"
3
+ CarrierWave.configure do |config|
4
+ config.storage_engines.merge!({:aliyun => "CarrierWave::Storage::Aliyun"})
5
+ end
6
+ CarrierWave::Uploader::Base.send(:include, CarrierWave::Aliyun::Configuration)
@@ -0,0 +1,35 @@
1
+ module CarrierWave
2
+ module Aliyun
3
+ module Configuration
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ add_config :aliyun_access_id
7
+ add_config :aliyun_access_key
8
+ add_config :aliyun_bucket
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ def add_config(name)
14
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
15
+ def self.#{name}(value=nil)
16
+ @#{name} = value if value
17
+ return @#{name} if self.object_id == #{self.object_id} || defined?(@#{name})
18
+ name = superclass.#{name}
19
+ return nil if name.nil? && !instance_variable_defined?("@#{name}")
20
+ @#{name} = name && !name.is_a?(Module) && !name.is_a?(Symbol) && !name.is_a?(Numeric) && !name.is_a?(TrueClass) && !name.is_a?(FalseClass) ? name.dup : name
21
+ end
22
+
23
+ def self.#{name}=(value)
24
+ @#{name} = value
25
+ end
26
+
27
+ def #{name}
28
+ value = self.class.#{name}
29
+ value.instance_of?(Proc) ? value.call : value
30
+ end
31
+ RUBY
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,136 @@
1
+ # encoding: utf-8
2
+ require 'carrierwave'
3
+ require 'digest/hmac'
4
+ require 'digest/md5'
5
+ require "rest-client"
6
+
7
+ module CarrierWave
8
+ module Storage
9
+ class Aliyun < Abstract
10
+
11
+ class Connection
12
+ def initialize(options={})
13
+ @aliyun_access_id = options[:aliyun_access_id]
14
+ @aliyun_access_key = options[:aliyun_access_key]
15
+ @aliyun_bucket = options[:aliyun_bucket]
16
+ end
17
+
18
+ def put(path, file)
19
+ content_md5 = Digest::MD5.hexdigest(file)
20
+ content_type = "image/jpg"
21
+ date = Time.now.gmtime.strftime("%a, %d %b %Y %H:%M:%S GMT")
22
+ path = "#{@aliyun_bucket}/#{path}"
23
+ url = "http://storage.aliyun.com/#{path}"
24
+ auth_sign = sign("PUT", path, content_md5, content_type ,date)
25
+ headers = {
26
+ "Authorization" => auth_sign,
27
+ "Content-Type" => content_type,
28
+ "Content-Length" => file.length,
29
+ "Date" => date,
30
+ "Host" => "storage.aliyun.com",
31
+ "Expect" => "100-Continue"
32
+ }
33
+ response = RestClient.put(url, file, headers)
34
+ end
35
+
36
+ def get(path)
37
+ @http.get(path)
38
+ end
39
+
40
+ private
41
+ def sign(verb, path, content_md5 = '', content_type = '', date)
42
+ canonicalized_oss_headers = ''
43
+ canonicalized_resource = "/#{path}"
44
+ string_to_sign = "#{verb}\n\n#{content_type}\n#{date}\n#{canonicalized_oss_headers}#{canonicalized_resource}"
45
+ digest = OpenSSL::Digest::Digest.new('sha1')
46
+ h = OpenSSL::HMAC.digest(digest, @aliyun_access_key, string_to_sign)
47
+ h = Base64.encode64(h)
48
+ "OSS #{@aliyun_access_id}:#{h}"
49
+ end
50
+ end
51
+
52
+ class File
53
+ def initialize(uploader, base, path)
54
+ @uploader = uploader
55
+ @path = path
56
+ @base = base
57
+ end
58
+
59
+ ##
60
+ # Returns the current path/filename of the file on Cloud Files.
61
+ #
62
+ # === Returns
63
+ #
64
+ # [String] A path
65
+ #
66
+ def path
67
+ @path
68
+ end
69
+
70
+ ##
71
+ # Reads the contents of the file from Cloud Files
72
+ #
73
+ # === Returns
74
+ #
75
+ # [String] contents of the file
76
+ #
77
+ def read
78
+ object = oss_connection.get(@path)
79
+ object.data
80
+ end
81
+
82
+ ##
83
+ # Remove the file from Cloud Files
84
+ #
85
+ def delete
86
+ begin
87
+ oss_connection.delete(@path)
88
+ true
89
+ rescue Exception => e
90
+ # If the file's not there, don't panic
91
+ nil
92
+ end
93
+ end
94
+
95
+ def url
96
+ "http://storage.aliyun.com/#{@uploader.aliyun_bucket}/#{@path}"
97
+ end
98
+
99
+ def store(data)
100
+ oss_connection.put(@path, data)
101
+ end
102
+
103
+ private
104
+
105
+ def headers
106
+ @headers ||= { }
107
+ end
108
+
109
+ def connection
110
+ @base.connection
111
+ end
112
+
113
+ def oss_connection
114
+ return @oss_connection if @oss_connection
115
+
116
+ config = {:aliyun_access_id => @uploader.aliyun_access_id,
117
+ :aliyun_access_key => @uploader.aliyun_access_key,
118
+ :aliyun_bucket => @uploader.aliyun_bucket
119
+ }
120
+ @oss_connection ||= CarrierWave::Storage::Aliyun::Connection.new(config)
121
+ end
122
+
123
+ end
124
+
125
+ def store!(file)
126
+ f = CarrierWave::Storage::Aliyun::File.new(uploader, self, uploader.store_path)
127
+ f.store(file.read)
128
+ f
129
+ end
130
+
131
+ def retrieve!(identifier)
132
+ CarrierWave::Storage::Aliyun::File.new(uploader, self, uploader.store_path(identifier))
133
+ end
134
+ end
135
+ end
136
+ end
data/spec/foo.gif ADDED
Binary file
data/spec/foo.jpg ADDED
Binary file
@@ -0,0 +1,36 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+ require 'rspec/autorun'
4
+ require 'rails'
5
+ require 'active_record'
6
+ require "carrierwave"
7
+ require 'carrierwave/orm/activerecord'
8
+ require 'carrierwave/processing/mini_magick'
9
+
10
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
11
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
12
+
13
+ require "carrierwave-aliyun"
14
+
15
+
16
+ module Rails
17
+ class <<self
18
+ def root
19
+ [File.expand_path(__FILE__).split('/')[0..-3].join('/'),"spec"].join("/")
20
+ end
21
+ end
22
+ end
23
+
24
+ ActiveRecord::Migration.verbose = false
25
+
26
+ # 测试的时候需要修改这个地方
27
+ CarrierWave.configure do |config|
28
+ config.storage = :aliyun
29
+ config.aliyun_access_id = "7ewl4zm3mhi45vko9zx022ul"
30
+ config.aliyun_access_key = 'Ajpi7IRKDKdXYHHFFoS89uQJQE8='
31
+ config.aliyun_bucket = "carrierwave"
32
+ end
33
+
34
+ def load_file(fname)
35
+ File.open([Rails.root,fname].join("/"))
36
+ end
@@ -0,0 +1,73 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ require "open-uri"
4
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
5
+
6
+ describe "Upload" do
7
+ def setup_db
8
+ ActiveRecord::Schema.define(:version => 1) do
9
+ create_table :photos do |t|
10
+ t.column :image, :string
11
+ end
12
+ end
13
+ end
14
+
15
+ def drop_db
16
+ ActiveRecord::Base.connection.tables.each do |table|
17
+ ActiveRecord::Base.connection.drop_table(table)
18
+ end
19
+ end
20
+
21
+ class PhotoUploader < CarrierWave::Uploader::Base
22
+ include CarrierWave::MiniMagick
23
+
24
+ version :small do
25
+ process :resize_to_fill => [120, 120]
26
+ end
27
+
28
+ def store_dir
29
+ "photos"
30
+ end
31
+ end
32
+
33
+ class Photo < ActiveRecord::Base
34
+ mount_uploader :image, PhotoUploader
35
+ end
36
+
37
+
38
+ before :all do
39
+ setup_db
40
+ end
41
+
42
+ after :all do
43
+ drop_db
44
+ end
45
+
46
+ describe "Upload Image" do
47
+ context "should upload image" do
48
+ before(:all) do
49
+ @file = load_file("foo.jpg")
50
+ @file1 = load_file("foo.gif")
51
+ @photo = Photo.new(:image => @file)
52
+ @photo1 = Photo.new(:image => @file1)
53
+ end
54
+
55
+ it "should upload file" do
56
+ @photo.save.should be_true
57
+ @photo1.save.should be_true
58
+ end
59
+
60
+ it "should get uploaded file" do
61
+ img = open(@photo.image.url)
62
+ img.size.should == @file.size
63
+ img1 = open(@photo1.image.url)
64
+ img1.size.should == @file1.size
65
+ end
66
+
67
+ it "sholud get small version uploaded file" do
68
+ open(@photo.image.small.url).should_not == nil
69
+ open(@photo1.image.small.url).should_not == nil
70
+ end
71
+ end
72
+ end
73
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-aliyun
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jason Lee
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: carrierwave
16
+ requirement: &70172009111240 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.5.7
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70172009111240
25
+ - !ruby/object:Gem::Dependency
26
+ name: rest-client
27
+ requirement: &70172009110740 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 1.6.7
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70172009110740
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70172009110260 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '2.6'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70172009110260
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: &70172009109780 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '0.9'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70172009109780
58
+ description: Aliyun OSS support for Carrierwave
59
+ email:
60
+ - huacnlee@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - .gitignore
66
+ - .rspec
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - README.md
70
+ - carrierwave-aliyun.gemspec
71
+ - lib/carrierwave-aliyun.rb
72
+ - lib/carrierwave/aliyun/configuration.rb
73
+ - lib/carrierwave/storage/aliyun.rb
74
+ - spec/foo.gif
75
+ - spec/foo.jpg
76
+ - spec/spec_helper.rb
77
+ - spec/upload_spec.rb
78
+ homepage: https://github.com/nowa/carrierwave-aliyun
79
+ licenses: []
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 1.8.10
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Aliyun OSS support for Carrierwave
102
+ test_files:
103
+ - spec/foo.gif
104
+ - spec/foo.jpg
105
+ - spec/spec_helper.rb
106
+ - spec/upload_spec.rb