carrierwave-grandcloud 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,2 @@
1
+ uploads/
2
+ *.gem
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 'sndacs', :git => 'git://github.com/hui/sndacs-ruby.git'
10
+ gem 'rspec','~> 2.6.0'
11
+ gem 'mocha','0.10.0'
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,120 @@
1
+ GIT
2
+ remote: git://github.com/hui/sndacs-ruby.git
3
+ revision: a50041f7fc265ebcbf72b62f85636f951979bcc1
4
+ specs:
5
+ sndacs (0.1.0)
6
+ proxies (~> 0.2.0)
7
+
8
+ GEM
9
+ remote: http://ruby.taobao.org/
10
+ specs:
11
+ actionmailer (3.2.1)
12
+ actionpack (= 3.2.1)
13
+ mail (~> 2.4.0)
14
+ actionpack (3.2.1)
15
+ activemodel (= 3.2.1)
16
+ activesupport (= 3.2.1)
17
+ builder (~> 3.0.0)
18
+ erubis (~> 2.7.0)
19
+ journey (~> 1.0.1)
20
+ rack (~> 1.4.0)
21
+ rack-cache (~> 1.1)
22
+ rack-test (~> 0.6.1)
23
+ sprockets (~> 2.1.2)
24
+ activemodel (3.2.1)
25
+ activesupport (= 3.2.1)
26
+ builder (~> 3.0.0)
27
+ activerecord (3.2.1)
28
+ activemodel (= 3.2.1)
29
+ activesupport (= 3.2.1)
30
+ arel (~> 3.0.0)
31
+ tzinfo (~> 0.3.29)
32
+ activeresource (3.2.1)
33
+ activemodel (= 3.2.1)
34
+ activesupport (= 3.2.1)
35
+ activesupport (3.2.1)
36
+ i18n (~> 0.6)
37
+ multi_json (~> 1.0)
38
+ arel (3.0.0)
39
+ builder (3.0.0)
40
+ carrierwave (0.5.8)
41
+ activesupport (~> 3.0)
42
+ diff-lcs (1.1.3)
43
+ erubis (2.7.0)
44
+ hike (1.2.1)
45
+ i18n (0.6.0)
46
+ journey (1.0.1)
47
+ json (1.6.5)
48
+ mail (2.4.1)
49
+ i18n (>= 0.4.0)
50
+ mime-types (~> 1.16)
51
+ treetop (~> 1.4.8)
52
+ metaclass (0.0.1)
53
+ mime-types (1.17.2)
54
+ mini_magick (3.4)
55
+ subexec (~> 0.2.1)
56
+ mocha (0.10.0)
57
+ metaclass (~> 0.0.1)
58
+ multi_json (1.0.4)
59
+ polyglot (0.3.3)
60
+ proxies (0.2.1)
61
+ rack (1.4.1)
62
+ rack-cache (1.1)
63
+ rack (>= 0.4)
64
+ rack-ssl (1.3.2)
65
+ rack
66
+ rack-test (0.6.1)
67
+ rack (>= 1.0)
68
+ rails (3.2.1)
69
+ actionmailer (= 3.2.1)
70
+ actionpack (= 3.2.1)
71
+ activerecord (= 3.2.1)
72
+ activeresource (= 3.2.1)
73
+ activesupport (= 3.2.1)
74
+ bundler (~> 1.0)
75
+ railties (= 3.2.1)
76
+ railties (3.2.1)
77
+ actionpack (= 3.2.1)
78
+ activesupport (= 3.2.1)
79
+ rack-ssl (~> 1.3.2)
80
+ rake (>= 0.8.7)
81
+ rdoc (~> 3.4)
82
+ thor (~> 0.14.6)
83
+ rake (0.9.2.2)
84
+ rdoc (3.12)
85
+ json (~> 1.4)
86
+ rspec (2.6.0)
87
+ rspec-core (~> 2.6.0)
88
+ rspec-expectations (~> 2.6.0)
89
+ rspec-mocks (~> 2.6.0)
90
+ rspec-core (2.6.4)
91
+ rspec-expectations (2.6.0)
92
+ diff-lcs (~> 1.1.2)
93
+ rspec-mocks (2.6.0)
94
+ sprockets (2.1.2)
95
+ hike (~> 1.2)
96
+ rack (~> 1.0)
97
+ tilt (~> 1.1, != 1.3.0)
98
+ sqlite3 (1.3.5)
99
+ sqlite3-ruby (1.3.3)
100
+ sqlite3 (>= 1.3.3)
101
+ subexec (0.2.1)
102
+ thor (0.14.6)
103
+ tilt (1.3.3)
104
+ treetop (1.4.10)
105
+ polyglot
106
+ polyglot (>= 0.3.1)
107
+ tzinfo (0.3.31)
108
+
109
+ PLATFORMS
110
+ ruby
111
+
112
+ DEPENDENCIES
113
+ carrierwave
114
+ mini_magick
115
+ mocha (= 0.10.0)
116
+ rails
117
+ rake
118
+ rspec (~> 2.6.0)
119
+ sndacs!
120
+ sqlite3-ruby
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # CarrierWave for SNDA Cloud Storage
2
+
3
+ This gem adds support for [SNDA Cloud Storage](http://oss.grandcloud.com) to [CarrierWave](https://github.com/jnicklas/carrierwave/), code base on [carrierwave-aliyun](https://github.com/huacnlee/carrierwave-aliyun)
4
+
5
+ ## Installation
6
+
7
+ gem install carrierwave-grandcloud
8
+
9
+ ## Using Bundler
10
+
11
+ gem 'sndacs', :git => 'git://github.com/hui/sndacs-ruby.git'
12
+ gem 'carrierwave-grandcloud'
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 = :grandcloud
21
+ config.grandcloud_access_id = "xxxxxx"
22
+ config.grandcloud_access_key = 'xxxxxx'
23
+ # you need create this bucket first!
24
+ config.grandcloud_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-grandcloud"
6
+ s.version = "0.1.0"
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Hui"]
9
+ s.email = ["hui@networking.io"]
10
+ s.homepage = "https://github.com/hui/carrierwave-grandcloud"
11
+ s.summary = %q{SNDA Cloud Storage support for Carrierwave}
12
+ s.description = %q{SNDA Cloud Storage 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 "sndacs", [">= 0.1.0"]
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/grandcloud"
2
+ require "carrierwave/grandcloud/configuration"
3
+ CarrierWave.configure do |config|
4
+ config.storage_engines.merge!({:grandcloud => "CarrierWave::Storage::Grandcloud"})
5
+ end
6
+ CarrierWave::Uploader::Base.send(:include, CarrierWave::Grandcloud::Configuration)
@@ -0,0 +1,36 @@
1
+ module CarrierWave
2
+ module Grandcloud
3
+ module Configuration
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ add_config :grandcloud_access_id
7
+ add_config :grandcloud_access_key
8
+ add_config :grandcloud_bucket
9
+ add_config :grandcloud_location
10
+ end
11
+ end
12
+
13
+ module ClassMethods
14
+ def add_config(name)
15
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
16
+ def self.#{name}(value=nil)
17
+ @#{name} = value if value
18
+ return @#{name} if self.object_id == #{self.object_id} || defined?(@#{name})
19
+ name = superclass.#{name}
20
+ return nil if name.nil? && !instance_variable_defined?("@#{name}")
21
+ @#{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
22
+ end
23
+
24
+ def self.#{name}=(value)
25
+ @#{name} = value
26
+ end
27
+
28
+ def #{name}
29
+ value = self.class.#{name}
30
+ value.instance_of?(Proc) ? value.call : value
31
+ end
32
+ RUBY
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,96 @@
1
+ # encoding: utf-8
2
+ require 'carrierwave'
3
+ require 'digest/hmac'
4
+ require 'digest/md5'
5
+ require "sndacs"
6
+
7
+ module CarrierWave
8
+ module Storage
9
+ class Grandcloud < Abstract
10
+ class File
11
+ def initialize(uploader, base, path)
12
+ @uploader = uploader
13
+ @path = path
14
+ @base = base
15
+ end
16
+
17
+ ##
18
+ # Returns the current path/filename of the file on Cloud Files.
19
+ #
20
+ # === Returns
21
+ #
22
+ # [String] A path
23
+ #
24
+ def path
25
+ @path
26
+ end
27
+
28
+ ##
29
+ # Reads the contents of the file from Cloud Files
30
+ #
31
+ # === Returns
32
+ #
33
+ # [String] contents of the file
34
+ #
35
+ def read
36
+ object = bucket.objects.find(@path)
37
+ object.content
38
+ end
39
+
40
+ ##
41
+ # Remove the file from Cloud Files
42
+ #
43
+ def delete
44
+ begin
45
+ object = bucket.objects.find(@path)
46
+ object.destroy
47
+ true
48
+ rescue Exception => e
49
+ # If the file's not there, don't panic
50
+ nil
51
+ end
52
+ end
53
+
54
+ def url(expires_at = Time.now + 3000)
55
+ object = bucket.objects.find(@path)
56
+ if bucket.policy == "Allow"
57
+ object.url
58
+ else
59
+ object.temporary_url(expires_at)
60
+ end
61
+ end
62
+
63
+ def store(data)
64
+ new_object = bucket.objects.build(@path)
65
+ new_object.content = data
66
+ new_object.save
67
+ end
68
+
69
+ private
70
+
71
+ def headers
72
+ @headers ||= { }
73
+ end
74
+
75
+ def bucket
76
+ return @bucket if @bucket
77
+ service = Sndacs::Service.new(:access_key_id => @uploader.grandcloud_access_id,
78
+ :secret_access_key => @uploader.grandcloud_access_key)
79
+ @bucket = service.bucket(@uploader.grandcloud_bucket, @uploader.grandcloud_location)
80
+ @bucket
81
+ end
82
+
83
+ end
84
+
85
+ def store!(file)
86
+ f = CarrierWave::Storage::Grandcloud::File.new(uploader, self, uploader.store_path)
87
+ f.store(file.read)
88
+ f
89
+ end
90
+
91
+ def retrieve!(identifier)
92
+ CarrierWave::Storage::Grandcloud::File.new(uploader, self, uploader.store_path(identifier))
93
+ end
94
+ end
95
+ end
96
+ end
data/spec/foo.gif ADDED
Binary file
data/spec/foo.jpg ADDED
Binary file
@@ -0,0 +1,37 @@
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-grandcloud"
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 = :grandcloud
29
+ config.grandcloud_access_id = "20RGC3KJUK0NV"
30
+ config.grandcloud_access_key = 'MzgyM2EwYTMtMzcwYi00ODM2'
31
+ config.grandcloud_bucket = "carrierwave"
32
+ config.grandcloud_location = "huabei-1"
33
+ end
34
+
35
+ def load_file(fname)
36
+ File.open([Rails.root,fname].join("/"))
37
+ end
@@ -0,0 +1,77 @@
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
+ def grandcloud_bucket
25
+ "carrierwave"
26
+ end
27
+
28
+ version :small do
29
+ process :resize_to_fill => [120, 120]
30
+ end
31
+
32
+ def store_dir
33
+ "photos"
34
+ end
35
+ end
36
+
37
+ class Photo < ActiveRecord::Base
38
+ mount_uploader :image, PhotoUploader
39
+ end
40
+
41
+
42
+ before :all do
43
+ setup_db
44
+ end
45
+
46
+ after :all do
47
+ drop_db
48
+ end
49
+
50
+ describe "Upload Image" do
51
+ context "should upload image" do
52
+ before(:all) do
53
+ @file = load_file("foo.jpg")
54
+ @file1 = load_file("foo.gif")
55
+ @photo = Photo.new(:image => @file)
56
+ @photo1 = Photo.new(:image => @file1)
57
+ end
58
+
59
+ it "should upload file" do
60
+ @photo.save.should be_true
61
+ @photo1.save.should be_true
62
+ end
63
+
64
+ it "should get uploaded file" do
65
+ img = open(@photo.image.url)
66
+ img.size.should == @file.size
67
+ img1 = open(@photo1.image.url)
68
+ img1.size.should == @file1.size
69
+ end
70
+
71
+ it "sholud get small version uploaded file" do
72
+ open(@photo.image.small.url).should_not == nil
73
+ open(@photo1.image.small.url).should_not == nil
74
+ end
75
+ end
76
+ end
77
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-grandcloud
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hui
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: carrierwave
16
+ requirement: &70286843901740 !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: *70286843901740
25
+ - !ruby/object:Gem::Dependency
26
+ name: sndacs
27
+ requirement: &70286843901240 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 0.1.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70286843901240
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70286843900760 !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: *70286843900760
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: &70286846513760 !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: *70286846513760
58
+ description: SNDA Cloud Storage support for Carrierwave
59
+ email:
60
+ - hui@networking.io
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - .gitignore
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - README.md
69
+ - carrierwave-grandcloud.gemspec
70
+ - lib/carrierwave-grandcloud.rb
71
+ - lib/carrierwave/grandcloud/configuration.rb
72
+ - lib/carrierwave/storage/grandcloud.rb
73
+ - spec/foo.gif
74
+ - spec/foo.jpg
75
+ - spec/spec_helper.rb
76
+ - spec/upload_spec.rb
77
+ homepage: https://github.com/hui/carrierwave-grandcloud
78
+ licenses: []
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 1.8.15
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: SNDA Cloud Storage support for Carrierwave
101
+ test_files:
102
+ - spec/foo.gif
103
+ - spec/foo.jpg
104
+ - spec/spec_helper.rb
105
+ - spec/upload_spec.rb