3 namespace Drupal\file\Controller;
5 use Symfony\Component\HttpFoundation\JsonResponse;
8 * Defines a controller to respond to file widget AJAX requests.
10 class FileWidgetAjaxController {
13 * Returns the progress status for a file upload process.
16 * The unique key for this upload process.
18 * @return \Symfony\Component\HttpFoundation\JsonResponse
19 * A JsonResponse object.
21 public function progress($key) {
23 'message' => t('Starting upload...'),
27 $implementation = file_progress_implementation();
28 if ($implementation == 'uploadprogress') {
29 $status = uploadprogress_get_info($key);
30 if (isset($status['bytes_uploaded']) && !empty($status['bytes_total'])) {
31 $progress['message'] = t('Uploading... (@current of @total)', ['@current' => format_size($status['bytes_uploaded']), '@total' => format_size($status['bytes_total'])]);
32 $progress['percentage'] = round(100 * $status['bytes_uploaded'] / $status['bytes_total']);
35 elseif ($implementation == 'apc') {
36 $status = apcu_fetch('upload_' . $key);
37 if (isset($status['current']) && !empty($status['total'])) {
38 $progress['message'] = t('Uploading... (@current of @total)', ['@current' => format_size($status['current']), '@total' => format_size($status['total'])]);
39 $progress['percentage'] = round(100 * $status['current'] / $status['total']);
43 return new JsonResponse($progress);