directUpload method

Future<CloudflareHTTPResponse<CloudflareImage?>> directUpload({
  1. required DataUploadDraft dataUploadDraft,
  2. DataTransmit<File>? contentFromFile,
  3. DataTransmit<String>? contentFromPath,
  4. DataTransmit<Uint8List>? contentFromBytes,
})

For image direct upload without API key or token. This function is to be used specifically after an image createDirectUpload has been requested.

Documentation: https://api.cloudflare.com/#cloudflare-images-create-authenticated-direct-upload-url-v2

Implementation

Future<CloudflareHTTPResponse<CloudflareImage?>> directUpload({
  /// Information on where to upload the image without an API key or token
  required DataUploadDraft dataUploadDraft,

  /// Image file to upload
  DataTransmit<File>? contentFromFile,

  /// Path to the image file to upload
  DataTransmit<String>? contentFromPath,

  /// Image byte array representation to upload
  DataTransmit<Uint8List>? contentFromBytes,
}) async {
  assert(
      contentFromFile != null ||
          contentFromPath != null ||
          contentFromBytes != null,
      'One of the content must be specified.');

  if (contentFromPath != null) {
    contentFromFile ??= DataTransmit<File>(
      data: File(contentFromPath.data),
      progressCallback: contentFromPath.progressCallback,
      cancelToken: contentFromPath.cancelToken,
    );
  }

  /// Web support
  if (contentFromFile != null && PlatformUtils.isWeb) {
    contentFromBytes ??= DataTransmit<Uint8List>(
      data: contentFromFile.data.readAsBytesSync(),
      progressCallback: contentFromFile.progressCallback,
      cancelToken: contentFromFile.cancelToken,
    );
    contentFromFile = null;
  }

  final dio = restAPI.dio;
  final formData = FormData();
  CancelToken? cancelToken;
  ProgressCallback? progressCallback;
  if (contentFromFile != null) {
    cancelToken = contentFromFile.cancelToken;
    final file = contentFromFile.data;
    progressCallback = contentFromFile.progressCallback;
    formData.files.add(MapEntry(
        Params.file,
        MultipartFile.fromFileSync(file.path,
            filename: file.path.split(Platform.pathSeparator).last)));
  } else {
    cancelToken = contentFromBytes!.cancelToken;
    final bytes = contentFromBytes.data;
    progressCallback = contentFromBytes.progressCallback;
    formData.files.add(MapEntry(
        Params.file,
        MultipartFile.fromBytes(
          bytes,
          filename: null,
        )));
  }

  final rawResponse = await dio.fetch<Map<String, dynamic>?>(Options(
    method: 'POST',
    // headers: _headers,
    responseType: ResponseType.json,
    contentType: 'multipart/form-data',
  ).compose(
      BaseOptions(
        baseUrl: dataUploadDraft.uploadURL,
      ),
      '',
      data: formData,
      onSendProgress: progressCallback,
      cancelToken: cancelToken));
  final value = rawResponse.data == null
      ? null
      : CloudflareResponse.fromJson(rawResponse.data!);
  final httpResponse = HttpResponse(value, rawResponse);

  final response = await parseResponse(Future.value(httpResponse));
  return response;
}