Skip to content

Commit ee21b7c

Browse files
Add ActiveStorage::Blob#open
[David Robertson & George Claghorn]
1 parent bfe4248 commit ee21b7c

8 files changed

Lines changed: 81 additions & 12 deletions

File tree

‎activestorage/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Add `ActiveStorage::Blob#open`, which downloads a blob to a tempfile on disk
2+
and yields the tempfile. Deprecate `ActiveStorage::Downloading`.
3+
4+
*David Robertson, George Claghorn*
5+
16
* Pass in `identify: false` as an argument when providing a `content_type` for
27
`ActiveStorage::Attached::{One,Many}#attach` to bypass automatic content
38
type inference. For example:

‎activestorage/app/models/active_storage/blob.rb‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require "active_storage/downloader"
4+
35
# A blob is a record that contains the metadata about a file and a key for where that file resides on the service.
46
# Blobs can be created in two ways:
57
#
@@ -164,6 +166,11 @@ def download(&block)
164166
service.download key, &block
165167
end
166168

169+
# Downloads the blob to a tempfile on disk. Yields the tempfile.
170+
def open(&block)
171+
ActiveStorage::Downloader.new(self).download_blob_to_tempfile(&block)
172+
end
173+
167174

168175
# Deletes the file on the service that's associated with this blob. This should only be done if the blob is going to be
169176
# deleted as well or you will essentially have a dead reference. It's recommended to use the +#purge+ and +#purge_later+

‎activestorage/app/models/active_storage/variant.rb‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
require "active_storage/downloading"
4-
53
# Image blobs can have variants that are the result of a set of transformations applied to the original.
64
# These variants are used to create thumbnails, fixed-size avatars, or any other derivative image from the
75
# original.
@@ -53,8 +51,6 @@
5351
# * {ImageProcessing::Vips}[https://github.com/janko-m/image_processing/blob/master/doc/vips.md#methods]
5452
# * {ruby-vips reference}[http://www.rubydoc.info/gems/ruby-vips/Vips/Image]
5553
class ActiveStorage::Variant
56-
include ActiveStorage::Downloading
57-
5854
WEB_IMAGE_CONTENT_TYPES = %w( image/png image/jpeg image/jpg image/gif )
5955

6056
attr_reader :blob, :variation
@@ -98,7 +94,7 @@ def processed?
9894
end
9995

10096
def process
101-
download_blob_to_tempfile do |image|
97+
blob.open do |image|
10298
transform image do |output|
10399
upload output
104100
end

‎activestorage/lib/active_storage/analyzer.rb‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
# frozen_string_literal: true
22

3-
require "active_storage/downloading"
4-
53
module ActiveStorage
64
# This is an abstract base class for analyzers, which extract metadata from blobs. See
75
# ActiveStorage::Analyzer::ImageAnalyzer for an example of a concrete subclass.
86
class Analyzer
9-
include Downloading
10-
117
attr_reader :blob
128

139
# Implement this method in a concrete subclass. Have it return true when given a blob from which
@@ -26,6 +22,11 @@ def metadata
2622
end
2723

2824
private
25+
# Downloads the blob to a tempfile on disk. Yields the tempfile.
26+
def download_blob_to_tempfile #:doc:
27+
blob.open(&block)
28+
end
29+
2930
def logger #:doc:
3031
ActiveStorage.logger
3132
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
module ActiveStorage
4+
class Downloader
5+
def initialize(blob)
6+
@blob = blob
7+
end
8+
9+
def download_blob_to_tempfile
10+
open_tempfile do |file|
11+
download_blob_to file
12+
yield file
13+
end
14+
end
15+
16+
private
17+
attr_reader :blob
18+
19+
def open_tempfile
20+
file = Tempfile.open([ "ActiveStorage", tempfile_extension_with_delimiter ])
21+
22+
begin
23+
yield file
24+
ensure
25+
file.close!
26+
end
27+
end
28+
29+
def download_blob_to(file)
30+
file.binmode
31+
blob.download { |chunk| file.write(chunk) }
32+
file.flush
33+
file.rewind
34+
end
35+
36+
def tempfile_extension_with_delimiter
37+
blob.filename.extension_with_delimiter
38+
end
39+
end
40+
end

‎activestorage/lib/active_storage/downloading.rb‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
# frozen_string_literal: true
22

33
require "tmpdir"
4+
require "active_support/core_ext/string/filters"
45

56
module ActiveStorage
67
module Downloading
8+
def self.included(klass)
9+
ActiveSupport::Deprecation.warn <<~MESSAGE.squish, caller_locations(2)
10+
ActiveStorage::Downloading is deprecated and will be removed in Active Storage 6.1.
11+
Use ActiveStorage::Blob#open instead.
12+
MESSAGE
13+
end
14+
715
private
816
# Opens a new tempfile in #tempdir and copies blob data into it. Yields the tempfile.
917
def download_blob_to_tempfile #:doc:

‎activestorage/lib/active_storage/previewer.rb‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
# frozen_string_literal: true
22

3-
require "active_storage/downloading"
3+
require "active_support/core_ext/string/filters"
44

55
module ActiveStorage
66
# This is an abstract base class for previewers, which generate images from blobs. See
77
# ActiveStorage::Previewer::PDFPreviewer and ActiveStorage::Previewer::VideoPreviewer for examples of
88
# concrete subclasses.
99
class Previewer
10-
include Downloading
11-
1210
attr_reader :blob
1311

1412
# Implement this method in a concrete subclass. Have it return true when given a blob from which
@@ -28,6 +26,11 @@ def preview
2826
end
2927

3028
private
29+
# Downloads the blob to a tempfile on disk. Yields the tempfile.
30+
def download_blob_to_tempfile #:doc:
31+
blob.open(&block)
32+
end
33+
3134
# Executes a system command, capturing its binary output in a tempfile. Yields the tempfile.
3235
#
3336
# Use this method to shell out to a system library (e.g. mupdf or ffmpeg) for preview image

‎activestorage/test/models/blob_test.rb‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ class UserWithHasOneAttachedDependentFalse < User
8484
assert_equal "a" * 64.kilobytes, chunks.second
8585
end
8686

87+
test "open" do
88+
create_file_blob(filename: "racecar.jpg").open do |file|
89+
assert file.binmode?
90+
assert_equal 0, file.pos
91+
assert_match(/\.jpg\z/, file.path)
92+
assert_equal file_fixture("racecar.jpg").binread, file.read, "Expected downloaded file to match fixture file"
93+
end
94+
end
95+
8796
test "urls expiring in 5 minutes" do
8897
blob = create_blob
8998

0 commit comments

Comments
 (0)