Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for Shrine 3.0 #37

Merged
merged 4 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,44 @@ PATH
specs:
shrine-google_cloud_storage (2.0.0)
google-cloud-storage (~> 1.6)
shrine (~> 2.11)
shrine (~> 3.0)

GEM
remote: https://rubygems.org/
specs:
addressable (2.6.0)
public_suffix (>= 2.0.2, < 4.0)
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
content_disposition (1.0.0)
declarative (0.0.10)
declarative-option (0.1.0)
digest-crc (0.4.1)
domain_name (0.5.20180417)
unf (>= 0.0.5, < 1.0.0)
dotenv (2.6.0)
down (4.8.0)
down (5.0.0)
addressable (~> 2.5)
faraday (0.15.4)
faraday (0.17.0)
multipart-post (>= 1.2, < 3)
google-api-client (0.28.4)
google-api-client (0.33.0)
addressable (~> 2.5, >= 2.5.1)
googleauth (>= 0.5, < 0.10.0)
googleauth (~> 0.9)
httpclient (>= 2.8.1, < 3.0)
mime-types (~> 3.0)
mini_mime (~> 1.0)
representable (~> 3.0)
retriable (>= 2.0, < 4.0)
signet (~> 0.10)
google-cloud-core (1.3.0)
signet (~> 0.12)
google-cloud-core (1.3.2)
google-cloud-env (~> 1.0)
google-cloud-env (1.0.5)
google-cloud-env (1.2.1)
faraday (~> 0.11)
google-cloud-storage (1.17.0)
google-cloud-storage (1.21.1)
addressable (~> 2.5)
digest-crc (~> 0.4)
google-api-client (~> 0.26)
google-cloud-core (~> 1.2)
googleauth (>= 0.6.2, < 0.10.0)
googleauth (0.8.0)
mini_mime (~> 1.0)
googleauth (0.9.0)
faraday (~> 0.12)
jwt (>= 1.4, < 3.0)
memoist (~> 0.16)
Expand All @@ -55,26 +57,24 @@ GEM
http-form_data (2.1.1)
http_parser.rb (0.6.0)
httpclient (2.8.3)
jwt (2.1.0)
jwt (2.2.1)
memoist (0.16.0)
mime-types (3.2.2)
mime-types-data (~> 3.2015)
mime-types-data (3.2018.0812)
mini_mime (1.0.2)
minitest (5.11.3)
multi_json (1.13.1)
multipart-post (2.0.0)
os (1.0.0)
public_suffix (3.0.3)
multi_json (1.14.1)
multipart-post (2.1.1)
os (1.0.1)
public_suffix (4.0.1)
rake (12.3.2)
representable (3.0.4)
declarative (< 0.1.0)
declarative-option (< 0.2.0)
uber (< 0.2.0)
retriable (3.1.2)
shrine (2.15.0)
shrine (3.0.0)
content_disposition (~> 1.0)
down (~> 4.1)
signet (0.11.0)
down (~> 5.0)
signet (0.12.0)
addressable (~> 2.3)
faraday (~> 0.9)
jwt (>= 1.5, < 3.0)
Expand Down
20 changes: 8 additions & 12 deletions lib/shrine/storage/google_cloud_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,32 +63,28 @@ def url(id, **options)
end
end

# Downloads the file from GCS, and returns a `Tempfile`.
def download(id)
tempfile = Tempfile.new(["googlestorage", File.extname(id)], binmode: true)
get_file(id).download tempfile.path
tempfile.tap(&:open)
end

# Opens the remote file and returns it as `Down::ChunkedIO` object.
# @return [Down::ChunkedIO] object
# @see https://github.com/janko-m/down#downchunkedio
def open(id)
def open(id, rewindable: true, **options)
file = get_file(id)

# create enumerator which lazily yields chunks of downloaded content
chunks = Enumerator.new do |yielder|
# trick to get google client to stream the download
proc_io = ProcIO.new { |data| yielder << data }
file.download(proc_io, verify: :none)
file.download(proc_io, verify: :none, **options)
end

# wrap chunks in an IO-like object which downloads when needed
Down::ChunkedIO.new(
chunks: chunks,
size: file.size,
data: { file: file }
chunks: chunks,
size: file.size,
rewindable: rewindable,
data: { file: file },
)
rescue # ?
raise Shrine::FileNotFound, "file #{id.inspect} not found on storage"
end

# checks if the file exists on the storage
Expand Down
2 changes: 1 addition & 1 deletion shrine-google_cloud_storage.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Gem::Specification.new do |gem|
gem.files = Dir["README.md", "LICENSE.txt", "lib/**/*.rb", "shrine-google_cloud_storage.gemspec"]
gem.require_path = "lib"

gem.add_dependency "shrine", "~> 2.11"
gem.add_dependency "shrine", "~> 3.0"
gem.add_dependency "google-cloud-storage", "~> 1.6"

gem.add_development_dependency "rake"
Expand Down
18 changes: 15 additions & 3 deletions test/gcs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,23 @@ def service_account

describe "#open" do
it "returns an IO-like object around the file content" do
gcs.upload(image, 'foo')
gcs.upload(fakeio('file'), 'foo')
io = gcs.open('foo')
assert_equal(image.size, io.size)
assert_equal(image.read, io.read)
assert_equal(4, io.size)
assert_equal('file', io.read)
assert_instance_of(Google::Cloud::Storage::File, io.data[:file])
end

it "accepts :rewindable" do
gcs.upload(fakeio, 'foo')
io = gcs.open('foo', rewindable: false)
assert_raises(IOError) { io.rewind }
end

it "accepts additional download options" do
gcs.upload(fakeio('file'), 'foo')
io = gcs.open('foo', range: 1..2)
assert_equal('il', io.read)
end
end
end