upload static method

Future<void> upload({
  1. required CommandLogger logger,
  2. required FileUploaderFactory fileUploaderFactory,
  3. required String uploadDescription,
  4. required List<int> projectZip,
  5. String subject = 'project',
})

Uploads projectZip to the destination in uploadDescription while showing the progress.

Throws ErrorExitException if the upload did not succeed.

Implementation

static Future<void> upload({
  required CommandLogger logger,
  required FileUploaderFactory fileUploaderFactory,
  required String uploadDescription,
  required List<int> projectZip,
  String subject = 'project',
}) async {
  final total = projectZip.length;
  final events = StreamController<_UploadProgress>();
  var lastPercent = -1;

  void onProgress(final int sent, final int _) {
    final percent = total == 0 ? 100 : sent * 100 ~/ total;
    if (percent == lastPercent) return;
    lastPercent = percent;
    events.add(_UploadProgress.sending(subject, sent, total));
  }

  unawaited(
    _runUpload(
      fileUploaderFactory,
      uploadDescription,
      projectZip,
      events,
      onProgress,
    ),
  );

  final _UploadProgress result;
  try {
    result = await logger.progressStream(
      'Uploading $subject...',
      events.stream,
      toMessage: (final p) => p.message,
      isSuccess: (final p) => p.succeeded,
      padRight: StatusCommands.progressMessagePadLength,
    );
  } on DioException catch (e) {
    throw failureFromDioException(e, action: 'upload the $subject');
  } on Exception catch (e, stackTrace) {
    throw FailureException.nested(
      e,
      stackTrace,
      'Failed to upload $subject.',
    );
  }

  if (!result.succeeded) {
    logger.error('Failed to upload $subject, please try again.');
    throw ErrorExitException('Failed to upload $subject.');
  }
}