public function S3CorsUploadAjaxController::signRequest in Flysystem - S3 8
Same name and namespace in other branches
- 2.0.x src/Controller/S3CorsUploadAjaxController.php \Drupal\flysystem_s3\Controller\S3CorsUploadAjaxController::signRequest()
Returns the signed request.
Parameters
\Symfony\Component\HttpFoundation\Request $request: The current request.
Return value
\Symfony\Component\HttpFoundation\JsonResponse A JsonResponse object.
1 string reference to 'S3CorsUploadAjaxController::signRequest'
File
- src/
Controller/ S3CorsUploadAjaxController.php, line 66
Class
- S3CorsUploadAjaxController
- Defines a controller to respond to S3 CORS upload AJAX requests.
Namespace
Drupal\flysystem_s3\ControllerCode
public function signRequest(Request $request) {
$post = $request->request
->all();
/** @var \Drupal\flysystem_s3\Flysystem\Adapter\S3Adapter $adapter */
$scheme = \Drupal::service('file_system')
->uriScheme($post['destination']);
$adapter = $this->flysystemFactory
->getPlugin($scheme)
->getAdapter();
$client = $adapter
->getClient();
$bucket = $adapter
->getBucket();
$destination = $adapter
->applyPathPrefix(StreamWrapperManager::getTarget($post['destination']));
$options = [
[
'acl' => $post['acl'],
],
[
'bucket' => $bucket,
],
[
'starts-with',
'$key',
$destination . '/',
],
];
// Retrieve the file name and build the URI.
// Destination does not contain a prefix as it is applied by the fly system.
$uri = \Drupal::service('file_system')
->createFilename($post['filename'], $post['destination']);
// Apply the prefix to the URI and use it as a key in the POST request.
$post['key'] = $adapter
->applyPathPrefix(StreamWrapperManager::getTarget($uri));
// Create a temporary file to return with a file ID in the response.
$file = File::create([
'uri' => $post['key'],
'filesize' => $post['filesize'],
'filename' => $post['filename'],
'filemime' => $post['filemime'],
'uid' => \Drupal::currentUser()
->getAccount()
->id(),
]);
$file
->save();
// Remove values not necessary for the request to Amazon.
unset($post['destination']);
unset($post['filename']);
unset($post['filemime']);
unset($post['filesize']);
// @todo Make this interval configurable.
$expiration = '+5 hours';
$postObject = new PostObjectV4($client, $bucket, $post, $options, $expiration);
$data = [];
$data['attributes'] = $postObject
->getFormAttributes();
$data['inputs'] = $postObject
->getFormInputs();
$data['options'] = $options;
$data['fid'] = $file
->id();
return new JsonResponse($data);
}