Class: ActiveStorageEncryption::EncryptedBlobsController

Inherits:
ActionController::Base
  • Object
show all
Includes:
ActiveStorage::SetCurrent
Defined in:
lib/active_storage_encryption/encrypted_blobs_controller.rb

Defined Under Namespace

Classes: InvalidParams

Constant Summary collapse

DEFAULT_BLOB_STREAMING_DISPOSITION =
"inline"

Instance Method Summary collapse

Instance Method Details

#create_direct_uploadObject

Creates a Blob record with a random encryption key and returns the details for PUTing it This is only necessary because in Rails there is some disagreement regarding the service_name parameter. See github.com/rails/rails/issues/38940 It does not require the service to support encryption. However, we mandate that the MD5 be provided upfront, so that it gets included into the signature



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/active_storage_encryption/encrypted_blobs_controller.rb', line 36

def create_direct_upload
  blob_params = params.require(:blob).permit(:filename, :byte_size, :checksum, :content_type, metadata: {})
  unless blob_params[:checksum]
    render(plain: "The `checksum' is required", status: :unprocessable_entity) and return
  end

  service = lookup_service(params.require(:service_name))
  blob = ActiveStorage::Blob.create_before_direct_upload!(
    **blob_params.to_h.symbolize_keys,
    service_name: service.name
  )
  render json: direct_upload_json(blob)
end

#updateObject

Accepts PUT requests for direct uploads to the EncryptedDiskService. It can actually accept uploads to any encrypted service, but for S3 and GCP the upload can be done to the cloud storage bucket directly.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_storage_encryption/encrypted_blobs_controller.rb', line 17

def update
  params = read_params_from_token_and_headers_for_put
  service = lookup_service(params[:service_name])
  key = params[:key]

  service.upload(key, request.body,
    content_type: params[:content_type],
    content_length: params[:content_length],
    checksum: params[:checksum],
    encryption_key: params[:encryption_key])
rescue InvalidParams, ActiveStorageEncryption::IncorrectEncryptionKey, ActiveSupport::MessageVerifier::InvalidSignature, ActiveStorage::IntegrityError
  head :unprocessable_entity
end