Skip to content

Commit

Permalink
Merge branch 'master' of github.com:rails/activestorage
Browse files Browse the repository at this point in the history
  • Loading branch information
dhh committed Jul 24, 2017
2 parents d4f014b + 52eed68 commit 6de1c0b
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/models/active_storage/blob.rb
Expand Up @@ -115,7 +115,7 @@ def service_url(expires_in: 5.minutes, disposition: :inline)
# Returns a URL that can be used to directly upload a file for this blob on the service. This URL is intended to be
# short-lived for security and only generated on-demand by the client-side JavaScript responsible for doing the uploading.
def service_url_for_direct_upload(expires_in: 5.minutes)
service.url_for_direct_upload key, expires_in: expires_in, content_type: content_type, content_length: byte_size
service.url_for_direct_upload key, expires_in: expires_in, content_type: content_type, content_length: byte_size, checksum: checksum
end


Expand Down
2 changes: 1 addition & 1 deletion lib/active_storage/service.rb
Expand Up @@ -78,7 +78,7 @@ def url(key, expires_in:, disposition:, filename:, content_type:)
raise NotImplementedError
end

def url_for_direct_upload(key, expires_in:, content_type:, content_length:)
def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
raise NotImplementedError
end

Expand Down
6 changes: 3 additions & 3 deletions lib/active_storage/service/gcs_service.rb
Expand Up @@ -44,7 +44,7 @@ def exist?(key)

def url(key, expires_in:, disposition:, filename:, content_type:)
instrument :url, key do |payload|
generated_url = file_for(key).signed_url expires: expires_in, query: {
generated_url = file_for(key).signed_url expires: expires_in, query: {
"response-content-disposition" => "#{disposition}; filename=\"#{filename}\"",
"response-content-type" => content_type
}
Expand All @@ -55,10 +55,10 @@ def url(key, expires_in:, disposition:, filename:, content_type:)
end
end

def url_for_direct_upload(key, expires_in:, content_type:, content_length:)
def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
instrument :url, key do |payload|
generated_url = bucket.signed_url key, method: "PUT", expires: expires_in,
content_type: content_type
content_type: content_type, content_md5: checksum

payload[:url] = generated_url

Expand Down
4 changes: 2 additions & 2 deletions lib/active_storage/service/s3_service.rb
Expand Up @@ -59,10 +59,10 @@ def url(key, expires_in:, disposition:, filename:, content_type:)
end
end

def url_for_direct_upload(key, expires_in:, content_type:, content_length:)
def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
instrument :url, key do |payload|
generated_url = object_for(key).presigned_url :put, expires_in: expires_in,
content_type: content_type, content_length: content_length
content_type: content_type, content_length: content_length, content_md5: checksum

payload[:url] = generated_url

Expand Down
11 changes: 6 additions & 5 deletions test/service/gcs_service_test.rb
Expand Up @@ -9,14 +9,15 @@ class ActiveStorage::Service::GCSServiceTest < ActiveSupport::TestCase

test "direct upload" do
begin
key = SecureRandom.base58(24)
data = "Something else entirely!"
direct_upload_url = @service.url_for_direct_upload(key, expires_in: 5.minutes, content_type: "text/plain", content_length: data.size)
key = SecureRandom.base58(24)
data = "Something else entirely!"
checksum = Digest::MD5.base64digest(data)
url = @service.url_for_direct_upload(key, expires_in: 5.minutes, content_type: "text/plain", content_length: data.size, checksum: checksum)

HTTParty.put(
direct_upload_url,
url,
body: data,
headers: { "Content-Type" => "text/plain" },
headers: { "Content-Type" => "text/plain", "Content-MD5" => checksum },
debug_output: STDOUT
)

Expand Down
9 changes: 5 additions & 4 deletions test/service/s3_service_test.rb
Expand Up @@ -9,14 +9,15 @@ class ActiveStorage::Service::S3ServiceTest < ActiveSupport::TestCase

test "direct upload" do
begin
key = SecureRandom.base58(24)
data = "Something else entirely!"
url = @service.url_for_direct_upload(key, expires_in: 5.minutes, content_type: "text/plain", content_length: data.size)
key = SecureRandom.base58(24)
data = "Something else entirely!"
checksum = Digest::MD5.base64digest(data)
url = @service.url_for_direct_upload(key, expires_in: 5.minutes, content_type: "text/plain", content_length: data.size, checksum: checksum)

HTTParty.put(
url,
body: data,
headers: { "Content-Type" => "text/plain" },
headers: { "Content-Type" => "text/plain", "Content-MD5" => checksum },
debug_output: STDOUT
)

Expand Down

0 comments on commit 6de1c0b

Please sign in to comment.