Switch to simpler signed_id for blob rather than full GlobalID

We don't need to lookup multiple different classes, so no need to use a globalid.
上级 8f206248
# Creates a new blob on the server side in anticipation of a direct-to-service upload from the client side.
# When the client-side upload is completed, the signed_blob_id can be submitted as part of the form to reference
# the blob that was created up front.
class ActiveStorage::DirectUploadsController < ActionController::Base
def create
blob = ActiveStorage::Blob.create_before_direct_upload!(blob_args)
render json: { url: blob.url_for_direct_upload, sgid: blob.to_sgid.to_param }
render json: { upload_to_url: blob.url_for_direct_upload, signed_blob_id: blob.signed_id }
end
private
......
require "active_storage/variant"
class ActiveStorage::VariantsController < ActionController::Base
def show
if blob_key = decode_verified_blob_key
redirect_to processed_variant_for(blob_key).url(disposition: disposition_param)
if blob = find_signed_blob
redirect_to ActiveStorage::Variant.new(blob, decoded_variation).processed.url(disposition: disposition_param)
else
head :not_found
end
end
private
def decode_verified_blob_key
ActiveStorage::VerifiedKeyWithExpiration.decode(params[:encoded_blob_key])
def find_signed_blob
ActiveStorage::Blob.find_signed(params[:signed_blob_id])
end
def processed_variant_for(blob_key)
ActiveStorage::Variant.new(
ActiveStorage::Blob.find_by!(key: blob_key),
ActiveStorage::Variation.decode(params[:variation_key])
).processed
def decoded_variation
ActiveStorage::Variation.decode(params[:variation_key])
end
def disposition_param
......
......@@ -2,6 +2,7 @@
require "active_storage/filename"
require "active_storage/purge_job"
require "active_storage/variant"
require "active_storage/variation"
# Schema: id, key, filename, content_type, metadata, byte_size, checksum, created_at
class ActiveStorage::Blob < ActiveRecord::Base
......@@ -13,6 +14,10 @@ class ActiveStorage::Blob < ActiveRecord::Base
class_attribute :service
class << self
def find_signed(id)
find ActiveStorage.verifier.verify(id)
end
def build_after_upload(io:, filename:, content_type: nil, metadata: nil)
new.tap do |blob|
blob.filename = filename
......@@ -33,6 +38,10 @@ def create_before_direct_upload!(filename:, byte_size:, checksum:, content_type:
end
def signed_id
ActiveStorage.verifier.generate(id)
end
def key
# We can't wait until the record is first saved to have a key for it
self[:key] ||= self.class.generate_unique_secure_token
......
......@@ -2,6 +2,7 @@
require "pathname"
require "digest/md5"
require "active_support/core_ext/numeric/bytes"
require "active_storage/verified_key_with_expiration"
class ActiveStorage::Service::DiskService < ActiveStorage::Service
attr_reader :root
......
Rails.application.routes.draw do
get "/rails/active_storage/disk/:encoded_key/*filename" => "active_storage/disk#show", as: :rails_disk_blob
post "/rails/active_storage/direct_uploads" => "active_storage/direct_uploads#create", as: :rails_direct_uploads
get "/rails/active_storage/variants/:encoded_blob_key/:variation_key/*filename" => "active_storage/variants#show", as: :rails_blob_variation
get "/rails/active_storage/variants/:signed_blob_id/:variation_key/*filename" => "active_storage/variants#show", as: :rails_blob_variation
direct :rails_variant do |variant|
encoded_blob_key = ActiveStorage::VerifiedKeyWithExpiration.encode(variant.blob.key)
variation_key = variant.variation.key
filename = variant.blob.filename
signed_blob_id = variant.blob.signed_id
variation_key = variant.variation.key
filename = variant.blob.filename
route_for(:rails_blob_variation, encoded_blob_key, variation_key, filename)
route_for(:rails_blob_variation, signed_blob_id, variation_key, filename)
end
resolve("ActiveStorage::Variant") { |variant| route_for(:rails_variant, variant) }
get "/rails/active_storage/disk/:encoded_key/*filename" => "active_storage/disk#show", as: :rails_disk_blob
post "/rails/active_storage/direct_uploads" => "active_storage/direct_uploads#create", as: :rails_direct_uploads
end
......@@ -26,7 +26,7 @@ def create_blob_from(attachable)
when Hash
ActiveStorage::Blob.create_after_upload!(attachable)
when String
GlobalID::Locator.locate_signed(attachable)
ActiveStorage::Blob.find_signed(attachable)
else
nil
end
......
......@@ -24,8 +24,8 @@ class ActiveStorage::S3DirectUploadsControllerTest < ActionController::TestCase
details = JSON.parse(@response.body)
assert_match /#{SERVICE_CONFIGURATIONS[:s3][:bucket]}\.s3.(\S+)?amazonaws\.com/, details["url"]
assert_equal "hello.txt", GlobalID::Locator.locate_signed(details["sgid"]).filename.to_s
assert_match /#{SERVICE_CONFIGURATIONS[:s3][:bucket]}\.s3.(\S+)?amazonaws\.com/, details["upload_to_url"]
assert_equal "hello.txt", ActiveStorage::Blob.find_signed(details["signed_blob_id"]).filename.to_s
end
end
else
......@@ -54,8 +54,8 @@ class ActiveStorage::GCSDirectUploadsControllerTest < ActionController::TestCase
details = JSON.parse(@response.body)
assert_match %r{storage\.googleapis\.com/#{@config[:bucket]}}, details["url"]
assert_equal "hello.txt", GlobalID::Locator.locate_signed(details["sgid"]).filename.to_s
assert_match %r{storage\.googleapis\.com/#{@config[:bucket]}}, details["upload_to_url"]
assert_equal "hello.txt", ActiveStorage::Blob.find_signed(details["signed_blob_id"]).filename.to_s
end
end
else
......
......@@ -14,7 +14,7 @@ class ActiveStorage::VariantsControllerTest < ActionController::TestCase
test "showing variant inline" do
get :show, params: {
filename: @blob.filename,
encoded_blob_key: ActiveStorage::VerifiedKeyWithExpiration.encode(@blob.key, expires_in: 5.minutes),
signed_blob_id: @blob.signed_id,
variation_key: ActiveStorage::Variation.encode(resize: "100x100") }
assert_redirected_to /racecar.jpg\?disposition=inline/
......
......@@ -21,7 +21,7 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase
end
test "attach existing sgid blob" do
@user.avatar.attach create_blob(filename: "funky.jpg").to_sgid.to_s
@user.avatar.attach create_blob(filename: "funky.jpg").signed_id
assert_equal "funky.jpg", @user.avatar.filename.to_s
end
......
require "test_helper"
require "database/setup"
require "active_storage/verified_key_with_expiration"
class ActiveStorage::BlobTest < ActiveSupport::TestCase
test "create after upload sets byte size and checksum" do
......
require "test_helper"
require "active_support/core_ext/securerandom"
require "active_storage/verified_key_with_expiration"
class ActiveStorage::VerifiedKeyWithExpirationTest < ActiveSupport::TestCase
FIXTURE_KEY = SecureRandom.base58(24)
......
......@@ -28,11 +28,6 @@
ActiveStorage::Blob.service = ActiveStorage::Service::DiskService.new(root: Dir.mktmpdir("active_storage_tests"))
ActiveStorage::Service.logger = ActiveSupport::Logger.new(STDOUT)
require "active_storage/verified_key_with_expiration"
ActiveStorage::VerifiedKeyWithExpiration.verifier = ActiveSupport::MessageVerifier.new("Testing")
require "active_storage/variation"
ActiveStorage::Variation.verifier = ActiveSupport::MessageVerifier.new("Testing")
ActiveStorage.verifier = ActiveSupport::MessageVerifier.new("Testing")
class ActiveSupport::TestCase
......@@ -71,4 +66,3 @@ class ActionController::TestCase
require "global_id"
GlobalID.app = "ActiveStorageExampleApp"
ActiveRecord::Base.send :include, GlobalID::Identification
SignedGlobalID.verifier = ActiveStorage::VerifiedKeyWithExpiration.verifier
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册