diff --git a/flysystem_s3.js b/flysystem_s3.js index c669617..f461715 100644 --- a/flysystem_s3.js +++ b/flysystem_s3.js @@ -196,7 +196,7 @@ */ Drupal.behaviors.flySystemS3CorsUpload = { attach: function (context) { - $(context).find('.js-form-managed-file.flysystem-s3-cors input[type="file"]') + $(context).find('.js-form-managed-file input[type="file"][flysystem-s3-cors]') // Add the CORS upload handler to the file input. .once('auto-cors-upload') .on('change.autoCorsFileUpload', Drupal.flysystemS3.submitCorsUpload) @@ -205,7 +205,7 @@ }, detach: function (context, setting, trigger) { if (trigger === 'unload') { - $(context).find('.js-form-managed-file.flysystem-s3-cors input[type="file"]') + $(context).find('.js-form-managed-file input[type="file"][flysystem-s3-cors]') .removeOnce('auto-cors-upload') .off('change.autoCorsFileUpload', Drupal.flysystemS3.submitCorsUpload); } diff --git a/flysystem_s3.routing.yml b/flysystem_s3.routing.yml index 6bc7cb5..fbfc28b 100644 --- a/flysystem_s3.routing.yml +++ b/flysystem_s3.routing.yml @@ -1,7 +1,7 @@ flysystem_s3.cors: path: '/flysystem-s3/cors-upload-sign' defaults: - _controller: '\Drupal\flysystem_s3\Controller\S3CorsUploadAjaxController::signRequest' + _controller: 'Drupal\flysystem_s3\Controller\S3CorsUploadAjaxController::signRequest' requirements: _permission: 'use S3 CORS upload' _method: 'POST' diff --git a/src/S3CorsManagedFileHelper.php b/src/S3CorsManagedFileHelper.php index 08c2bd8..3c884f4 100644 --- a/src/S3CorsManagedFileHelper.php +++ b/src/S3CorsManagedFileHelper.php @@ -14,7 +14,7 @@ class S3CorsManagedFileHelper { $types['managed_file']['#process'][] = [get_called_class(), 'postProcessCors']; } - public static function preProcessCors(&$element) { + public static function preProcessCors(array &$element) { if (isset($element['#s3_cors']) && !$element['#s3_cors']) { // S3 CORS support has been specifically disabled for this element. return $element; @@ -40,9 +40,6 @@ class S3CorsManagedFileHelper { // Add a flag to the element to indicate that this is a CORS upload. $element['#s3_cors'] = TRUE; - // Add a class to the element for the JS to select this element. - $element['#attributes']['class'][] = 'flysystem-s3-cors'; - // Attach the JS library to the element conditionally. $element['#attached']['library'][] = 'flysystem_s3/drupal.s3_cors_upload'; @@ -56,12 +53,14 @@ class S3CorsManagedFileHelper { return $element; } - public static function postProcessCors(&$element) { + public static function postProcessCors(array &$element) { if (!empty($element['#s3_cors'])) { + // Add data attributes that are used by flysystem_s3.js to submit the // AJAX request to sign the upload. $element['upload']['#attributes']['data-s3-acl'] = $element['#s3_acl']; $element['upload']['#attributes']['data-s3-destination'] = $element['#upload_location']; + $element['upload']['#attributes']['flysystem-s3-cors'] = TRUE; // Add the valid extensions as data attributes. if (!empty($element['#upload_validators']['file_validate_extensions'][0])) { @@ -82,15 +81,7 @@ class S3CorsManagedFileHelper { * The Flysystem file scheme's settings. */ public static function getSchemeSettings($scheme) { - static $settings = []; - - if (!isset($settings[$scheme])) { - /** @var \Drupal\flysystem\FlysystemFactory $factory */ - $factory = \Drupal::service('flysystem_factory'); - $settings[$scheme] = $factory->getSettings($scheme); - } - - return $settings[$scheme]; + return \Drupal::service('flysystem_factory')->getSettings($scheme); } /** @@ -109,6 +100,7 @@ class S3CorsManagedFileHelper { $account = \Drupal::currentUser()->getAccount(); } $settings = static::getSchemeSettings($scheme); + return !empty($settings['driver']) && $settings['driver'] === 's3' && !empty($settings['config']['cors']) && $account->hasPermission('use S3 CORS upload'); } @@ -127,9 +119,8 @@ class S3CorsManagedFileHelper { if (isset($settings['config']['options']['ACL'])) { return $settings['config']['options']['ACL']; } - else { - return 'private'; - } + + return 'private'; } }