refile-gridfs 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9862d00d38ad7c7573b9804bf098d894f71d2bce
4
+ data.tar.gz: 8d4eabaf55ecb9584321290728dd82aaa29ba63d
5
+ SHA512:
6
+ metadata.gz: 63d87863155b3459ba59ebc9a4de8a894b6974043666104cda7a1acfc109b0281f1ae9057ab50f2476ab0d604fa9b990954dfd044d272053c29cec8e60acd11d
7
+ data.tar.gz: 0778c954358a2fd1da1211b5587226f52a7c2eab5454aee50938cde5f0c6f13a71060ad29cc81bf523bd45ef6a9847b9983c2aee595497f04197feee819d512a
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in refile-gridfs.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Jonas Nicklas
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.
@@ -0,0 +1,45 @@
1
+ # Refile::Gridfs
2
+
3
+ A Gridfs backend for [Refile](https://github.com/elabs/refile).
4
+
5
+ ## Why?
6
+
7
+ * You want to store all your data in one place to simplify backups and replication
8
+
9
+ ## Take into account
10
+
11
+ * Gem is developed and tested using Mongodb 3.0, Ruby 2.2 and Mongo 2.1. It might work with earlier versions.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'refile-gridfs'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install refile-gridfs
28
+
29
+ ## Usage with Rails
30
+
31
+ Generate migration for table were to store list of attachments.
32
+
33
+ $ rails g refile:gridfs:migration
34
+
35
+ Generate initializer and set Refile::Gridfs as `store` backend.
36
+
37
+ $ rails g refile:gridfs:initializer
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it ( https://github.com/Titinux/refile-gridfs/fork )
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create a new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task :default => :spec
7
+ rescue LoadError
8
+ # no rspec available
9
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generates initializer and sets Gridfs as store backend
3
+
4
+ Example:
5
+ rails generate initializer
6
+
7
+ This will create:
8
+ config/initializers/refile.rb
@@ -0,0 +1,6 @@
1
+ class Refile::Gridfs::InitializerGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('../templates', __FILE__)
3
+ def copy_initializer_file
4
+ copy_file "refile.rb", "config/initializers/refile.rb"
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ require "refile"
2
+
3
+ Refile.configure do |config|
4
+ config.store = Refile::Gridfs::Backend.new(proc { Mongo::Client.new('mongodb://127.0.0.1:27017/my_db') } )
5
+ end
@@ -0,0 +1,8 @@
1
+ require "refile"
2
+ require "refile/gridfs/version"
3
+ require "refile/gridfs/backend"
4
+
5
+ module Refile
6
+ module Gridfs
7
+ end
8
+ end
@@ -0,0 +1,86 @@
1
+ require "mongo"
2
+ require "refile/backend_macros"
3
+
4
+ module Refile
5
+ module Gridfs
6
+ class Backend
7
+ extend Refile::BackendMacros
8
+
9
+ DEFAULT_PREFIX = "fs"
10
+ INIT_CONNECTION_ARG_ERROR_MSG = "When initializing new Refile::Gridfs::Backend first argument should be an instance of Mongo::Client or a lambda/proc that returns it. When using Mongoid it is available as Mongoid.client(:default)"
11
+
12
+ attr_reader :max_size, :prefix, :hasher
13
+
14
+ def initialize(connection_or_proc, max_size: nil, prefix: DEFAULT_PREFIX, hasher: Refile::RandomHasher.new)
15
+ @connection_or_proc = connection_or_proc
16
+ @max_size = max_size
17
+ @prefix = prefix
18
+ @hasher = hasher
19
+ end
20
+
21
+ verify_uploadable def upload(uploadable)
22
+ filename = @hasher.hash(uploadable)
23
+
24
+ file = Mongo::Grid::File.new(uploadable.read, filename: filename)
25
+ gridfs.insert_one(file)
26
+
27
+ Refile::File.new(self, filename)
28
+ end
29
+
30
+ verify_id def get(id)
31
+ Refile::File.new(self, id)
32
+ end
33
+
34
+ verify_id def delete(id)
35
+ file = gridfs.find_one(filename: id)
36
+ gridfs.delete_one(file) if file
37
+ end
38
+
39
+ verify_id def open(id)
40
+ StringIO.new(read(id))
41
+ end
42
+
43
+ verify_id def read(id)
44
+ if exists?(id)
45
+ gridfs.find_one(filename: id).data
46
+ else
47
+ nil
48
+ end
49
+ end
50
+
51
+ verify_id def size(id)
52
+ file = gridfs.find_one(filename: id)
53
+ file.data.length if file
54
+ end
55
+
56
+ verify_id def exists?(id)
57
+ !! gridfs.find_one(filename: id)
58
+ end
59
+
60
+ def clear!(confirm = nil)
61
+ raise Refile::Confirm unless confirm == :confirm
62
+
63
+ connection["#{@prefix}.files"].drop
64
+ connection["#{@prefix}.chunks"].drop
65
+ end
66
+
67
+ private
68
+
69
+ def obtain_new_connection
70
+ candidate = @connection_or_proc.is_a?(Proc) ? @connection_or_proc.call : @connection_or_proc
71
+ unless candidate.is_a?(Mongo::Client)
72
+ raise ArgumentError.new(INIT_CONNECTION_ARG_ERROR_MSG)
73
+ end
74
+ @connection = candidate
75
+ end
76
+
77
+ def connection
78
+ @connection || obtain_new_connection
79
+ end
80
+
81
+ def gridfs
82
+ connection.database.fs(fs_name: @prefix)
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,5 @@
1
+ module Refile
2
+ module Gridfs
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'refile/gridfs/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "refile-gridfs"
8
+ spec.version = Refile::Gridfs::VERSION
9
+ spec.authors = ["Jérémie Horhant"]
10
+ spec.email = ["jeremie.horhant@progralab.fr"]
11
+ spec.summary = %q{Gridfs backend for Refile}
12
+ spec.homepage = "https://github.com/Titinux/refile-gridfs"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+
20
+ spec.add_dependency "refile", "~> 0.6.1"
21
+ spec.add_dependency "mongo", "~> 2.1"
22
+
23
+ spec.add_development_dependency "webmock", "~> 1.20.4"
24
+ spec.add_development_dependency "bundler", "~> 1.7"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "pry"
28
+ spec.add_development_dependency "pry-stack_explorer"
29
+ spec.add_development_dependency "codeclimate-test-reporter"
30
+ end
@@ -0,0 +1,47 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe Refile::Gridfs::Backend do
4
+ let(:connection_or_proc) { Mongo::Client.new('mongodb://127.0.0.1:27017/refile_test') }
5
+ let(:backend) { Refile::Gridfs::Backend.new(connection_or_proc, max_size: 100) }
6
+ it_behaves_like :backend
7
+
8
+ # context "Connection tests" do
9
+ # def connection
10
+ # PG.connect(dbname: 'refile_test')
11
+ # end
12
+
13
+ # context "when using proc" do
14
+ # def connection_or_proc
15
+ # proc { connection }
16
+ # end
17
+
18
+ # it "reuses the same PG::Connection if connection is ok" do
19
+ # expect(backend.connection).to eq(backend.connection)
20
+ # end
21
+
22
+ # it "executes proc and obtains new connection if old one is closed" do
23
+ # old = backend.connection
24
+ # old.close
25
+ # expect(backend.connection).not_to eq(old)
26
+ # expect(backend.connection.finished?).to be_falsey
27
+ # end
28
+ # end
29
+
30
+ # context "when not using procs and providing PG::Connection directly" do
31
+ # def connection_or_proc
32
+ # connection
33
+ # end
34
+
35
+ # it "reuses the same PG::Connection" do
36
+ # expect(backend.connection).to eq(backend.connection)
37
+ # end
38
+
39
+ # it "continues to use old connection if the old one is closed" do
40
+ # old = backend.connection
41
+ # old.close
42
+ # expect(backend.connection).to eq(old)
43
+ # expect(backend.connection.finished?).to be_truthy
44
+ # end
45
+ # end
46
+ # end
47
+ end
@@ -0,0 +1,15 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
3
+
4
+ $LOAD_PATH.unshift(File.join(Gem::Specification.find_by_name("refile").gem_dir, "spec"))
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+
7
+ require "refile/spec_helper"
8
+ require "mongo"
9
+ require "pry"
10
+ require "refile/gridfs"
11
+
12
+ WebMock.disable!(:except => [:codeclimate_test_reporter])
13
+
14
+ RSpec.configure do |config|
15
+ end
metadata ADDED
@@ -0,0 +1,186 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: refile-gridfs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jérémie Horhant
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: refile
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.6.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: mongo
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: webmock
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.20.4
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.20.4
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry-stack_explorer
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: codeclimate-test-reporter
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description:
140
+ email:
141
+ - jeremie.horhant@progralab.fr
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - Gemfile
148
+ - LICENSE.txt
149
+ - README.md
150
+ - Rakefile
151
+ - lib/generators/refile/gridfs/initializer/USAGE
152
+ - lib/generators/refile/gridfs/initializer/initializer_generator.rb
153
+ - lib/generators/refile/gridfs/initializer/templates/refile.rb
154
+ - lib/refile/gridfs.rb
155
+ - lib/refile/gridfs/backend.rb
156
+ - lib/refile/gridfs/version.rb
157
+ - refile-gridfs.gemspec
158
+ - spec/refile/gridfs/backend_spec.rb
159
+ - spec/spec_helper.rb
160
+ homepage: https://github.com/Titinux/refile-gridfs
161
+ licenses:
162
+ - MIT
163
+ metadata: {}
164
+ post_install_message:
165
+ rdoc_options: []
166
+ require_paths:
167
+ - lib
168
+ required_ruby_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ requirements: []
179
+ rubyforge_project:
180
+ rubygems_version: 2.4.5.1
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: Gridfs backend for Refile
184
+ test_files:
185
+ - spec/refile/gridfs/backend_spec.rb
186
+ - spec/spec_helper.rb