carrierwave-tt 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ .DS_Store
2
+ *.gem
3
+ uploads/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format nested
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source :rubygems
2
+ gem "rake"
3
+
4
+ group :test do
5
+ gem 'rails', '3.0.0'
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
@@ -0,0 +1,102 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionmailer (3.0.0)
6
+ actionpack (= 3.0.0)
7
+ mail (~> 2.2.5)
8
+ actionpack (3.0.0)
9
+ activemodel (= 3.0.0)
10
+ activesupport (= 3.0.0)
11
+ builder (~> 2.1.2)
12
+ erubis (~> 2.6.6)
13
+ i18n (~> 0.4.1)
14
+ rack (~> 1.2.1)
15
+ rack-mount (~> 0.6.12)
16
+ rack-test (~> 0.5.4)
17
+ tzinfo (~> 0.3.23)
18
+ activemodel (3.0.0)
19
+ activesupport (= 3.0.0)
20
+ builder (~> 2.1.2)
21
+ i18n (~> 0.4.1)
22
+ activerecord (3.0.0)
23
+ activemodel (= 3.0.0)
24
+ activesupport (= 3.0.0)
25
+ arel (~> 1.0.0)
26
+ tzinfo (~> 0.3.23)
27
+ activeresource (3.0.0)
28
+ activemodel (= 3.0.0)
29
+ activesupport (= 3.0.0)
30
+ activesupport (3.0.0)
31
+ arel (1.0.1)
32
+ activesupport (~> 3.0.0)
33
+ builder (2.1.2)
34
+ carrierwave (0.5.7)
35
+ activesupport (~> 3.0)
36
+ diff-lcs (1.1.3)
37
+ erubis (2.6.6)
38
+ abstract (>= 1.0.0)
39
+ i18n (0.4.2)
40
+ mail (2.2.19)
41
+ activesupport (>= 2.3.6)
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.3)
48
+ subexec (~> 0.1.0)
49
+ mocha (0.10.0)
50
+ metaclass (~> 0.0.1)
51
+ polyglot (0.3.3)
52
+ rack (1.2.4)
53
+ rack-mount (0.6.14)
54
+ rack (>= 1.0.0)
55
+ rack-test (0.5.7)
56
+ rack (>= 1.0)
57
+ rails (3.0.0)
58
+ actionmailer (= 3.0.0)
59
+ actionpack (= 3.0.0)
60
+ activerecord (= 3.0.0)
61
+ activeresource (= 3.0.0)
62
+ activesupport (= 3.0.0)
63
+ bundler (~> 1.0.0)
64
+ railties (= 3.0.0)
65
+ railties (3.0.0)
66
+ actionpack (= 3.0.0)
67
+ activesupport (= 3.0.0)
68
+ rake (>= 0.8.4)
69
+ thor (~> 0.14.0)
70
+ rake (0.9.2.2)
71
+ rest-client (1.6.7)
72
+ mime-types (>= 1.16)
73
+ rspec (2.6.0)
74
+ rspec-core (~> 2.6.0)
75
+ rspec-expectations (~> 2.6.0)
76
+ rspec-mocks (~> 2.6.0)
77
+ rspec-core (2.6.4)
78
+ rspec-expectations (2.6.0)
79
+ diff-lcs (~> 1.1.2)
80
+ rspec-mocks (2.6.0)
81
+ sqlite3 (1.3.4)
82
+ sqlite3-ruby (1.3.3)
83
+ sqlite3 (>= 1.3.3)
84
+ subexec (0.1.0)
85
+ thor (0.14.6)
86
+ treetop (1.4.10)
87
+ polyglot
88
+ polyglot (>= 0.3.1)
89
+ tzinfo (0.3.30)
90
+
91
+ PLATFORMS
92
+ ruby
93
+
94
+ DEPENDENCIES
95
+ carrierwave
96
+ mini_magick
97
+ mocha (= 0.10.0)
98
+ rails (= 3.0.0)
99
+ rake
100
+ rest-client
101
+ rspec (~> 2.6.0)
102
+ sqlite3-ruby
@@ -0,0 +1,83 @@
1
+ # CarrierWave for [TokyoTyrant](http://fallabs.com/tokyotyrant/)
2
+
3
+ This gem adds support for tokyotyrant to [CarrierWave](https://github.com/jnicklas/carrierwave/)
4
+
5
+ ## Installation
6
+
7
+ gem install carrierwave-tt
8
+
9
+ ## Or using Bundler, in `Gemfile`
10
+
11
+ gem 'rest-client'
12
+ gem 'carrierwave-tt'
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 = :tt
21
+ config.host = "http://localhost"
22
+ config.port = 1978
23
+ config.domain = 'localhost'
24
+ end
25
+ ```
26
+
27
+ And then in your uploader, set the storage to `:tt`:
28
+
29
+ ```ruby
30
+ class AvatarUploader < CarrierWave::Uploader::Base
31
+ storage :tt
32
+ end
33
+ ```
34
+
35
+ You can override configuration item in individual uploader like this:
36
+
37
+ ```ruby
38
+ class AvatarUploader < CarrierWave::Uploader::Base
39
+ storage :tt
40
+
41
+ end
42
+ ```
43
+
44
+ ## Configuration for use TT "Image Space"
45
+
46
+ ```ruby
47
+ # The defined image name versions to limit use
48
+ IMAGE_UPLOADER_ALLOW_IMAGE_VERSION_NAMES = %(320 640 800)
49
+ class ImageUploader < CarrierWave::Uploader::Base
50
+ def store_dir
51
+ "#{model.class.to_s.underscore}/#{mounted_as}"
52
+ end
53
+
54
+ def default_url
55
+ # You can use FTP to upload a default image
56
+ "#{Setting.upload_url}/blank.png#{version_name}"
57
+ end
58
+
59
+ # Override url method to implement with "Image Space"
60
+ def url(version_name = "")
61
+ @url ||= super({})
62
+ version_name = version_name.to_s
63
+ return @url if version_name.blank?
64
+ if not version_name.in?(IMAGE_UPLOADER_ALLOW_IMAGE_VERSION_NAMES)
65
+ # To protected version name using, when it not defined, this will be give an error message in development environment
66
+ raise "ImageUploader version_name:#{version_name} not allow."
67
+ end
68
+ [@url,version_name].join("!") # thumb split with "!"
69
+ end
70
+
71
+ def extension_white_list
72
+ %w(jpg jpeg gif png)
73
+ end
74
+
75
+ def filename
76
+ if super.present?
77
+ model.uploader_secure_token ||= SecureRandom.uuid.gsub("-","")
78
+ Rails.logger.debug("(BaseUploader.filename) #{model.uploader_secure_token}")
79
+ "#{model.uploader_secure_token}.#{file.extension.downcase}"
80
+ end
81
+ end
82
+ end
83
+ ```
@@ -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-tt"
6
+ s.version = "0.0.1"
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Aaron"]
9
+ s.email = ["yalong1976@gmail.com"]
10
+ s.homepage = "https://github.com/totothink/carrierwave-tt"
11
+ s.summary = %q{TokyoTyrant Storage support for CarrierWave}
12
+ s.description = %q{TokyoTyrant 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 "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,7 @@
1
+ require "carrierwave/storage/tt"
2
+ require "carrierwave/tt/configuration"
3
+ CarrierWave.configure do |config|
4
+ config.storage_engines.merge!({:tt => "CarrierWave::Storage::TT"})
5
+ end
6
+
7
+ CarrierWave::Uploader::Base.send(:include, CarrierWave::TT::Configuration)
@@ -0,0 +1,194 @@
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
+ class TT < Abstract
14
+
15
+ class Connection
16
+ def initialize(options={})
17
+ @tt_host = options[:tt_host]
18
+ @tt_port = options[:tt_port]
19
+ @tt_domain = options[:tt_domain]
20
+ @connection_options = options[:connection_options] || {}
21
+ @@http ||= new_rest_client
22
+ @@http = new_rest_client if @@http.url != "#{@tt_host}:#{@tt_port}"
23
+ end
24
+
25
+ def new_rest_client
26
+ RestClient::Resource.new("#{@tt_host}:#{@tt_port}")
27
+ end
28
+
29
+ def put(path, payload, headers = {})
30
+ @@http["#{escaped(path)}"].put(payload, headers)
31
+ end
32
+
33
+ def get(path, headers = {})
34
+ @@http["#{escaped(path)}"].get(headers)
35
+ end
36
+
37
+ def delete(path, headers = {})
38
+ @@http["#{escaped(path)}"].delete(headers)
39
+ end
40
+
41
+ def post(path, payload, headers = {})
42
+ @@http["#{escaped(path)}"].post(payload, headers)
43
+ end
44
+
45
+ def escaped(path)
46
+ CGI.escape(path)
47
+ end
48
+ end
49
+
50
+ class File
51
+
52
+ def initialize(uploader, base, path)
53
+ @uploader = uploader
54
+ @path = path
55
+ @base = base
56
+ end
57
+
58
+ ##
59
+ # Returns the current path/filename of the file on Cloud Files.
60
+ #
61
+ # === Returns
62
+ #
63
+ # [String] A path
64
+ #
65
+ def path
66
+ @path
67
+ end
68
+
69
+ ##
70
+ # Reads the contents of the file from Cloud Files
71
+ #
72
+ # === Returns
73
+ #
74
+ # [String] contents of the file
75
+ #
76
+ def read
77
+ object = tt_connection.get(@path)
78
+ @headers = object.headers
79
+ object.net_http_res.body
80
+ end
81
+
82
+ ##
83
+ # Remove the file from Cloud Files
84
+ #
85
+ def delete
86
+ begin
87
+ tt_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
+ ##
96
+ # Returns the url on the Cloud Files CDN. Note that the parent container must be marked as
97
+ # public for this to work.
98
+ #
99
+ # === Returns
100
+ #
101
+ # [String] file's url
102
+ #
103
+ def url
104
+ if @uploader.tt_domain
105
+ "http://" + @uploader.tt_domain + '/' + @path
106
+ else
107
+ "#{@uploader.tt_host}:#{@uploader.tt_port}/#{@path}"
108
+ end
109
+ end
110
+
111
+ def content_type
112
+ headers[:content_type]
113
+ end
114
+
115
+ def content_type=(new_content_type)
116
+ headers[:content_type] = new_content_type
117
+ end
118
+
119
+ ##
120
+ # Writes the supplied data into the object on Cloud Files.
121
+ #
122
+ # === Returns
123
+ #
124
+ # boolean
125
+ #
126
+ def store(data,headers={})
127
+ tt_connection.put(@path, data, {'Expect' => '', 'Mkdir' => 'true'}.merge(headers))
128
+ true
129
+ end
130
+
131
+ private
132
+
133
+ def headers
134
+ @headers ||= begin
135
+ tt_connection.get(@path).headers
136
+ rescue Excon::Errors::NotFound # Don't die, just return no headers
137
+ {}
138
+ end
139
+ end
140
+
141
+ def connection
142
+ @base.connection
143
+ end
144
+
145
+ def tt_connection
146
+ if @tt_connection
147
+ @tt_connection
148
+ else
149
+ config = {:tt_host => @uploader.tt_host,
150
+ :tt_port => @uploader.tt_port,
151
+ :tt_domain => @uploader.tt_domain
152
+ }
153
+ @tt_connection ||= CarrierWave::Storage::TT::Connection.new(config)
154
+ end
155
+ end
156
+
157
+ end
158
+
159
+ ##
160
+ # Store the file on TokyoTyrant
161
+ #
162
+ # === Parameters
163
+ #
164
+ # [file (CarrierWave::SanitizedFile)] the file to store
165
+ #
166
+ # === Returns
167
+ #
168
+ # [CarrierWave::Storage::TT::File] the stored file
169
+ #
170
+ def store!(file)
171
+ cloud_files_options = {'Content-Type' => file.content_type}
172
+ f = CarrierWave::Storage::TT::File.new(uploader, self, uploader.store_path)
173
+ f.store(file.read,cloud_files_options)
174
+ f
175
+ end
176
+
177
+ # Do something to retrieve the file
178
+ #
179
+ # @param [String] identifier uniquely identifies the file
180
+ #
181
+ # [identifier (String)] uniquely identifies the file
182
+ #
183
+ # === Returns
184
+ #
185
+ # [CarrierWave::Storage::TT::File] the stored file
186
+ #
187
+ def retrieve!(identifier)
188
+ CarrierWave::Storage::TT::File.new(uploader, self, uploader.store_path(identifier))
189
+ end
190
+
191
+
192
+ end # CloudFiles
193
+ end # Storage
194
+ end # CarrierWave
@@ -0,0 +1,35 @@
1
+ module CarrierWave
2
+ module TT
3
+ module Configuration
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ add_config :tt_host
7
+ add_config :tt_port
8
+ add_config :tt_domain
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
Binary file
Binary file
@@ -0,0 +1,34 @@
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-tt"
14
+
15
+ module Rails
16
+ class <<self
17
+ def root
18
+ [File.expand_path(__FILE__).split('/')[0..-3].join('/'),"spec"].join("/")
19
+ end
20
+ end
21
+ end
22
+
23
+ ActiveRecord::Migration.verbose = false
24
+
25
+ # 测试的时候需要修改这个地方
26
+ CarrierWave.configure do |config|
27
+ config.storage = :tt
28
+ config.host = "http://localhost"
29
+ config.port = 1978
30
+ end
31
+
32
+ def load_file(fname)
33
+ File.open([Rails.root,fname].join("/"))
34
+ end
@@ -0,0 +1,58 @@
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
+ context "Upload Image" do
47
+ it "does upload image" do
48
+ f = load_file("foo.jpg")
49
+ puts Benchmark.measure {
50
+ @photo = Photo.create(:image => f)
51
+ }
52
+ @photo.errors.count.should == 0
53
+ open(@photo.image.url).should_not == nil
54
+ open(@photo.image.url).size.should == f.size
55
+ open(@photo.image.small.url).should_not == nil
56
+ end
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave-tt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Aaron
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-04 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.5.7
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.5.7
30
+ - !ruby/object:Gem::Dependency
31
+ name: rest-client
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.6.7
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.6.7
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.6'
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.6'
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: TokyoTyrant Storage support for CarrierWave
79
+ email:
80
+ - yalong1976@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rspec
87
+ - Gemfile
88
+ - Gemfile.lock
89
+ - README.md
90
+ - carrierwave-tt.gemspec
91
+ - lib/carrierwave-tt.rb
92
+ - lib/carrierwave/storage/tt.rb
93
+ - lib/carrierwave/tt/configuration.rb
94
+ - spec/foo.gif
95
+ - spec/foo.jpg
96
+ - spec/spec_helper.rb
97
+ - spec/upload_spec.rb
98
+ homepage: https://github.com/totothink/carrierwave-tt
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.25
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: TokyoTyrant Storage support for CarrierWave
122
+ test_files:
123
+ - spec/foo.gif
124
+ - spec/foo.jpg
125
+ - spec/spec_helper.rb
126
+ - spec/upload_spec.rb