You are here

public static function Remote::progress in FileField Sources 8

Menu callback; progress.js callback to return upload progress.

File

src/Plugin/FilefieldSource/Remote.php, line 346

Class

Remote
A FileField source plugin to allow downloading a file from a remote server.

Namespace

Drupal\filefield_sources\Plugin\FilefieldSource

Code

public static function progress($entity_type, $bundle_name, $field_name, $delta) {
  $key = $entity_type . '_' . $bundle_name . '_' . $field_name . '_' . $delta;
  $progress = [
    'message' => t('Starting transfer...'),
    'percentage' => -1,
  ];
  if ($cache = \Drupal::cache()
    ->get('filefield_transfer:' . session_id() . ':' . $key)) {
    $current = $cache->data['current'];
    $total = $cache->data['total'];
    $progress['message'] = t('Transferring... (@current of @total)', [
      '@current' => format_size($current),
      '@total' => format_size($total),
    ]);
    $progress['percentage'] = round(100 * $current / $total);
  }
  return new JsonResponse($progress);
}