class ActiveStorage::DirectUploadsController

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.

Public Instance Methods

create() click to toggle source
# File activestorage/app/controllers/active_storage/direct_uploads_controller.rb, line 7
def create
  blob = ActiveStorage::Blob.create_before_direct_upload!(blob_args)
  render json: direct_upload_json(blob)
end

Private Instance Methods

blob_args() click to toggle source
# File activestorage/app/controllers/active_storage/direct_uploads_controller.rb, line 13
def blob_args
  params.require(:blob).permit(:filename, :byte_size, :checksum, :content_type, :metadata).to_h.symbolize_keys
end
direct_upload_json(blob) click to toggle source
# File activestorage/app/controllers/active_storage/direct_uploads_controller.rb, line 17
def direct_upload_json(blob)
  blob.as_json(methods: :signed_id).merge(direct_upload: {
    url: blob.service_url_for_direct_upload,
    headers: blob.service_headers_for_direct_upload
  })
end