carrierwave-qiniu 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ uploads/
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in carrierwave-qiniu.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rails', '3.2.8'
8
+ gem 'sqlite3', '1.3.6'
9
+ gem 'carrierwave', '0.6.2'
10
+ gem 'mini_magick', '3.4'
11
+ gem 'qiniu-rs', '2.3.3'
12
+ gem 'rspec', '~> 2.11'
13
+ gem 'mocha', '0.10.0'
14
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 huobazi
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # Carrierwave::Qiniu
2
+
3
+ storage [Carrierwave](https://github.com/jnicklas/carrierwave) attachments to http://qiniutek.com
4
+
5
+ example: https://github.com/huobazi/carrierwave-qiniu-example
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'carrierwave-qiniu'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install carrierwave-qiniu
20
+
21
+ ## Usage
22
+
23
+ You'll need to configure it in config/initializes/carrierwave.rb
24
+
25
+ ```ruby
26
+ ::CarrierWave.configure do |config|
27
+ config.storage = :qiniu
28
+ config.qiniu_access_key = "your qiniu access_key"
29
+ config.qiniu_secret_key = 'your qiniu secret_key'
30
+ config.qiniu_bucket = "carrierwave-qiniu-example"
31
+ config.qiniu_bucket_domain = "carrierwave-qiniu-example.aspxboy.com"
32
+ end
33
+ ```
34
+
35
+ for more information on `qiniu_bucket_domain`, please read http://docs.qiniutek.com/v2/sdk/ruby/#publish
36
+
37
+ And then in your uploader, set the storage to `:qiniu`:
38
+
39
+ ```ruby
40
+ class AvatarUploader < CarrierWave::Uploader::Base
41
+ storage :qiniu
42
+ end
43
+ ```
44
+
45
+ You can override configuration item in individual uploader like this:
46
+
47
+ ```ruby
48
+ class AvatarUploader < CarrierWave::Uploader::Base
49
+ storage :qiniu
50
+
51
+ self.qiniu_bucket = "avatars"
52
+ self.qiniu_bucket_domain = "avatars.files.example.com"
53
+ end
54
+ ```
55
+ You can see a example project on: https://github.com/huobazi/carrierwave-qiniu-example or see the spec test on http://xxxxxxxxxxx
56
+
57
+ ## Contributing
58
+
59
+ 1. Fork it
60
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
61
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
62
+ 4. Push to the branch (`git push origin my-new-feature`)
63
+ 5. Create new Pull Request
64
+
65
+
66
+ ## CHANGELOG
67
+
68
+ ### 0.0.1 (2012-08-17)
69
+
70
+ * it works.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/carrierwave-qiniu/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["huobazi"]
6
+ gem.email = ["huobazi@gmail.com"]
7
+ gem.description = %q{Qiniu Storage support for CarrierWave}
8
+ gem.summary = %q{Qiniu Storage support for CarrierWave}
9
+ gem.homepage = "https://github.com/huobazi/carrierwave-qiniu"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "carrierwave-qiniu"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Carrierwave::Qiniu::VERSION
17
+
18
+
19
+ gem.add_development_dependency "carrierwave"
20
+ gem.add_development_dependency "qiniu-rs",["~> 2.3.3"]
21
+ gem.add_development_dependency "rspec", ["~> 2.11"]
22
+ gem.add_development_dependency "rake", ["~> 0.9"]
23
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+
3
+ module CarrierWave
4
+ module Qiniu
5
+ module Configuration
6
+ extend ActiveSupport::Concern
7
+ included do
8
+ add_config :qiniu_bucket_domain
9
+ add_config :qiniu_bucket
10
+ add_config :qiniu_access_key
11
+ add_config :qiniu_secret_key
12
+ end
13
+ end
14
+
15
+ module ClassMethods
16
+ def add_config(name)
17
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
18
+ def self.#{name}(value=nil)
19
+ @#{name} = value if value
20
+ return @#{name} if self.object_id == #{self.object_id} || defined?(@#{name})
21
+ name = superclass.#{name}
22
+ return nil if name.nil? && !instance_variable_defined?("@#{name}")
23
+ @#{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
24
+ end
25
+
26
+ def self.#{name}=(value)
27
+ @#{name} = value
28
+ end
29
+
30
+ def #{name}
31
+ value = self.class.#{name}
32
+ value.instance_of?(Proc) ? value.call : value
33
+ end
34
+ RUBY
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,141 @@
1
+ # encoding: utf-8
2
+
3
+ begin
4
+ require 'carrierwave'
5
+ rescue LoadError
6
+ raise "You dot't have the 'carrierwave' gem installed"
7
+ end
8
+ begin
9
+ require 'qiniu-rs'
10
+ rescue LoadError
11
+ raise "You dot't have the 'qiniu-rs' gem installed"
12
+ end
13
+
14
+ # Qiniu::RS.establish_connection! :access_key => 'fR8TmveK4Eon_9te76mGyLKyC7I0JFwqBr6tVFdp',
15
+ # :secret_key => 'OhEs7BQAGMxoktRHvv1aNrxvosoM4C44cF6pmaMp'
16
+
17
+ module CarrierWave
18
+ module Storage
19
+ class Qiniu < Abstract
20
+
21
+ class Connection
22
+ def initialize(options={})
23
+ @qiniu_bucket_domain = options[:qiniu_bucket_domain]
24
+ @qiniu_bucket = options[:qiniu_bucket]
25
+ @qiniu_access_key = options[:qiniu_access_key]
26
+ @qiniu_secret_key = options[:qiniu_secret_key]
27
+ end
28
+
29
+ def store(file, key)
30
+ init
31
+ remote_upload_url = ::Qiniu::RS.put_auth
32
+ opts = {
33
+ :url => remote_upload_url,
34
+ :file => file.path,
35
+ :key => key,
36
+ :bucket => @qiniu_bucket,
37
+ :mime_type => file.content_type,
38
+ :enable_crc32_check => true
39
+ }
40
+
41
+ ::Qiniu::RS.upload opts
42
+
43
+ end
44
+
45
+ def delete(key)
46
+ init
47
+ begin
48
+ Qiniu::RS.delete(@qiniu_bucket, key)
49
+ true
50
+ rescue Exception => e
51
+ nil
52
+ end
53
+ end
54
+
55
+ def get_public_url(key)
56
+ init
57
+ if @qiniu_bucket_domain and @qiniu_bucket_domain.size > 0
58
+ "http://#{@qiniu_bucket_domain}/#{key}"
59
+ else
60
+ res = ::Qiniu::RS.get(@qiniu_bucket, key)
61
+ if res
62
+ res["url"]
63
+ else
64
+ nil
65
+ end
66
+ end
67
+ end
68
+
69
+ private
70
+ def init
71
+ init_qiniu_rs_connection
72
+ setup_publish_bucket_and_domain
73
+ end
74
+
75
+ def init_qiniu_rs_connection
76
+ return if @qiniu_rs_connection_inited
77
+ ::Qiniu::RS.establish_connection! :access_key => @qiniu_access_key,
78
+ :secret_key => @qiniu_secret_key
79
+
80
+ @qiniu_rs_connection_inited = true
81
+ end
82
+
83
+ def setup_publish_bucket_and_domain
84
+ ::Qiniu::RS.publish(@qiniu_bucket_domain, @qiniu_bucket)
85
+ end
86
+
87
+ end
88
+
89
+ class File
90
+
91
+ def initialize(uploader, path)
92
+ @uploader, @path = uploader, path
93
+ end
94
+
95
+ def path
96
+ @path
97
+ end
98
+
99
+ def url
100
+ qiniu_connection.get_public_url(@path)
101
+ end
102
+
103
+ def store(file)
104
+ qiniu_connection.store(file, @path)
105
+ end
106
+
107
+ def delete
108
+ qiniu_connection.delete(@path)
109
+ end
110
+
111
+ private
112
+
113
+ def qiniu_connection
114
+ if @qiniu_connection
115
+ @qiniu_connection
116
+ else
117
+ config = {
118
+ :qiniu_access_key => @uploader.qiniu_access_key,
119
+ :qiniu_secret_key => @uploader.qiniu_secret_key,
120
+ :qiniu_bucket => @uploader.qiniu_bucket,
121
+ :qiniu_bucket_domain => @uploader.qiniu_bucket_domain
122
+ }
123
+ @qiniu_connection ||= Connection.new config
124
+ end
125
+ end
126
+
127
+ end
128
+
129
+ def store!(file)
130
+ f = ::CarrierWave::Storage::Qiniu::File.new(uploader, uploader.store_path(uploader.filename))
131
+ f.store(file)
132
+ f
133
+ end
134
+
135
+ def retrieve!(identifier)
136
+ ::CarrierWave::Storage::Qiniu::File.new(uploader, uploader.store_path(identifier))
137
+ end
138
+
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ module Carrierwave
3
+ module Qiniu
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+ require "carrierwave/storage/qiniu"
3
+ require "carrierwave/qiniu/configuration"
4
+ require "carrierwave-qiniu/version"
5
+
6
+ ::CarrierWave.configure do |config|
7
+ config.storage_engines.merge!({:qiniu => "::CarrierWave::Storage::Qiniu"})
8
+ end
9
+
10
+ ::CarrierWave::Uploader::Base.send(:include, ::CarrierWave::Qiniu::Configuration)
Binary file
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+ require "rubygems"
3
+ require "rspec"
4
+ require "rspec/autorun"
5
+ require "rails"
6
+ require "active_record"
7
+ require "carrierwave"
8
+ require "carrierwave/orm/activerecord"
9
+ require "carrierwave/processing/mini_magick"
10
+
11
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
12
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__),"..","lib"))
13
+
14
+ require "carrierwave-qiniu"
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 = :qiniu
29
+ config.qiniu_access_key = "your qiniu access_key"
30
+ config.qiniu_secret_key = 'your qiniu secret_key'
31
+ config.qiniu_bucket = "spec-test"
32
+ config.qiniu_bucket_domain = "carrierwave-qiniu-example.aspxboy.com"
33
+ end
34
+
35
+ def load_file(fname)
36
+ File.open([Rails.root,fname].join("/"))
37
+ end
@@ -0,0 +1,74 @@
1
+ # encoding: utf-8
2
+ # thanks for https://github.com/nowa/carrierwave-upyun/blob/master/spec/upload_spec.rb
3
+
4
+ require File.dirname(__FILE__) + '/spec_helper'
5
+ require "open-uri"
6
+
7
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
8
+
9
+ describe "Upload" do
10
+ def setup_db
11
+ ActiveRecord::Schema.define(:version => 1) do
12
+ create_table :photos do |t|
13
+ t.column :image, :string
14
+ end
15
+ end
16
+ end
17
+
18
+ def drop_db
19
+ ActiveRecord::Base.connection.tables.each do |table|
20
+ ActiveRecord::Base.connection.drop_table(table)
21
+ end
22
+ end
23
+
24
+ class PhotoUploader < CarrierWave::Uploader::Base
25
+ include CarrierWave::MiniMagick
26
+
27
+ version :small do
28
+ process :resize_to_fill => [100, 100]
29
+ end
30
+
31
+ def store_dir
32
+ "photos"
33
+ end
34
+
35
+ def filename
36
+ "images/#{secure_token(10)}.#{file.extension}" if original_filename.present?
37
+ end
38
+
39
+ protected
40
+ def secure_token(length=16)
41
+ var = :"@#{mounted_as}_secure_token"
42
+ model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.hex(length/2))
43
+ end
44
+ end
45
+
46
+ class Photo < ActiveRecord::Base
47
+ mount_uploader :image, PhotoUploader
48
+ end
49
+
50
+
51
+ before :all do
52
+ setup_db
53
+ end
54
+
55
+ after :all do
56
+ drop_db
57
+ end
58
+
59
+ context "Upload Image" do
60
+ it "does upload image" do
61
+ f = load_file("ruby-china.png")
62
+ photo = Photo.create(:image => f)
63
+ photo.errors.count.should == 0
64
+ open(photo.image.url).should_not == nil
65
+ open(photo.image.url).size.should == f.size
66
+ open(photo.image.small.url).should_not == nil
67
+ puts ""
68
+ puts 'The image was uploaded to:'
69
+ puts ""
70
+ puts photo.image.url
71
+ puts photo.image.small.url
72
+ end
73
+ end
74
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-qiniu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - huobazi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: carrierwave
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: qiniu-rs
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.3.3
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.3.3
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '2.11'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.11'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '0.9'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '0.9'
78
+ description: Qiniu Storage support for CarrierWave
79
+ email:
80
+ - huobazi@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE
88
+ - README.md
89
+ - Rakefile
90
+ - carrierwave-qiniu.gemspec
91
+ - lib/carrierwave-qiniu.rb
92
+ - lib/carrierwave-qiniu/version.rb
93
+ - lib/carrierwave/qiniu/configuration.rb
94
+ - lib/carrierwave/storage/qiniu.rb
95
+ - spec/ruby-china.png
96
+ - spec/spec_helper.rb
97
+ - spec/upload_spec.rb
98
+ homepage: https://github.com/huobazi/carrierwave-qiniu
99
+ licenses: []
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 1.8.24
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: Qiniu Storage support for CarrierWave
122
+ test_files:
123
+ - spec/ruby-china.png
124
+ - spec/spec_helper.rb
125
+ - spec/upload_spec.rb