shrine-gridfs 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 49c11d81531933f8c3eaa0448f8d1216fd72fe6a
4
+ data.tar.gz: 3227bf6c0438935cb4e014e2bb976dbc09612f98
5
+ SHA512:
6
+ metadata.gz: 83d330efac6ad0b2dcba7b49ff055f741f17882da41d8698efc610ead1ff9af43bec98876faf7a8ce0fdd95db34c06fc286c9a1686ab4363b4d6e5d3db9a6db5
7
+ data.tar.gz: 47f8039bad05332b7ca9c36507a28245c6b6178a4dcb17bd73c2cd4c1d0d0a0b4b3475fbece5e91d9cac55b6801387ce007bbaf27ab12b952fe8255b7c30acdd
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Janko Marohnić
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Shrine::Gridfs
2
+
3
+ Provides MongoDB's [GridFS] storage for [Shrine].
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ gem 'shrine-gridfs'
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ You can initialize the GridFS storage with a Mongo connection:
14
+
15
+ ```rb
16
+ require "shrine/storage/gridfs"
17
+
18
+ client = Mongo::Client.new("mongodb://127.0.0.1:27017/mydb")
19
+ Shrine::Storage::Gridfs.new(client: client)
20
+ ```
21
+
22
+ The default prefix (bucket name) is "fs", you can change it with the `:prefix`
23
+ option:
24
+
25
+ ```rb
26
+ Shrine::Storage::Gridfs.new(client: client, prefix: "foo")
27
+ ```
28
+
29
+ ## Development
30
+
31
+ You can run the tests with Rake:
32
+
33
+ ```rb
34
+ $ bundle exec rake test
35
+ ```
36
+
37
+ ## License
38
+
39
+ [MIT](http://opensource.org/licenses/MIT)
40
+
41
+ [GridFS]: https://docs.mongodb.org/v3.0/core/gridfs/
42
+ [Shrine]: https://github.com/janko-m/shrine
@@ -0,0 +1,72 @@
1
+ require "mongo"
2
+ require "stringio"
3
+
4
+ class Shrine
5
+ module Storage
6
+ class Gridfs
7
+ attr_reader :client, :prefix, :bucket
8
+
9
+ def initialize(client:, prefix: "fs", **options)
10
+ @client = client
11
+ @prefix = prefix
12
+ @bucket = @client.database.fs(bucket_name: @prefix)
13
+ @bucket.send(:ensure_indexes!)
14
+ end
15
+
16
+ def upload(io, id, metadata = {})
17
+ filename = metadata["filename"] || id
18
+ file = Mongo::Grid::File.new(io.read, filename: filename, metadata: metadata)
19
+ result = bucket.insert_one(file)
20
+ id.replace(result.to_s)
21
+ io.rewind
22
+ end
23
+
24
+ def download(id)
25
+ filename = bucket.find(_id: bson_id(id)).first[:filename]
26
+ tempfile = Tempfile.new(["shrine", File.extname(filename)], binmode: true)
27
+ bucket.download_to_stream(bson_id(id), tempfile)
28
+ tempfile.open
29
+ tempfile
30
+ end
31
+
32
+ def open(id)
33
+ download(id)
34
+ end
35
+
36
+ def read(id)
37
+ stringio = StringIO.new
38
+ bucket.download_to_stream(bson_id(id), stringio)
39
+ stringio.string
40
+ end
41
+
42
+ def exists?(id)
43
+ !!bucket.find(_id: bson_id(id)).first
44
+ end
45
+
46
+ def delete(id)
47
+ bucket.delete(bson_id(id))
48
+ end
49
+
50
+ def multi_delete(ids)
51
+ ids = ids.map { |id| bson_id(id) }
52
+ bucket.files_collection.find(_id: {"$in" => ids}).delete_many
53
+ bucket.chunks_collection.find(files_id: {"$in" => ids}).delete_many
54
+ end
55
+
56
+ def url(id, **options)
57
+ end
58
+
59
+ def clear!(confirm = nil)
60
+ raise Shrine::Confirm unless confirm == :confirm
61
+ bucket.files_collection.find.delete_many
62
+ bucket.chunks_collection.find.delete_many
63
+ end
64
+
65
+ private
66
+
67
+ def bson_id(id)
68
+ BSON::ObjectId(id)
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,22 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = "shrine-gridfs"
3
+ gem.version = "0.1.0"
4
+
5
+ gem.required_ruby_version = ">= 2.1"
6
+
7
+ gem.summary = "Provides Mongo GridFS storage for Shrine."
8
+ gem.homepage = "https://github.com/janko-m/shrine-gridfs"
9
+ gem.authors = ["Janko Marohnić"]
10
+ gem.email = ["janko.marohnic@gmail.com"]
11
+ gem.license = "MIT"
12
+
13
+ gem.files = Dir["README.md", "LICENSE.txt", "lib/**/*.rb", "shrine-gridfs.gemspec"]
14
+ gem.require_path = "lib"
15
+
16
+ gem.add_dependency "mongo", ">= 2.2"
17
+
18
+ gem.add_development_dependency "rake"
19
+ gem.add_development_dependency "shrine"
20
+ gem.add_development_dependency "minitest"
21
+ gem.add_development_dependency "dotenv"
22
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shrine-gridfs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Janko Marohnić
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mongo
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '2.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: shrine
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: dotenv
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - janko.marohnic@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - LICENSE.txt
91
+ - README.md
92
+ - lib/shrine/storage/gridfs.rb
93
+ - shrine-gridfs.gemspec
94
+ homepage: https://github.com/janko-m/shrine-gridfs
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '2.1'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.4.5
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Provides Mongo GridFS storage for Shrine.
118
+ test_files: []
119
+ has_rdoc: