You are here

private function MediaSubFormManager::formatSize in Media Bulk Upload 8

Format the amount of bites into a common string format.

Parameters

int $size: Size in bytes.

Return value

string Formatted file size.

1 call to MediaSubFormManager::formatSize()
MediaSubFormManager::__construct in src/MediaSubFormManager.php
BulkMediaUploadForm constructor.

File

src/MediaSubFormManager.php, line 107

Class

MediaSubFormManager
Class MediaSubFormManager.

Namespace

Drupal\media_bulk_upload

Code

private function formatSize($size) {
  $unit = 'B';
  $calculatedSize = $size;
  if ($size >= Bytes::KILOBYTE) {
    $calculatedSize = $size / Bytes::KILOBYTE;
    $units = [
      'KB',
      'MB',
      'GB',
      'TB',
      'PB',
      'EB',
      'ZB',
      'YB',
    ];
    foreach ($units as $unit) {
      if (round($calculatedSize, 2) >= Bytes::KILOBYTE) {
        $calculatedSize /= Bytes::KILOBYTE;
      }
      else {
        break;
      }
    }
  }
  $calculatedSize = round($calculatedSize, 2);
  return sprintf('%d ' . $unit, $calculatedSize);
}