shrine-gridfs 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e0c54bce081cf7e9ddfb91319ea7c7f2b87dce4d
4
- data.tar.gz: 218dee27bac26e562c22ceaf8e1694dc221a90c2
3
+ metadata.gz: 6e8fabfa3388d58c758b30e733442cc7391abe20
4
+ data.tar.gz: 52f633d0fbf668df2275c4b238af1954ecf77c87
5
5
  SHA512:
6
- metadata.gz: a4246aab41aa653b47c554990252302e94998e530390bffe9dacce31d2d7cfc9a5520babd5c0fb5bc4947bf400311920de9c9c4e4453c1f2cbc2471401871fb4
7
- data.tar.gz: f61d44f87bbd1aa65da985d90964924a81a50d6c9e819113a645d353738c86abf851f0f217b80b066c86f81a5334d453f0c6cc682a3c4122e5c4cbbb54b6a7d4
6
+ metadata.gz: fe629ee33d419e92a360dbec5236a9d57f11b239f5e1ef54942341cc70d0692e83e930421143a653f4b1d1e7773ddfcb081c032cb920f31c996dd3035a35e1aa
7
+ data.tar.gz: ebf937d63144034f7354cc26f656a0a4f9bfadf18d36625911d1ad00e3adee8abc71c5d8f3de3a08b3f89080ffeac5ca47910388bdade63a4f25fbec433c8e00
data/README.md CHANGED
@@ -22,6 +22,8 @@ client = Mongo::Client.new("mongodb://127.0.0.1:27017/mydb")
22
22
  Shrine::Storage::Gridfs.new(client: client)
23
23
  ```
24
24
 
25
+ ### Prefix
26
+
25
27
  The default prefix (bucket name) is "fs", you can change it with the `:prefix`
26
28
  option:
27
29
 
@@ -29,6 +31,15 @@ option:
29
31
  Shrine::Storage::Gridfs.new(client: client, prefix: "foo")
30
32
  ```
31
33
 
34
+ ### Chunk size
35
+
36
+ By default the Gridfs storage will store files in chunks of 256KB, you can
37
+ change that via `:chunk_size`:
38
+
39
+ ```rb
40
+ Shrine::Storage::Gridfs.new(client: client, chunk_size: 1*1024*1024) # 1MB
41
+ ```
42
+
32
43
  ### URLs
33
44
 
34
45
  You can generate URLs through which the GridFS files will be streamed with the
@@ -56,7 +67,7 @@ $ bundle exec rake test
56
67
 
57
68
  ## Inspiration
58
69
 
59
- This gem has been inspired by [refile-gridfs].
70
+ This gem was inspired by [refile-gridfs].
60
71
 
61
72
  ## License
62
73
 
@@ -1,30 +1,50 @@
1
1
  require "shrine"
2
2
  require "mongo"
3
3
  require "down"
4
-
5
- require "stringio"
4
+ require "digest"
6
5
 
7
6
  class Shrine
8
7
  module Storage
9
8
  class Gridfs
10
- attr_reader :client, :prefix, :bucket
9
+ attr_reader :client, :prefix, :bucket, :chunk_size
10
+
11
+ BATCH_SIZE = 5 * 1024 * 1024
12
+
13
+ def initialize(client:, prefix: "fs", chunk_size: 256*1024, **options)
14
+ @client = client
15
+ @prefix = prefix
16
+ @chunk_size = chunk_size
17
+ @bucket = @client.database.fs(bucket_name: @prefix)
11
18
 
12
- def initialize(client:, prefix: "fs", **options)
13
- @client = client
14
- @prefix = prefix
15
- @bucket = @client.database.fs(bucket_name: @prefix)
16
19
  @bucket.send(:ensure_indexes!)
17
20
  end
18
21
 
19
- def upload(io, id, shrine_metadata: {}, **upload_options)
20
- filename = shrine_metadata["filename"] || id
21
- file = Mongo::Grid::File.new(io, filename: filename, metadata: shrine_metadata)
22
- result = bucket.insert_one(file)
23
- id.replace(result.to_s + File.extname(id))
22
+ def upload(io, id, shrine_metadata: {}, **)
23
+ file = create_file(id, shrine_metadata: shrine_metadata)
24
+
25
+ until io.eof?
26
+ chunk = io.read([BATCH_SIZE, chunk_size].max, buffer ||= "")
27
+ grid_chunks = Mongo::Grid::File::Chunk.split(chunk, file.info, offset ||= 0)
28
+
29
+ chunks_collection.insert_many(grid_chunks)
30
+
31
+ offset += grid_chunks.count
32
+ grid_chunks.each { |grid_chunk| grid_chunk.data.data.clear } # deallocate strings
33
+ chunk.clear # deallocate string
34
+ end
35
+
36
+ files_collection.find(_id: file.id).update_one("$set" => {md5: file.info.md5.hexdigest})
37
+ end
38
+
39
+ def move(io, id, shrine_metadata: {}, **)
40
+ file = create_file(id, shrine_metadata: shrine_metadata)
41
+
42
+ chunks_collection.find(files_id: bson_id(io.id)).update_many("$set" => {files_id: file.id})
43
+ files_collection.delete_one(_id: bson_id(io.id))
24
44
  end
