cloudinaryUpload method

void cloudinaryUpload({
  1. required List<String> paths,
  2. required String uploadPreset,
  3. required String cloudName,
  4. void onProgress(
    1. int,
    2. int
    )?,
  5. dynamic onException(
    1. CloudinaryException
    )?,
})

Uploads one or more image files to Cloudinary using the specified upload preset and cloud name.

paths is a list of file paths to upload.

uploadPreset is the upload preset to use for the upload.

cloudName is the name of the Cloudinary account to upload the files to.

onProgress is an optional callback function that is called with the current progress of the upload, as a percentage.

onException is an optional callback function that is called if an exception occurs during the upload. The function is passed a CloudinaryException object.

Example usage:

RhUtils.instance.cloudinaryUpload(
  paths: ['/path/to/image1.jpg', '/path/to/image2.png'],
  uploadPreset: 'upload_preset',
  cloudName: 'cloud_name',
  onProgress: (sent, total) => print('$sent of $total bytes uploaded'),
  onException: (e) => print('Upload failed: ${e.message}'),
);

Implementation

void cloudinaryUpload({
  required List<String> paths,
  required String uploadPreset,
  required String cloudName,
  void Function(int, int)? onProgress,
  Function(CloudinaryException)? onException,
}) async {
  final cloudinary = CloudinaryPublic(cloudName, uploadPreset);

  if (paths.isNotEmpty) {
    try {
      for (var path in paths) {
        await cloudinary.uploadFile(
            CloudinaryFile.fromFile(path,
                resourceType: CloudinaryResourceType.Image),
            onProgress: onProgress);
      }
    } on CloudinaryException catch (e) {
      if (onException != null) {
        onException(e);
      }
    }
  }
}