-
-
Save EfeAgare/debea4b262cae0ee3f6e75e8c7836c00 to your computer and use it in GitHub Desktop.
Active Storage's direct uploads controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# 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 < ActiveStorage::BaseController | |
skip_before_action :verify_authenticity_token, :only => [:create] | |
def create | |
blob = ActiveStorage::Blob.create_before_direct_upload!(blob_args) | |
render json: direct_upload_json(blob) | |
end | |
private | |
def blob_args | |
params.require(:blob).permit(:filename, :byte_size, :checksum, :content_type, :metadata).to_h.symbolize_keys | |
end | |
def direct_upload_json(blob) | |
blob.as_json(root: false, methods: :signed_id).merge(direct_upload: { | |
url: blob.service_url_for_direct_upload, | |
headers: blob.service_headers_for_direct_upload | |
}) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment