diff --git a/core/modules/file/src/Controller/FileWidgetAjaxController.php b/core/modules/file/src/Controller/FileWidgetAjaxController.php index ef09a79..3e45c80 100644 --- a/core/modules/file/src/Controller/FileWidgetAjaxController.php +++ b/core/modules/file/src/Controller/FileWidgetAjaxController.php @@ -15,34 +15,70 @@ class FileWidgetAjaxController { /** - * Returns the progress status for a file upload process. + * Determines the file progress implementation available and passes the $key + * on to the implementation wrapper. * * @param string $key * The unique key for this upload process. * + * @see file_progress_implementation() + */ + public function progress($key) { + if ($implementation = ucfirst(file_progress_implementation())) { + $this->{"progress$implementation"}($key); + } + else { + $this->sendResponse(); + } + } + + /** + * Retrieves the file upload progress status from uploadprogress. + * + * @param string $key + * The unique key for this upload process. + */ + protected function progressUploadprogress($key) { + $this->sendResponse(uploadprogress_get_info($key), 'bytes_uploaded', 'bytes_total'); + } + + /** + * Retrieves the file upload progress status from APC. + * + * @param string $key + * The unique key for this upload process. + * + */ + protected function progressApc($key) { + $this->sendResponse(apc_fetch('upload_' . $key)); + } + + /** + * Sends the file progress status information back to the browser. + * + * @param array $status + * An array with upload status information retrieved from the file + * progress implementation. + * @param string $current + * The array key to use to get the current upload progress (in bytes) + * from $status. + * @param string $total + * The array key to use to get the total bytes to upload from $status. + * * @return \Symfony\Component\HttpFoundation\JsonResponse * A JsonResponse object. */ - public function progress($key) { + protected function sendResponse($status = [], $current = 'current', $total = 'total') { $progress = array( 'message' => t('Starting upload...'), 'percentage' => -1, ); - - $implementation = file_progress_implementation(); - if ($implementation == 'uploadprogress') { - $status = uploadprogress_get_info($key); - if (isset($status['bytes_uploaded']) && !empty($status['bytes_total'])) { - $progress['message'] = t('Uploading... (@current of @total)', array('@current' => format_size($status['bytes_uploaded']), '@total' => format_size($status['bytes_total']))); - $progress['percentage'] = round(100 * $status['bytes_uploaded'] / $status['bytes_total']); - } - } - elseif ($implementation == 'apc') { - $status = apcu_fetch('upload_' . $key); - if (isset($status['current']) && !empty($status['total'])) { - $progress['message'] = t('Uploading... (@current of @total)', array('@current' => format_size($status['current']), '@total' => format_size($status['total']))); - $progress['percentage'] = round(100 * $status['current'] / $status['total']); - } + if (isset($status[$current]) && !empty($status[$total])) { + $progress['message'] = t('Uploading... (@current of @total)', array( + '@current' => format_size($status[$current]), + '@total' => format_size($status[$total]) + )); + $progress['percentage'] = round(100 * $status[$current] / $status[$total]); } return new JsonResponse($progress);