resolveUploadParameters method

Future<({Uint8List bodyBytes, String contentType, String filePath, String? release})?> resolveUploadParameters({
  1. ReleaseBundle? releaseBundle,
})

Implementation

Future<
    ({
      String filePath,
      Uint8List bodyBytes,
      String contentType,
      String? release
    })?> resolveUploadParameters({ReleaseBundle? releaseBundle}) async {
  String? directory;
  String? file;
  String? release;
  Object? body;

  var contentType = parameters?['contentType'] as String?;

  if (this.body == '%RELEASE_BUNDLE%') {
    if (releaseBundle == null) {
      print('  ▒  Release bundle not provided for body: $this');
      return null;
    } else {
      print('   »  Using `ReleaseBundle` as body.');

      file = parameters?['file'] as String?;
      if (file != null) {
        var release = releaseBundle.release;
        var fileFormatted = ReleaseBundle.formatReleaseBundleFile(
            file, release.name, release.version, release.platform);
        file = fileFormatted;

        print('   »  Parameter `file`: $fileFormatted');
      }

      release = parameters?['release'] as String?;
      if (release != null && release.toLowerCase() == '%release%') {
        release = releaseBundle.release.toString();
        release = release;
      }
    }

    body = await releaseBundle.toBytes();
    contentType ??= releaseBundle.contentType;
  } else {
    file = parameters?['file'] as String?;
    release = parameters?['release'] as String?;
    body = this.body;
  }

  contentType ??= 'application/octet-stream';

  directory = parameters?['directory'] as String?;

  if (file == null) {
    throw ArgumentError.notNull("file");
  }

  if (body == null) {
    throw ArgumentError.notNull("body");
  }

  String filePath;
  if (directory != null && directory.isNotEmpty) {
    var pathContext = pack_path.Context(style: pack_path.Style.posix);
    filePath = pathContext.normalize(pathContext.join(directory, file));
  } else {
    filePath = file;
  }

  Uint8List bodyBytes;
  if (body is String) {
    bodyBytes = dart_convert.utf8.encode(body);
  } else if (body is List<int>) {
    bodyBytes = body.asUint8List;
  } else {
    throw ArgumentError("Invalid body type: ${body.runtimeType}");
  }

  return (
    filePath: filePath,
    bodyBytes: bodyBytes,
    contentType: contentType,
    release: release,
  );
}