shrine-google_cloud_storage 1.0.1 → 2.0.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
- SHA1:
3
- metadata.gz: d1076839610e614727ac6be9742ee1ec630112f7
4
- data.tar.gz: 638f6ad6360e4f65ef6db03ca5a0fbd8fa5e6c75
2
+ SHA256:
3
+ metadata.gz: 23c1f63dede9dfeb07206378d3f05febbe2daeebe13dac4eefe44acabd56f969
4
+ data.tar.gz: f96cfa7a0c31b7f563844e6538bc793733b4bfacdbe30210369d57d3bcb17997
5
5
  SHA512:
6
- metadata.gz: '01968d677153ad556ea301f30149813fee671e094ce1c2744d3fbe9ba59b8f0a85f032d7ce12ef9469c8be8809b36a96ffbb80c51b93dbb7dfe5abf7ea8ca0b2'
7
- data.tar.gz: '060239c68bea2b0bba944991ec369bd73b32c6c8c87650ba26dc9d31168092a27ace43aa01af2f7a21f445ebecae406572ed859f40d212500c8f9d0917eb8268'
6
+ metadata.gz: 5d15f754a22f4eea98aeffac564a91033fd1a40fb53cd4540043e15facaa067f62f10fe7492e5c8adc2123c19a2b0964d06d6ec86e6242565a0ecc2484e4a063
7
+ data.tar.gz: 30d8de03eaa32029cadd2a54804c0470eec79504066fdc7ce92cc1578a064147abdba29062b2c43697715f407350b6fcf51a9d0c7f4d40952dacf15ac23d9064
data/README.md CHANGED
@@ -96,5 +96,5 @@ GCS_DEBUG=true
96
96
  [MIT](http://opensource.org/licenses/MIT)
97
97
 
98
98
  [Google Cloud Storage]: https://cloud.google.com/storage/
99
- [Shrine]: https://github.com/janko-m/shrine
99
+ [Shrine]: https://github.com/shrinerb/shrine
100
100
  [Project and Credential Lookup]: http://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-storage/master/guides/authentication#projectandcredentiallookup
@@ -38,14 +38,16 @@ class Shrine
38
38
  end
39
39
  file
40
40
  else
41
- get_bucket.create_file(
42
- prepare_io(io), # file - IO object, or IO-ish object like StringIO
43
- object_name(id), # path
44
- @object_options.merge(
45
- content_type: shrine_metadata["mime_type"],
46
- acl: @default_acl
47
- ).merge(options)
48
- )
41
+ with_file(io) do |file|
42
+ get_bucket.create_file(
43
+ file,
44
+ object_name(id), # path
45
+ @object_options.merge(
46
+ content_type: shrine_metadata["mime_type"],
47
+ acl: @default_acl
48
+ ).merge(options)
49
+ )
50
+ end
49
51
  end
50
52
  end
51
53
 
@@ -99,31 +101,36 @@ class Shrine
99
101
  # deletes the file from the storage
100
102
  def delete(id)
101
103
  file = get_file(id)
102
- file.delete unless file.nil?
104
+ delete_file(file) unless file.nil?
103
105
  end
104
106
 
105
- # Otherwise deletes all objects from the storage.
106
- def clear!
107
+ # deletes all objects from the storage
108
+ # If block is givem, deletes only objects for which
109
+ # the block evaluates to true.
110
+ #
111
+ # clear!
112
+ # # or
113
+ # clear! { |file| file.updated_at < 1.week.ago }
114
+ def clear!(&block)
107
115
  prefix = "#{@prefix}/" if @prefix
108
116
  files = get_bucket.files prefix: prefix
109
- batch_delete(files.lazy.map(&:name))
117
+
110
118
  loop do
111
- break if !files.next?
112
- batch_delete(files.next.lazy.map(&:name))
119
+ files.each { |file| delete_file(file) if block.nil? || block.call(file) }
120
+ files = files.next or break
113
121
  end
114
122
  end
115
123
 
124
+ # returns request data (:method, :url, and :headers) for direct uploads
116
125
  def presign(id, **options)
126
+ url = storage.signed_url(@bucket, object_name(id), method: "PUT", **options)
127
+
117
128
  headers = {}
118
129
  headers["Content-Type"] = options[:content_type] if options[:content_type]
119
130
  headers["Content-MD5"] = options[:content_md5] if options[:content_md5]
120
131
  headers.merge!(options[:headers]) if options[:headers]
121
132
 
122
- OpenStruct.new(
123
- url: storage.signed_url(@bucket, object_name(id), method: "PUT", **options),
124
- fields: {},
125
- headers: headers
126
- )
133
+ { method: :put, url: url, headers: headers }
127
134
  end
128
135
 
129
136
  def object_name(id)
@@ -155,27 +162,20 @@ class Shrine
155
162
  # TODO: add a check for the credentials
156
163
  end
157
164
 
158
- # Google cloud storage client only accepts IO|IOString|Tempfile instances
159
- # or else it will raise a "Google::Apis::ClientError, 'Invalid upload source"
160
- #
161
- # We need to convert our file to an IO.
162
- j
163
- # see:
164
- # https://github.com/google/google-api-ruby-client/blob/1c2cf5d57fd5e606c03f2aecab88469f46b8f3b2/lib/google/apis/core/upload.rb#L77
165
- def prepare_io(io)
166
- if io.respond_to?(:to_io)
167
- io.to_io
168
- elsif io.respond_to?(:tempfile)
169
- io.tempfile
165
+ def with_file(io)
166
+ if io.respond_to?(:tempfile) # ActionDispatch::Http::UploadedFile
167
+ yield io.tempfile
170
168
  else
171
- io
169
+ Shrine.with_file(io) { |file| yield file }
172
170
  end
173
171
  end
174
172
 
175
- def batch_delete(object_names)
176
- bucket = get_bucket
177
- object_names.each do |name|
178
- bucket.file(name).delete
173
+ # deletes file with ignoring NotFoundError
174
+ def delete_file(file)
175
+ begin
176
+ file.delete
177
+ rescue Google::Cloud::NotFoundError
178
+ # we're fine if the file was already deleted
179
179
  end
180
180
  end
181
181
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "shrine-google_cloud_storage"
3
- gem.version = "1.0.1"
3
+ gem.version = "2.0.0"
4
4
 
5
5
  gem.required_ruby_version = ">= 2.1"
6
6
 
@@ -13,11 +13,11 @@ Gem::Specification.new do |gem|
13
13
  gem.files = Dir["README.md", "LICENSE.txt", "lib/**/*.rb", "shrine-google_cloud_storage.gemspec"]
14
14
  gem.require_path = "lib"
15
15
 
16
- gem.add_dependency "shrine", "~> 2.0"
16
+ gem.add_dependency "shrine", "~> 2.11"
17
17
  gem.add_dependency "google-cloud-storage", "~> 1.6"
18
18
 
19
19
  gem.add_development_dependency "rake"
20
20
  gem.add_development_dependency "minitest"
21
21
  gem.add_development_dependency "dotenv"
22
- gem.add_development_dependency "http", "~> 3.0"
22
+ gem.add_development_dependency "http", "~> 4.0"
23
23
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shrine-google_cloud_storage
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Renaud Chaput
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-13 00:00:00.000000000 Z
11
+ date: 2019-02-12 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.11'
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.11'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: google-cloud-storage
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -86,14 +86,14 @@ dependencies:
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '3.0'
89
+ version: '4.0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '3.0'
96
+ version: '4.0'
97
97
  description:
98
98
  email:
99
99
  - renchap@gmail.com
@@ -123,8 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
125
  requirements: []
126
- rubyforge_project:
127
- rubygems_version: 2.6.14
126
+ rubygems_version: 3.0.1
128
127
  signing_key:
129
128
  specification_version: 4
130
129
  summary: Provides Google Cloud Storage storage for Shrine.