uploadFile method

Future<CloudinaryResponse> uploadFile(
  1. CloudinaryFile file, {
  2. String? uploadPreset,
  3. ProgressCallback? onProgress,
})

Upload the cloudinary file to using the public api Override the default upload preset (when CloudinaryPublic is instantiated) with this one (if specified).

Implementation

Future<CloudinaryResponse> uploadFile(
  CloudinaryFile file, {
  String? uploadPreset,
  ProgressCallback? onProgress,
}) async {
  if (cache) {
    if (_uploadedFiles.containsKey(file.identifier)) {
      return _uploadedFiles[file.identifier]!.enableCache();
    }
  }

  Map<String, dynamic> data =
      file.toFormData(uploadPreset: uploadPreset ?? _uploadPreset);

  if (file.fromExternalUrl) {
    data[_fieldName] = file.url!;
  } else {
    data[_fieldName] = await file.toMultipartFile();
  }

  var response = await _dioClient.post(
    _createUrl(file.resourceType),
    data: FormData.fromMap(data),
    onSendProgress: onProgress,
  );

  if (response.statusCode != 200) {
    throw CloudinaryException(
      response.data,
      response.statusCode ?? 0,
      request: {
        'url': file.url,
        'path': file.filePath,
        'public_id': file.identifier,
        'identifier': file.identifier,
      },
    );
  }

  final cloudinaryResponse = CloudinaryResponse.fromMap(
    response.data,
  );

  if (cache) {
    /// Temporary cache for this class instance
    _uploadedFiles[file.identifier] = cloudinaryResponse;
  }
  return cloudinaryResponse;
}