Differentiate between io streams and read data

上级 8da081c3
......@@ -11,16 +11,16 @@ class ActiveFile::Blob < ActiveRecord::Base
class_attribute :site
class << self
def build_after_upload(data:, filename:, content_type: nil, metadata: nil)
def build_after_upload(io:, filename:, content_type: nil, metadata: nil)
new.tap do |blob|
blob.filename = name
blob.content_type = content_type # Marcel::MimeType.for(data, name: name, declared_type: content_type)
blob.upload data
blob.upload io
end
end
def create_after_upload!(data:, filename:, content_type: nil, metadata: nil)
build_after_upload(data: data, filename: filename, content_type: content_type, metadata: metadata).tap(&:save!)
def create_after_upload!(io:, filename:, content_type: nil, metadata: nil)
build_after_upload(io: io, filename: filename, content_type: content_type, metadata: metadata).tap(&:save!)
end
end
......@@ -38,8 +38,8 @@ def url(disposition: :inline, expires_in: 5.minutes)
end
def upload(data)
site.upload(key, data)
def upload(io)
site.upload(key, io)
self.checksum = site.checksum(key)
self.byte_size = site.byte_size(key)
......
......@@ -3,7 +3,7 @@ class ActiveFile::Site
def initialize
end
def upload(key, data)
def upload(key, io)
raise NotImplementedError
end
......
......@@ -9,9 +9,9 @@ def initialize(root:)
end
def upload(key, data)
def upload(key, io)
File.open(make_path_for(key), "wb") do |file|
while chunk = data.read(65536)
while chunk = io.read(65536)
file.write(chunk)
end
end
......
......@@ -8,8 +8,8 @@ def initialize(project:, keyfile:, bucket:)
@bucket = @client.bucket(bucket)
end
def upload(key, data)
bucket.create_file(data, key)
def upload(key, io)
bucket.create_file(io, key)
end
def download(key)
......
......@@ -5,8 +5,8 @@ def initialize(sites:)
@sites = sites
end
def upload(key, data)
perform_across_sites :upload, key, data
def upload(key, io)
perform_across_sites :upload, key, io
end
def download(key)
......
......@@ -8,8 +8,8 @@ def initialize(access_key_id:, secret_access_key:, region:, bucket:)
@bucket = @client.bucket(bucket)
end
def upload(key, data)
object_for(key).put(body: data)
def upload(key, io)
object_for(key).put(body: io)
end
def download(key)
......
......@@ -22,6 +22,6 @@ class ActiveFile::BlobTest < ActiveSupport::TestCase
private
def create_blob(data: "Hello world!", filename: "hello.txt", content_type: "text/plain")
ActiveFile::Blob.create_after_upload! data: StringIO.new(data), filename: filename, content_type: content_type
ActiveFile::Blob.create_after_upload! io: StringIO.new(data), filename: filename, content_type: content_type
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册