Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
562f3a2d
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
562f3a2d
编写于
3月 28, 2019
作者:
G
George Claghorn
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add ActiveStorage::Service#open
上级
e3f5f1c9
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
32 addition
and
30 deletion
+32
-30
activestorage/app/models/active_storage/blob.rb
activestorage/app/models/active_storage/blob.rb
+5
-4
activestorage/lib/active_storage/analyzer.rb
activestorage/lib/active_storage/analyzer.rb
+2
-2
activestorage/lib/active_storage/downloader.rb
activestorage/lib/active_storage/downloader.rb
+14
-15
activestorage/lib/active_storage/previewer.rb
activestorage/lib/active_storage/previewer.rb
+4
-4
activestorage/lib/active_storage/service.rb
activestorage/lib/active_storage/service.rb
+4
-0
activestorage/test/models/blob_test.rb
activestorage/test/models/blob_test.rb
+3
-5
未找到文件。
activestorage/app/models/active_storage/blob.rb
浏览文件 @
562f3a2d
...
...
@@ -193,17 +193,18 @@ def download(&block)
#
# The tempfile's name is prefixed with +ActiveStorage-+ and the blob's ID. Its extension matches that of the blob.
#
# By default, the tempfile is created in <tt>Dir.tmpdir</tt>. Pass +t
e
mpdir:+ to create it in a different directory:
# By default, the tempfile is created in <tt>Dir.tmpdir</tt>. Pass +tmpdir:+ to create it in a different directory:
#
# blob.open(t
e
mpdir: "/path/to/tmp") do |file|
# blob.open(tmpdir: "/path/to/tmp") do |file|
# # ...
# end
#
# The tempfile is automatically closed and unlinked after the given block is executed.
#
# Raises ActiveStorage::IntegrityError if the downloaded data does not match the blob's checksum.
def
open
(
tempdir:
nil
,
&
block
)
ActiveStorage
::
Downloader
.
new
(
self
,
tempdir:
tempdir
).
download_blob_to_tempfile
(
&
block
)
def
open
(
tmpdir:
nil
,
&
block
)
service
.
open
key
,
checksum:
checksum
,
name:
[
"ActiveStorage-
#{
id
}
-"
,
filename
.
extension_with_delimiter
],
tmpdir:
tmpdir
,
&
block
end
...
...
activestorage/lib/active_storage/analyzer.rb
浏览文件 @
562f3a2d
...
...
@@ -24,14 +24,14 @@ def metadata
private
# Downloads the blob to a tempfile on disk. Yields the tempfile.
def
download_blob_to_tempfile
(
&
block
)
#:doc:
blob
.
open
t
empdir:
te
mpdir
,
&
block
blob
.
open
t
mpdir:
t
mpdir
,
&
block
end
def
logger
#:doc:
ActiveStorage
.
logger
end
def
t
e
mpdir
#:doc:
def
tmpdir
#:doc:
Dir
.
tmpdir
end
end
...
...
activestorage/lib/active_storage/downloader.rb
浏览文件 @
562f3a2d
...
...
@@ -2,24 +2,23 @@
module
ActiveStorage
class
Downloader
#:nodoc:
def
initialize
(
blob
,
tempdir:
nil
)
@blob
=
blob
@tempdir
=
tempdir
attr_reader
:service
def
initialize
(
service
)
@service
=
service
end
def
download_blob_to_tempfile
open_tempfile
do
|
file
|
download
_blob_to
file
verify_integrity_of
file
def
open
(
key
,
checksum
:,
name:
"ActiveStorage-"
,
tmpdir:
nil
)
open_tempfile
(
name
,
tmpdir
)
do
|
file
|
download
key
,
file
verify_integrity_of
file
,
checksum:
checksum
yield
file
end
end
private
attr_reader
:blob
,
:tempdir
def
open_tempfile
file
=
Tempfile
.
open
([
"ActiveStorage-
#{
blob
.
id
}
-"
,
blob
.
filename
.
extension_with_delimiter
],
tempdir
)
def
open_tempfile
(
name
,
tmpdir
=
nil
)
file
=
Tempfile
.
open
(
name
,
tmpdir
)
begin
yield
file
...
...
@@ -28,15 +27,15 @@ def open_tempfile
end
end
def
download
_blob_to
(
file
)
def
download
(
key
,
file
)
file
.
binmode
blob
.
download
{
|
chunk
|
file
.
write
(
chunk
)
}
service
.
download
(
key
)
{
|
chunk
|
file
.
write
(
chunk
)
}
file
.
flush
file
.
rewind
end
def
verify_integrity_of
(
file
)
unless
Digest
::
MD5
.
file
(
file
).
base64digest
==
blob
.
checksum
def
verify_integrity_of
(
file
,
checksum
:
)
unless
Digest
::
MD5
.
file
(
file
).
base64digest
==
checksum
raise
ActiveStorage
::
IntegrityError
end
end
...
...
activestorage/lib/active_storage/previewer.rb
浏览文件 @
562f3a2d
...
...
@@ -26,7 +26,7 @@ def preview
private
# Downloads the blob to a tempfile on disk. Yields the tempfile.
def
download_blob_to_tempfile
(
&
block
)
#:doc:
blob
.
open
t
empdir:
te
mpdir
,
&
block
blob
.
open
t
mpdir:
t
mpdir
,
&
block
end
# Executes a system command, capturing its binary output in a tempfile. Yields the tempfile.
...
...
@@ -42,7 +42,7 @@ def download_blob_to_tempfile(&block) #:doc:
# end
# end
#
# The output tempfile is opened in the directory returned by #t
e
mpdir.
# The output tempfile is opened in the directory returned by #tmpdir.
def
draw
(
*
argv
)
#:doc:
open_tempfile
do
|
file
|
instrument
:preview
,
key:
blob
.
key
do
...
...
@@ -54,7 +54,7 @@ def draw(*argv) #:doc:
end
def
open_tempfile
tempfile
=
Tempfile
.
open
(
"ActiveStorage-"
,
t
e
mpdir
)
tempfile
=
Tempfile
.
open
(
"ActiveStorage-"
,
tmpdir
)
begin
yield
tempfile
...
...
@@ -77,7 +77,7 @@ def logger #:doc:
ActiveStorage
.
logger
end
def
t
e
mpdir
#:doc:
def
tmpdir
#:doc:
Dir
.
tmpdir
end
end
...
...
activestorage/lib/active_storage/service.rb
浏览文件 @
562f3a2d
...
...
@@ -82,6 +82,10 @@ def download_chunk(key, range)
raise
NotImplementedError
end
def
open
(
*
args
,
&
block
)
ActiveStorage
::
Downloader
.
new
(
self
).
open
(
*
args
,
&
block
)
end
# Delete the file at the +key+.
def
delete
(
key
)
raise
NotImplementedError
...
...
activestorage/test/models/blob_test.rb
浏览文件 @
562f3a2d
...
...
@@ -104,14 +104,12 @@ class ActiveStorage::BlobTest < ActiveSupport::TestCase
end
end
test
"open in a custom tempdir"
do
tempdir
=
Dir
.
mktmpdir
create_file_blob
(
filename:
"racecar.jpg"
).
open
(
tempdir:
tempdir
)
do
|
file
|
test
"open in a custom tmpdir"
do
create_file_blob
(
filename:
"racecar.jpg"
).
open
(
tmpdir:
tmpdir
=
Dir
.
mktmpdir
)
do
|
file
|
assert
file
.
binmode?
assert_equal
0
,
file
.
pos
assert_match
(
/\.jpg\z/
,
file
.
path
)
assert
file
.
path
.
starts_with?
(
t
e
mpdir
)
assert
file
.
path
.
starts_with?
(
tmpdir
)
assert_equal
file_fixture
(
"racecar.jpg"
).
binread
,
file
.
read
,
"Expected downloaded file to match fixture file"
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录