carrierwave-upyun 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # CarrierWave for TFS
2
+
3
+ This gem adds support for [UpYun Storage](http://www.upyun.com) to [CarrierWave](https://github.com/jnicklas/carrierwave/)
4
+
5
+ ## Installation
6
+
7
+ gem install carrierwave-upyun
8
+
9
+ ## Using Bundler
10
+
11
+ gem 'rest-client'
12
+ gem 'carrierwave-upyun'
13
+
14
+ ## Configuration
15
+
16
+ You'll need to configure the to use this in config/initializes/carrierwave.rb
17
+
18
+ ```ruby
19
+ require "carrierwave/upyun"
20
+ CarrierWave.configure do |config|
21
+ config.upyun_storage_username = "xxxxxx"
22
+ config.upyun_storage_userpass = 'xxxxxx'
23
+ config.upyun_storage_bucket = "my_bucket"
24
+ config.upyun_bucket_domain = "my_bucket.files.example.com"
25
+ end
26
+ ```
27
+
28
+ And then in your uploader, set the storage to `:upyun`:
29
+
30
+ ```ruby
31
+ class AvatarUploader < CarrierWave::Uploader::Base
32
+ storage :upyun
33
+ end
34
+ ```
35
+
@@ -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-upyun"
6
+ s.version = "0.1"
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Nowa Zhu"]
9
+ s.email = ["nowazhu@gmail.com"]
10
+ s.homepage = "https://github.com/nowa/carrierwave-upyun"
11
+ s.summary = %q{UpYun Storage support for CarrierWave}
12
+ s.description = %q{UpYun 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_development_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,189 @@
1
+ # encoding: utf-8
2
+ require 'carrierwave'
3
+ begin
4
+ require 'rest_client'
5
+ RestClient.log = nil
6
+ rescue LoadError
7
+ raise "You don't have the 'rest_client' gem installed"
8
+ end
9
+
10
+ module CarrierWave
11
+ module Storage
12
+
13
+ ##
14
+ #
15
+ # CarrierWave.configure do |config|
16
+ # config.upyun_storage_username = "xxxxxx"
17
+ # config.upyun_storage_userpass = "xxxxxx"
18
+ # config.upyun_storage_bucket = "my_bucket"
19
+ # config.upyun_bucket_domain = "https://my_bucket.files.example.com"
20
+ # config.upyun_storage_api_host = "http://v0.api.upyun.com"
21
+ # end
22
+ #
23
+ #
24
+ class UpYun < Abstract
25
+
26
+ class Connection
27
+ def initialize(options={})
28
+ @upyun_storage_username = options[:upyun_storage_username]
29
+ @upyun_storage_userpass = options[:upyun_storage_userpass]
30
+ @upyun_storage_bucket = options[:upyun_storage_bucket]
31
+ @connection_options = options[:connection_options] || {}
32
+ @host = options[:api_host] || 'http://v0.api.upyun.com'
33
+ @http = RestClient::Resource.new("#{@host}/#{@upyun_storage_bucket}", @upyun_storage_username, @upyun_storage_userpass)
34
+ end
35
+
36
+ def put(path, payload, headers = {})
37
+ @http["#{escaped(path)}"].put(payload, headers)
38
+ end
39
+
40
+ def get(path, headers = {})
41
+ @http["#{escaped(path)}"].get(headers)
42
+ end
43
+
44
+ def delete(path, headers = {})
45
+ @http["#{escaped(path)}"].delete(headers)
46
+ end
47
+
48
+ def post(path, payload, headers = {})
49
+ @http["#{escaped(path)}"].post(payload, headers)
50
+ end
51
+
52
+ def escaped(path)
53
+ CGI.escape(path)
54
+ end
55
+ end
56
+
57
+ class File
58
+
59
+ def initialize(uploader, base, path)
60
+ @uploader = uploader
61
+ @path = path
62
+ @base = base
63
+ end
64
+
65
+ ##
66
+ # Returns the current path/filename of the file on Cloud Files.
67
+ #
68
+ # === Returns
69
+ #
70
+ # [String] A path
71
+ #
72
+ def path
73
+ @path
74
+ end
75
+
76
+ ##
77
+ # Reads the contents of the file from Cloud Files
78
+ #
79
+ # === Returns
80
+ #
81
+ # [String] contents of the file
82
+ #
83
+ def read
84
+ object = uy_connection.get(@path)
85
+ object.data
86
+ end
87
+
88
+ ##
89
+ # Remove the file from Cloud Files
90
+ #
91
+ def delete
92
+ begin
93
+ uy_connection.delete(@path)
94
+ true
95
+ rescue Exception => e
96
+ # If the file's not there, don't panic
97
+ nil
98
+ end
99
+ end
100
+
101
+ ##
102
+ # Returns the url on the Cloud Files CDN. Note that the parent container must be marked as
103
+ # public for this to work.
104
+ #
105
+ # === Returns
106
+ #
107
+ # [String] file's url
108
+ #
109
+ def url
110
+ if @uploader.upyun_bucket_domain
111
+ "http://" + @uploader.upyun_bucket_domain + '/' + @path
112
+ else
113
+ nil
114
+ end
115
+ end
116
+
117
+ ##
118
+ # Writes the supplied data into the object on Cloud Files.
119
+ #
120
+ # === Returns
121
+ #
122
+ # boolean
123
+ #
124
+ def store(data,headers={})
125
+ uy_connection.put(@path, data, {'Expect' => '', 'Mkdir' => 'true'}.merge(headers))
126
+ true
127
+ end
128
+
129
+ private
130
+
131
+ def headers
132
+ @headers ||= { }
133
+ end
134
+
135
+ def connection
136
+ @base.connection
137
+ end
138
+
139
+ def uy_connection
140
+ if @uy_connection
141
+ @uy_connection
142
+ else
143
+ config = {:upyun_storage_username => @uploader.upyun_storage_username,
144
+ :upyun_storage_userpass => @uploader.upyun_storage_userpass,
145
+ :upyun_storage_bucket => @uploader.upyun_storage_bucket
146
+ }
147
+ config[:api_host] = @uploader.upyun_storage_api_host if @uploader.respond_to?(:upyun_storage_api_host)
148
+ @uy_connection ||= CarrierWave::Storage::UpYun::Connection.new(config)
149
+ end
150
+ end
151
+
152
+ end
153
+
154
+ ##
155
+ # Store the file on UpYun
156
+ #
157
+ # === Parameters
158
+ #
159
+ # [file (CarrierWave::SanitizedFile)] the file to store
160
+ #
161
+ # === Returns
162
+ #
163
+ # [CarrierWave::Storage::UpYun::File] the stored file
164
+ #
165
+ def store!(file)
166
+ cloud_files_options = {'Content-Type' => file.content_type}
167
+ f = CarrierWave::Storage::UpYun::File.new(uploader, self, uploader.store_path)
168
+ f.store(file.read,cloud_files_options)
169
+ f
170
+ end
171
+
172
+ # Do something to retrieve the file
173
+ #
174
+ # @param [String] identifier uniquely identifies the file
175
+ #
176
+ # [identifier (String)] uniquely identifies the file
177
+ #
178
+ # === Returns
179
+ #
180
+ # [CarrierWave::Storage::UpYun::File] the stored file
181
+ #
182
+ def retrieve!(identifier)
183
+ CarrierWave::Storage::UpYun::File.new(uploader, self, uploader.store_path(identifier))
184
+ end
185
+
186
+
187
+ end # CloudFiles
188
+ end # Storage
189
+ end # CarrierWave
@@ -0,0 +1,7 @@
1
+ require "carrierwave/storage/upyun"
2
+ require "carrierwave/upyun/configuration"
3
+ CarrierWave.configure do |config|
4
+ config.storage_engines.merge!({:upyun => "CarrierWave::Storage::UpYun"})
5
+ end
6
+
7
+ CarrierWave::Uploader::Base.send(:include, CarrierWave::UpYun::Configuration)
@@ -0,0 +1,37 @@
1
+ module CarrierWave
2
+ module UpYun
3
+ module Configuration
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ add_config :upyun_storage_username
7
+ add_config :upyun_storage_userpass
8
+ add_config :upyun_storage_bucket
9
+ add_config :upyun_storage_api_host
10
+ add_config :upyun_bucket_domain
11
+ end
12
+ end
13
+
14
+ module ClassMethods
15
+ def add_config(name)
16
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
17
+ def self.#{name}(value=nil)
18
+ @#{name} = value if value
19
+ return @#{name} if self.object_id == #{self.object_id} || defined?(@#{name})
20
+ name = superclass.#{name}
21
+ return nil if name.nil? && !instance_variable_defined?("@#{name}")
22
+ @#{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
23
+ end
24
+
25
+ def self.#{name}=(value)
26
+ @#{name} = value
27
+ end
28
+
29
+ def #{name}
30
+ value = self.class.#{name}
31
+ value.instance_of?(Proc) ? value.call : value
32
+ end
33
+ RUBY
34
+ end
35
+ end
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-upyun
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ version: "0.1"
9
+ platform: ruby
10
+ authors:
11
+ - Nowa Zhu
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2011-11-03 00:00:00 +08:00
17
+ default_executable:
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: carrierwave
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ segments:
27
+ - 0
28
+ - 5
29
+ - 7
30
+ version: 0.5.7
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: rest-client
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 1
42
+ - 6
43
+ - 7
44
+ version: 1.6.7
45
+ type: :development
46
+ version_requirements: *id002
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ prerelease: false
50
+ requirement: &id003 !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 2
56
+ - 6
57
+ version: "2.6"
58
+ type: :development
59
+ version_requirements: *id003
60
+ - !ruby/object:Gem::Dependency
61
+ name: rake
62
+ prerelease: false
63
+ requirement: &id004 !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ - 9
70
+ version: "0.9"
71
+ type: :development
72
+ version_requirements: *id004
73
+ description: UpYun Storage support for CarrierWave
74
+ email:
75
+ - nowazhu@gmail.com
76
+ executables: []
77
+
78
+ extensions: []
79
+
80
+ extra_rdoc_files: []
81
+
82
+ files:
83
+ - README.md
84
+ - carrierwave-upyun.gemspec
85
+ - lib/carrierwave/storage/upyun.rb
86
+ - lib/carrierwave/upyun.rb
87
+ - lib/carrierwave/upyun/configuration.rb
88
+ has_rdoc: true
89
+ homepage: https://github.com/nowa/carrierwave-upyun
90
+ licenses: []
91
+
92
+ post_install_message:
93
+ rdoc_options: []
94
+
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ segments:
109
+ - 0
110
+ version: "0"
111
+ requirements: []
112
+
113
+ rubyforge_project:
114
+ rubygems_version: 1.3.6
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: UpYun Storage support for CarrierWave
118
+ test_files: []
119
+