Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add ActiveStorage::Blob#open
[David Robertson & George Claghorn]
- Loading branch information
1 parent
bfe4248
commit ee21b7c
Showing
8 changed files
with
81 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveStorage | ||
class Downloader | ||
def initialize(blob) | ||
@blob = blob | ||
end | ||
|
||
def download_blob_to_tempfile | ||
open_tempfile do |file| | ||
download_blob_to file | ||
yield file | ||
end | ||
end | ||
|
||
private | ||
attr_reader :blob | ||
|
||
def open_tempfile | ||
file = Tempfile.open([ "ActiveStorage", tempfile_extension_with_delimiter ]) | ||
|
||
begin | ||
yield file | ||
ensure | ||
file.close! | ||
end | ||
end | ||
|
||
def download_blob_to(file) | ||
file.binmode | ||
blob.download { |chunk| file.write(chunk) } | ||
file.flush | ||
file.rewind | ||
end | ||
|
||
def tempfile_extension_with_delimiter | ||
blob.filename.extension_with_delimiter | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters