1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix customizing Content-Type via GCS service URLs

This commit is contained in:
George Claghorn 2017-12-07 15:14:22 -05:00
parent e8c3b58cb6
commit e8286ee272
2 changed files with 21 additions and 1 deletions

View file

@ -16,7 +16,13 @@ module ActiveStorage
def upload(key, io, checksum: nil)
instrument :upload, key: key, checksum: checksum do
begin
bucket.create_file(io, key, md5: checksum)
# The official GCS client library doesn't allow us to create a file with no Content-Type metadata.
# We need the file we create to have no Content-Type so we can control it via the response-content-type
# param in signed URLs. Workaround: let the GCS client create the file with an inferred
# Content-Type (usually "application/octet-stream") then clear it.
bucket.create_file(io, key, md5: checksum).update do |file|
file.content_type = nil
end
rescue Google::Cloud::InvalidArgumentError
raise ActiveStorage::IntegrityError
end

View file

@ -35,6 +35,20 @@ if SERVICE_CONFIGURATIONS[:gcs]
assert_match(/storage\.googleapis\.com\/.*response-content-disposition=inline.*test\.txt.*response-content-type=text%2Fplain/,
@service.url(FIXTURE_KEY, expires_in: 2.minutes, disposition: :inline, filename: ActiveStorage::Filename.new("test.txt"), content_type: "text/plain"))
end
test "signed URL response headers" do
begin
key = SecureRandom.base58(24)
data = "Something else entirely!"
@service.upload(key, StringIO.new(data), checksum: Digest::MD5.base64digest(data))
url = @service.url(key, expires_in: 2.minutes, disposition: :inline, filename: ActiveStorage::Filename.new("test.txt"), content_type: "text/plain")
response = Net::HTTP.get_response(URI(url))
assert_equal "text/plain", response.header["Content-Type"]
ensure
@service.delete key
end
end
end
else
puts "Skipping GCS Service tests because no GCS configuration was supplied"