carrierwave-riak 0.1.0
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/.rspec +2 -0
- data/Gemfile +9 -0
- data/README.md +61 -0
- data/carrierwave-riak.gemspec +23 -0
- data/lib/carrierwave/riak.rb +8 -0
- data/lib/carrierwave/riak/configuration.rb +35 -0
- data/lib/carrierwave/storage/riak.rb +228 -0
- data/lib/carrierwave/uploader/riak.rb +23 -0
- data/spec/spec_helper.rb +34 -0
- metadata +150 -0
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# CarrierWave for [Riak](http://wiki.basho.com/Riak.html)
|
2
|
+
|
3
|
+
This gem adds storage support for [Riak](http://wiki.basho.com/Riak.html) to [CarrierWave](https://github.com/jnicklas/carrierwave/)
|
4
|
+
|
5
|
+
This module should work for basic uploads, but hasn't been tested with all features of carrierrwave and is very new. The code was initially based
|
6
|
+
on [carrierwave-upyun](https://github.com/nowa/carrierwave-upyun) but also uses ideas taken from the built in Fog storage provider.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
gem install carrierwave-riak
|
11
|
+
|
12
|
+
## Or using Bundler, in `Gemfile`
|
13
|
+
|
14
|
+
gem 'riak-client'
|
15
|
+
gem 'carrierwave-riak', :require => "carrierwave/riak"
|
16
|
+
|
17
|
+
## Configuration
|
18
|
+
|
19
|
+
You'll need to configure the Riak client in config/initializes/carrierwave.rb
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
CarrierWave.configure do |config|
|
23
|
+
config.storage = :riak
|
24
|
+
config.riak_host = 'localhost'
|
25
|
+
config.riak_port = 8098
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
## Usage example
|
30
|
+
|
31
|
+
Note that for your uploader, your should extend the CarrierWave::Uploader::Riak class.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class DocumentUploader < CarrierWave::Uploader::Riak
|
35
|
+
|
36
|
+
# If key method is not defined, Riak generated keys will be used and returned as the identifier
|
37
|
+
|
38
|
+
def key
|
39
|
+
original_filename
|
40
|
+
end
|
41
|
+
|
42
|
+
def bucket
|
43
|
+
"my_bucket"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
### Using Riak generated keys ###
|
49
|
+
|
50
|
+
Because the orm record is saved before the storage object is, the orm record needs to be updated after
|
51
|
+
saving to storage if a Riak generated key is to be used as the identifier. The CarrierWave::Uploader::Riak
|
52
|
+
class defines an :after callback to facilitate this. This only works for ActiveRecord and is likely pretty
|
53
|
+
hacky. Maybe someone can suggest a better way to deal with this.
|
54
|
+
|
55
|
+
## TODO ###
|
56
|
+
|
57
|
+
- Write specs. Bad programmer.
|
58
|
+
|
59
|
+
## Contributing ##
|
60
|
+
|
61
|
+
If this is helpful to you, but isn't quite working please send me pull requests.
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "carrierwave-riak"
|
6
|
+
s.version = "0.1.0"
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["Kevin Motschiedler"]
|
9
|
+
s.email = ["kdmotschiedler@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/motske/carrierwave-riak"
|
11
|
+
s.summary = %q{Riak Storage support for CarrierWave}
|
12
|
+
s.description = %q{Riak 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_development_dependency "carrierwave"
|
19
|
+
s.add_development_dependency "riak-client", ["~> 1.0.0"]
|
20
|
+
s.add_development_dependency "rails", ["~> 3.0.5"]
|
21
|
+
s.add_development_dependency "rspec", ["~> 2.6"]
|
22
|
+
s.add_development_dependency "rake", ["~> 0.9"]
|
23
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
require "carrierwave/storage/riak"
|
2
|
+
require "carrierwave/riak/configuration"
|
3
|
+
CarrierWave.configure do |config|
|
4
|
+
config.storage_engines.merge!({:riak => "CarrierWave::Storage::Riak"})
|
5
|
+
end
|
6
|
+
|
7
|
+
CarrierWave::Uploader::Base.send(:include, CarrierWave::Riak::Configuration)
|
8
|
+
require "carrierwave/uploader/riak"
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module CarrierWave
|
2
|
+
module Riak
|
3
|
+
module Configuration
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
included do
|
6
|
+
add_config :riak_bucket
|
7
|
+
add_config :riak_host
|
8
|
+
add_config :riak_port
|
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,228 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'carrierwave'
|
3
|
+
require 'riak'
|
4
|
+
|
5
|
+
module CarrierWave
|
6
|
+
module Storage
|
7
|
+
|
8
|
+
##
|
9
|
+
#
|
10
|
+
# CarrierWave.configure do |config|
|
11
|
+
# config.riak_host = "http://localhost
|
12
|
+
# config.riak_port = 8098
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
#
|
16
|
+
class Riak < Abstract
|
17
|
+
|
18
|
+
class Connection
|
19
|
+
def initialize(options={})
|
20
|
+
@riak_bucket = options[:riak_bucket]
|
21
|
+
@host = options[:riak_host]
|
22
|
+
@port = options[:riak_port]
|
23
|
+
@client = ::Riak::Client.new(:host => @host, :http_port => @port)
|
24
|
+
end
|
25
|
+
|
26
|
+
def store(bucket, key, payload, headers = {})
|
27
|
+
bucket = @client.bucket(bucket)
|
28
|
+
robject = ::Riak::RObject.new(bucket, key)
|
29
|
+
robject.content_type = headers[:content_type]
|
30
|
+
robject.raw_data = payload
|
31
|
+
robject.store
|
32
|
+
end
|
33
|
+
|
34
|
+
def get(bucket, key)
|
35
|
+
bucket = @client.bucket(bucket)
|
36
|
+
bucket.get(key)
|
37
|
+
end
|
38
|
+
|
39
|
+
def delete(bucket, key)
|
40
|
+
bucket = @client.bucket(bucket)
|
41
|
+
bucket.delete(key)
|
42
|
+
end
|
43
|
+
|
44
|
+
def post(path, payload, headers = {})
|
45
|
+
@http["#{escaped(path)}"].post(payload, headers)
|
46
|
+
end
|
47
|
+
|
48
|
+
def escaped(path)
|
49
|
+
CGI.escape(path)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class File
|
54
|
+
|
55
|
+
def initialize(uploader, base, bucket, key)
|
56
|
+
@uploader = uploader
|
57
|
+
@bucket = bucket
|
58
|
+
@key = key
|
59
|
+
@base = base
|
60
|
+
end
|
61
|
+
|
62
|
+
##
|
63
|
+
# Returns the key of the riak file
|
64
|
+
#
|
65
|
+
# === Returns
|
66
|
+
#
|
67
|
+
# [String] A filename
|
68
|
+
#
|
69
|
+
def key
|
70
|
+
@key
|
71
|
+
end
|
72
|
+
|
73
|
+
##
|
74
|
+
# Lookup value for file content-type header
|
75
|
+
#
|
76
|
+
# === Returns
|
77
|
+
#
|
78
|
+
# [String] value of content-type
|
79
|
+
#
|
80
|
+
def content_type
|
81
|
+
@content_type || file.content_type
|
82
|
+
end
|
83
|
+
|
84
|
+
##
|
85
|
+
# Set non-default content-type header (default is file.content_type)
|
86
|
+
#
|
87
|
+
# === Returns
|
88
|
+
#
|
89
|
+
# [String] returns new content type value
|
90
|
+
#
|
91
|
+
def content_type=(new_content_type)
|
92
|
+
@content_type = new_content_type
|
93
|
+
end
|
94
|
+
|
95
|
+
##
|
96
|
+
# Return riak meta data
|
97
|
+
#
|
98
|
+
# === Returns
|
99
|
+
#
|
100
|
+
# [Haash] A hash of X-Riak-Meta-* headers
|
101
|
+
#
|
102
|
+
def meta
|
103
|
+
file.meta
|
104
|
+
end
|
105
|
+
|
106
|
+
##
|
107
|
+
# Return size of file body
|
108
|
+
#
|
109
|
+
# === Returns
|
110
|
+
#
|
111
|
+
# [Integer] size of file body
|
112
|
+
#
|
113
|
+
def size
|
114
|
+
file.raw_data.length
|
115
|
+
end
|
116
|
+
|
117
|
+
##
|
118
|
+
# Reads the contents of the file from Riak
|
119
|
+
#
|
120
|
+
# === Returns
|
121
|
+
#
|
122
|
+
# [String] contents of the file
|
123
|
+
#
|
124
|
+
def read
|
125
|
+
file.raw_data
|
126
|
+
end
|
127
|
+
|
128
|
+
##
|
129
|
+
# Remove the file from Riak
|
130
|
+
#
|
131
|
+
def delete
|
132
|
+
begin
|
133
|
+
riak_client.delete(@bucket, @key)
|
134
|
+
true
|
135
|
+
rescue Exception => e
|
136
|
+
# If the file's not there, don't panic
|
137
|
+
nil
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
##
|
142
|
+
# Writes the supplied data into Riak
|
143
|
+
#
|
144
|
+
# === Returns
|
145
|
+
#
|
146
|
+
# boolean
|
147
|
+
#
|
148
|
+
def store(file)
|
149
|
+
@file = riak_client.store(@bucket, @key, file.read, {:content_type => file.content_type})
|
150
|
+
@key = @file.key
|
151
|
+
@uploader.key = @key
|
152
|
+
true
|
153
|
+
end
|
154
|
+
|
155
|
+
private
|
156
|
+
|
157
|
+
def headers
|
158
|
+
@headers ||= { }
|
159
|
+
end
|
160
|
+
|
161
|
+
def connection
|
162
|
+
@base.connection
|
163
|
+
end
|
164
|
+
|
165
|
+
##
|
166
|
+
# lookup file
|
167
|
+
#
|
168
|
+
# === Returns
|
169
|
+
#
|
170
|
+
# [Riak::RObject] file data from remote service
|
171
|
+
#
|
172
|
+
def file
|
173
|
+
@file ||= riak_client.get(@bucket, @key)
|
174
|
+
end
|
175
|
+
|
176
|
+
def riak_client
|
177
|
+
if @riak_client
|
178
|
+
@riak_client
|
179
|
+
else
|
180
|
+
config = {
|
181
|
+
:riak_bucket => @uploader.riak_bucket,
|
182
|
+
:riak_host => @uploader.riak_host,
|
183
|
+
:riak_port => @uploader.riak_port
|
184
|
+
}
|
185
|
+
@riak_client ||= CarrierWave::Storage::Riak::Connection.new(config)
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
end
|
190
|
+
|
191
|
+
##
|
192
|
+
# Store the file on Riak
|
193
|
+
#
|
194
|
+
# === Parameters
|
195
|
+
#
|
196
|
+
# [file (CarrierWave::SanitizedFile)] the file to store
|
197
|
+
#
|
198
|
+
# === Returns
|
199
|
+
#
|
200
|
+
# [CarrierWave::Storage::Riak::File] the stored file
|
201
|
+
#
|
202
|
+
def store!(file)
|
203
|
+
f = CarrierWave::Storage::Riak::File.new(uploader, self, uploader.bucket, uploader.key)
|
204
|
+
f.store(file)
|
205
|
+
f
|
206
|
+
end
|
207
|
+
|
208
|
+
# Do something to retrieve the file
|
209
|
+
#
|
210
|
+
# @param [String] identifier uniquely identifies the file
|
211
|
+
#
|
212
|
+
# [identifier (String)] uniquely identifies the file
|
213
|
+
#
|
214
|
+
# === Returns
|
215
|
+
#
|
216
|
+
# [CarrierWave::Storage::Riak::File] the stored file
|
217
|
+
#
|
218
|
+
def retrieve!(key)
|
219
|
+
CarrierWave::Storage::Riak::File.new(uploader, self, uploader.bucket, key)
|
220
|
+
end
|
221
|
+
|
222
|
+
def identifier
|
223
|
+
uploader.key
|
224
|
+
end
|
225
|
+
|
226
|
+
end # CloudFiles
|
227
|
+
end # Storage
|
228
|
+
end # CarrierWave
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'carrierwave'
|
2
|
+
require 'riak'
|
3
|
+
|
4
|
+
module CarrierWave
|
5
|
+
module Uploader
|
6
|
+
class Riak < Base
|
7
|
+
|
8
|
+
attr_accessor :key
|
9
|
+
|
10
|
+
storage :riak
|
11
|
+
|
12
|
+
if defined?(Rails)
|
13
|
+
after :store, :updatemodel
|
14
|
+
|
15
|
+
def updatemodel(file)
|
16
|
+
if(model.read_attribute(:"#{self.mounted_as}").nil?)
|
17
|
+
model.update_attribute(:"#{self.mounted_as}", self.key)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -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/riak"
|
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
|
+
CarrierWave.configure do |config|
|
27
|
+
config.storage = :riak
|
28
|
+
config.riak_bucket = "rspec"
|
29
|
+
config.riak_host = "localhost"
|
30
|
+
end
|
31
|
+
|
32
|
+
def load_file(fname)
|
33
|
+
File.open([Rails.root,fname].join("/"))
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: carrierwave-riak
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Kevin Motschiedler
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-03-07 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: carrierwave
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: riak-client
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ~>
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 23
|
43
|
+
segments:
|
44
|
+
- 1
|
45
|
+
- 0
|
46
|
+
- 0
|
47
|
+
version: 1.0.0
|
48
|
+
type: :development
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: rails
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ~>
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 13
|
59
|
+
segments:
|
60
|
+
- 3
|
61
|
+
- 0
|
62
|
+
- 5
|
63
|
+
version: 3.0.5
|
64
|
+
type: :development
|
65
|
+
version_requirements: *id003
|
66
|
+
- !ruby/object:Gem::Dependency
|
67
|
+
name: rspec
|
68
|
+
prerelease: false
|
69
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ~>
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
hash: 15
|
75
|
+
segments:
|
76
|
+
- 2
|
77
|
+
- 6
|
78
|
+
version: "2.6"
|
79
|
+
type: :development
|
80
|
+
version_requirements: *id004
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: rake
|
83
|
+
prerelease: false
|
84
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
hash: 25
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
- 9
|
93
|
+
version: "0.9"
|
94
|
+
type: :development
|
95
|
+
version_requirements: *id005
|
96
|
+
description: Riak Storage support for CarrierWave
|
97
|
+
email:
|
98
|
+
- kdmotschiedler@gmail.com
|
99
|
+
executables: []
|
100
|
+
|
101
|
+
extensions: []
|
102
|
+
|
103
|
+
extra_rdoc_files: []
|
104
|
+
|
105
|
+
files:
|
106
|
+
- .rspec
|
107
|
+
- Gemfile
|
108
|
+
- README.md
|
109
|
+
- carrierwave-riak.gemspec
|
110
|
+
- lib/carrierwave/riak.rb
|
111
|
+
- lib/carrierwave/riak/configuration.rb
|
112
|
+
- lib/carrierwave/storage/riak.rb
|
113
|
+
- lib/carrierwave/uploader/riak.rb
|
114
|
+
- spec/spec_helper.rb
|
115
|
+
homepage: https://github.com/motske/carrierwave-riak
|
116
|
+
licenses: []
|
117
|
+
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
|
121
|
+
require_paths:
|
122
|
+
- lib
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
none: false
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
hash: 3
|
129
|
+
segments:
|
130
|
+
- 0
|
131
|
+
version: "0"
|
132
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
|
+
none: false
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
hash: 3
|
138
|
+
segments:
|
139
|
+
- 0
|
140
|
+
version: "0"
|
141
|
+
requirements: []
|
142
|
+
|
143
|
+
rubyforge_project:
|
144
|
+
rubygems_version: 1.8.10
|
145
|
+
signing_key:
|
146
|
+
specification_version: 3
|
147
|
+
summary: Riak Storage support for CarrierWave
|
148
|
+
test_files:
|
149
|
+
- spec/spec_helper.rb
|
150
|
+
has_rdoc:
|