Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
951b2ee5
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,发现更多精彩内容 >>
提交
951b2ee5
编写于
7月 03, 2017
作者:
D
David Heinemeier Hansson
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'master' of github.com:basecamp/activefile
上级
59d3e03b
29d65979
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
82 addition
and
6 deletion
+82
-6
Gemfile
Gemfile
+3
-1
Gemfile.lock
Gemfile.lock
+10
-0
lib/active_file/blob.rb
lib/active_file/blob.rb
+0
-5
lib/active_file/sites/s3_site.rb
lib/active_file/sites/s3_site.rb
+69
-0
未找到文件。
Gemfile
浏览文件 @
951b2ee5
...
...
@@ -4,4 +4,6 @@ gemspec
gem
'
rake
'
gem
'
byebug
'
gem
'
sqlite3
'
\ No newline at end of file
gem
'
sqlite3
'
gem
'
aws-sdk
'
\ No newline at end of file
Gemfile.lock
浏览文件 @
951b2ee5
...
...
@@ -24,11 +24,20 @@ GEM
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (8.0.0)
aws-sdk (2.10.7)
aws-sdk-resources (= 2.10.7)
aws-sdk-core (2.10.7)
aws-sigv4 (~> 1.0)
jmespath (~> 1.0)
aws-sdk-resources (2.10.7)
aws-sdk-core (= 2.10.7)
aws-sigv4 (1.0.0)
byebug (9.0.6)
concurrent-ruby (1.0.5)
globalid (0.4.0)
activesupport (>= 4.2.0)
i18n (0.8.4)
jmespath (1.3.1)
minitest (5.10.2)
rake (12.0.0)
sqlite3 (1.3.13)
...
...
@@ -41,6 +50,7 @@ PLATFORMS
DEPENDENCIES
activefile!
aws-sdk
bundler (~> 1.15)
byebug
rake
...
...
lib/active_file/blob.rb
浏览文件 @
951b2ee5
...
...
@@ -7,14 +7,9 @@ class ActiveFile::Blob < ActiveRecord::Base
has_secure_token
:key
store
:metadata
,
coder:
JSON
class_attribute
:verifier
,
default:
->
{
Rails
.
application
.
message_verifier
(
'ActiveFile'
)
}
class_attribute
:site
class
<<
self
def
find_verified
(
signed_id
)
find
(
verifier
.
verify
(
signed_id
))
end
def
build_after_upload
(
data
:,
filename
:,
content_type:
nil
,
metadata:
nil
)
new
.
tap
do
|
blob
|
blob
.
filename
=
name
...
...
lib/active_file/sites/s3_site.rb
0 → 100644
浏览文件 @
951b2ee5
require
"aws-sdk"
class
ActiveFile::Sites::S3Site
<
ActiveFile
::
Site
attr_reader
:client
,
:bucket
def
initialize
(
access_key_id
:,
secret_access_key
:,
region
:,
bucket
:)
@client
=
Aws
::
S3
::
Resource
.
new
(
access_key_id:
access_key_id
,
secret_access_key:
secret_access_key
,
region:
region
)
@bucket
=
@client
.
bucket
(
bucket
)
end
def
upload
(
key
,
data
)
object_for
(
key
).
put
(
body:
data
)
end
def
download
(
key
)
if
block_given?
stream
(
key
,
&
block
)
else
object_for
(
key
).
read
end
end
def
delete
(
key
)
object_for
(
key
).
delete
end
def
exists?
(
key
)
object_for
(
key
).
exists?
end
def
byte_size
(
key
)
object_for
(
key
).
head
[
:size
]
end
def
checksum
(
key
)
head
=
object_for
(
key
).
head
# If the etag has no dashes, it's the MD5
if
!
head
.
etag
.
include?
(
"-"
)
head
.
etag
.
gsub
(
'"'
,
''
)
# Check for md5 in metadata if it was uploaded via multipart
elsif
md5sum
=
head
.
meta
[
"md5sum"
]
md5sum
# Otherwise, we don't have a digest yet for this key
else
nil
end
end
private
def
object_for
(
key
)
bucket
.
object
(
key
)
end
# Reads the object for the given key in chunks, yielding each to the block.
def
stream
(
key
,
options
=
{},
&
block
)
object
=
object_for
(
key
)
chunk_size
=
5242880
# 5 megabytes
offset
=
0
while
offset
<
object
.
content_length
yield
object
.
read
(
options
.
merge
(
:range
=>
"bytes=
#{
offset
}
-
#{
offset
+
chunk_size
-
1
}
"
))
offset
+=
chunk_size
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录