class FileSizeUtil {
  /// A method returns a human readable string representing a file fileSize
  static String fileSize(dynamic size, [int round = 2]) {
    /**
     * [size] can be passed as number or as string
     *
     * the optional parameter [round] specifies the number
     * of digits after comma/point (default is 2)
     */
    var divider = 1024;
    int fileSize;
    try {
      fileSize = int.parse(size.toString());
    } catch (e) {
      throw ArgumentError('Can not parse the size parameter: $e');
    }

    if (fileSize < divider) {
      return '$fileSize B';
    }

    if (fileSize < divider * divider && fileSize % divider == 0) {
      return '${(fileSize / divider).toStringAsFixed(0)} KB';
    }

    if (fileSize < divider * divider) {
      return '${(fileSize / divider).toStringAsFixed(round)} KB';
    }

    if (fileSize < divider * divider * divider && fileSize % divider == 0) {
      return '${(fileSize / (divider * divider)).toStringAsFixed(0)} MB';
    }

    if (fileSize < divider * divider * divider) {
      return '${(fileSize / divider / divider).toStringAsFixed(round)} MB';
    }

    if (fileSize < divider * divider * divider * divider && fileSize % divider == 0) {
      return '${(fileSize / (divider * divider * divider)).toStringAsFixed(0)} GB';
    }

    if (fileSize < divider * divider * divider * divider) {
      return '${(fileSize / divider / divider / divider).toStringAsFixed(round)} GB';
    }

    if (fileSize < divider * divider * divider * divider * divider &&
        fileSize % divider == 0) {
      num r = fileSize / divider / divider / divider / divider;
      return '${r.toStringAsFixed(0)} TB';
    }

    if (fileSize < divider * divider * divider * divider * divider) {
      num r = fileSize / divider / divider / divider / divider;
      return '${r.toStringAsFixed(round)} TB';
    }

    if (fileSize < divider * divider * divider * divider * divider * divider &&
        fileSize % divider == 0) {
      num r = fileSize / divider / divider / divider / divider / divider;
      return '${r.toStringAsFixed(0)} PB';
    } else {
      num r = fileSize / divider / divider / divider / divider / divider;
      return '${r.toStringAsFixed(round)} PB';
    }
  }
}