25
45
 
26
- def download(id)
27
- Down.copy_to_tempfile(id, open(id))
46
+ def movable?(io, id)
47
+ io.is_a?(UploadedFile) && io.storage.is_a?(Storage::Gridfs)
28
48
  end
29
49
 
30
50
  def open(id)
@@ -38,45 +58,53 @@ class Shrine
38
58
  )
39
59
  end
40
60
 
41
- def read(id)
42
- stringio = StringIO.new
43
- bucket.download_to_stream(bson_id(id), stringio)
44
- stringio.string
45
- end
46
-
47
61
  def exists?(id)
48
62
  !!bucket.find(_id: bson_id(id)).first
49
63
  end
50
64
 
51
65
  def delete(id)
52
66
  bucket.delete(bson_id(id))
67
+ rescue Mongo::Error::FileNotFound
53
68
  end
54
69
 
55
70
  def multi_delete(ids)
56
71
  ids = ids.map { |id| bson_id(id) }
57
- bucket.files_collection.find(_id: {"$in" => ids}).delete_many
58
- bucket.chunks_collection.find(files_id: {"$in" => ids}).delete_many
72
+ files_collection.find(_id: {"$in" => ids}).delete_many
73
+ chunks_collection.find(files_id: {"$in" => ids}).delete_many
59
74
  end
60
75
 
61
- def url(id, **options)
76
+ def url(id, **)
62
77
  end
63
78
 
64
79
  def clear!
65
- bucket.files_collection.find.delete_many
66
- bucket.chunks_collection.find.delete_many
80
+ files_collection.find.delete_many
81
+ chunks_collection.find.delete_many
67
82
  end
68
83
 
69
- def method_missing(name, *args)
70
- if name == :stream
71
- warn "Shrine::Storage::Gridfs#stream is deprecated over calling #each_chunk on result of Gridfs#open."
72
- content_length = bucket.find(_id: bson_id(*args)).first["length"]
73
- bucket.open_download_stream(bson_id(*args)) do |stream|
74
- stream.each { |chunk| yield chunk, content_length }
75
- end
76
- end
84
+ private
85
+
86
+ def create_file(id, shrine_metadata: {})
87
+ file = Mongo::Grid::File.new("",
88
+ filename: shrine_metadata["filename"] || id,
89
+ content_type: shrine_metadata["mime_type"],
90
+ metadata: shrine_metadata,
91
+ chunk_size: chunk_size,
92
+ )
93
+ id.replace(file.id.to_s + File.extname(id))
94
+
95
+ bucket.insert_one(file)
96
+
97
+ file.info.document[:md5] = Digest::MD5.new
98
+ file
77
99
  end
78
100
 
79
- private
101
+ def files_collection
102
+ bucket.files_collection
103
+ end
104
+
105
+ def chunks_collection
106
+ bucket.chunks_collection
107
+ end
80
108
 
81
109
  def bson_id(id)
82
110
  BSON::ObjectId(File.basename(id, ".*"))
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "shrine-gridfs"
3
- gem.version = "0.2.1"
3
+ gem.version = "0.3.0"
4
4
 
5
5
  gem.required_ruby_version = ">= 2.1"
6
6
 
@@ -13,7 +13,7 @@ Gem::Specification.new do |gem|
13
13
  gem.files = Dir["README.md", "LICENSE.txt", "lib/**/*.rb", "shrine-gridfs.gemspec"]
14
14
  gem.require_path = "lib"
15
15
 
16
- gem.add_dependency "shrine", "~> 2.0"
16
+ gem.add_dependency "shrine", "~> 2.2"
17
17
  gem.add_dependency "mongo", ">= 2.2.2", "< 3"
18
18
  gem.add_dependency "down", ">= 2.3.3"
19
19
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shrine-gridfs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janko Marohnić
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-24 00:00:00.000000000 Z
11
+ date: 2017-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: shrine
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.0'
19
+ version: '2.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '2.0'
26
+ version: '2.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: mongo
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -136,4 +136,3 @@ signing_key:
136
136
  specification_version: 4
137
137
  summary: Provides MongoDB's GridFS storage for Shrine.
138
138
  test_files: []
139
- has_rdoc: