upload method

Future<CloudflareHTTPResponse<CloudflareImage?>> upload({
  1. DataTransmit<File>? contentFromFile,
  2. DataTransmit<String>? contentFromPath,
  3. DataTransmit<Uint8List>? contentFromBytes,
  4. DataTransmit<String>? contentFromUrl,
  5. bool? requireSignedURLs,
  6. Map<String, dynamic>? metadata,
})

An image up to 10 Megabytes can be upload.

Documentation: https://api.cloudflare.com/#cloudflare-images-upload-an-image-using-a-single-http-request

Implementation

Future<CloudflareHTTPResponse<CloudflareImage?>> upload({
  /// 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,

  /// An url to fetch an image from origin and upload it.
  ///
  /// e.g: "https://example.com/some/path/to/image.jpeg"
  DataTransmit<String>? contentFromUrl,

  /// Indicates whether the image requires a signature token for the access.
  ///
  /// default value: false
  ///
  /// valid values: (true,false)
  bool? requireSignedURLs,

  /// User modifiable key-value store. Can use used for keeping references to
  /// another system of record for managing images.
  ///
  /// e.g: "{\"meta\": \"metaID\"}"
  Map<String, dynamic>? metadata,
}) async {
  assert(!isBasic, RestAPIService.authorizedRequestAssertMessage);
  assert(
      contentFromFile != null ||
          contentFromPath != null ||
          contentFromBytes != null ||
          contentFromUrl != null,
      'One of the content must be specified.');

  final CloudflareHTTPResponse<CloudflareImage?> response;

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

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

  if (contentFromFile != null) {
    response = await parseResponse(service.uploadFromFile(
      file: contentFromFile.data,
      requireSignedURLs: requireSignedURLs,
      metadata: metadata,
      onUploadProgress: contentFromFile.progressCallback,
      cancelToken: contentFromFile.cancelToken,
    ));
  } else if (contentFromBytes != null) {
    response = await parseResponse(service.uploadFromBytes(
      bytes: contentFromBytes.data,
      requireSignedURLs: requireSignedURLs,
      metadata: metadata,
      onUploadProgress: contentFromBytes.progressCallback,
      cancelToken: contentFromBytes.cancelToken,
    ));
  } else {
    response = await parseResponse(service.uploadFromUrl(
      url: contentFromUrl!.data,
      requireSignedURLs: requireSignedURLs,
      metadata: metadata,
      onUploadProgress: contentFromUrl.progressCallback,
      cancelToken: contentFromUrl.cancelToken,
    ));
  }

  return response;
}