More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / modules / file / src / Controller / FileWidgetAjaxController.php
1 <?php
2
3 namespace Drupal\file\Controller;
4
5 use Symfony\Component\HttpFoundation\JsonResponse;
6
7 /**
8  * Defines a controller to respond to file widget AJAX requests.
9  */
10 class FileWidgetAjaxController {
11
12   /**
13    * Returns the progress status for a file upload process.
14    *
15    * @param string $key
16    *   The unique key for this upload process.
17    *
18    * @return \Symfony\Component\HttpFoundation\JsonResponse
19    *   A JsonResponse object.
20    */
21   public function progress($key) {
22     $progress = [
23       'message' => t('Starting upload...'),
24       'percentage' => -1,
25     ];
26
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']);
33       }
34     }
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']);
40       }
41     }
42
43     return new JsonResponse($progress);
44   }
45
46 }