Skip to content

nixrajput/cloudinary-dart

Repository files navigation

cloudinary

A dart package for to integrate Cloudinary API into your dart and flutter app. This package is a wrapper around the Cloudinary API.

pub package Stars Forks Watchers Contributors

GitHub release (latest by date) GitHub last commit GitHub issues GitHub pull requests GitHub Licence

Table of Contents

Installation

Add cloudinary as a dependency in your pubspec.yaml file:

dependencies:
  cloudinary: ^latest_version

Finally you just have to run: dart pub get or flutter pub get depending on the project type and this will download the dependency to your pub-cache.

And import it:

import 'package:cloudinary/cloudinary.dart';

Usage

Initialize a Cloudinary object

/// This three params can be obtained directly from your Cloudinary account Dashboard.
/// The .signedConfig(...) factory constructor is recommended only for server side apps, 
/// where [apiKey] and [apiSecret] are secure. 
final cloudinary = Cloudinary.signedConfig(
  apiKey: apiKey,
  apiSecret: apiSecret,
  cloudName: cloudName,
);

or

/// The .unsignedConfig(...) factory constructor is recommended for client side apps, where [apiKey] and 
/// [apiSecret] must not be used, so .basic(...) constructor allows to do later unsigned requests.
final cloudinary = Cloudinary.unsignedConfig(
  cloudName: cloudName,
);

Do a signed file upload

Recommended only for server side apps.

final response = await cloudinary.upload(
  file: file.path,
  fileBytes: file.readAsBytesSync(),
  resourceType: CloudinaryResourceType.image,
  folder: cloudinaryCustomFolder,
  fileName: 'some-name',
  progressCallback: (count, total) {
    print('Uploading image from file with progress: $count/$total');
  },
);

if (response.isSuccessful) {
  print('Get your image from with ${response.secureUrl}');
}   

You can upload a file from path or byte array representation, you can also pass an optParams map to do a more elaborated upload according to https://cloudinary.com/documentation/image_upload_api_reference. The cloudinary.upload(...) function is fully documented, you can check the description to know what other options you have.

Do a unsigned file upload

Recommended for server client side apps. The way to do this request is almost the same as above, the only difference is the uploadPreset which is required for unsigned uploads.

final response = await cloudinary.unsignedUpload(
  file: file.path,
  uploadPreset: somePreset,
  fileBytes: file.readAsBytesSync(),
  resourceType: CloudinaryResourceType.image,
  folder: cloudinaryCustomFolder,
  fileName: 'some-name',
  progressCallback: (count, total) {
    print('Uploading image from file with progress: $count/$total');
  }
);

if (response.isSuccessful) {
  print('Get your image from with ${response.secureUrl}');
}

You can upload a file from path or byte array representation, you can also pass an optParams map to do a more elaborated upload according to https://cloudinary.com/documentation/image_upload_api_reference. The cloudinary.unsignedUpload(...) function is fully documented, you can check the description to know what other options you have.

Do a file delete (this will use the cloudinary destroy method)

final response = await cloudinary.destroy(
  'public_id',
  url: url,
  resourceType: CloudinaryResourceType.image,
  invalidate: false,
);

if (response.isSuccessful ?? false){
  //Do something else
}

To delete a cloudinary file it´s necessary a public_id, as you can see in the sample code the deleteResource(...) function can delete a file by it's url. You can also pass an optParams map to do a more elaborated delete (destroy) according to https://cloudinary.com/documentation/image_upload_api_reference#destroy_method. The cloudinary.destroy(...) function is fully documented, you can check the description to know what other options you have.

About Cloudinary

Cloudinary is a powerful media API for websites and mobile apps alike, Cloudinary enables developers to efficiently manage, transform, optimize, and deliver images and videos through multiple CDNs. Ultimately, viewers enjoy responsive and personalized visual-media experiences—irrespective of the viewing device.

Contributing

If you would like to contribute to this project, feel free to fork the repository, make your changes, and submit a pull request. Please follow the guidelines in the CONTRIBUTING.md file.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Sponsor Me

  • By sponsoring my efforts, you're not merely contributing to the development of my projects; you're investing in its growth and sustainability.
  • Your support empowers me to dedicate more time and resources to improving the project's features, addressing issues, and ensuring its continued relevance in the rapidly evolving landscape of technology.
  • Your sponsorship directly fuels innovation, fosters a vibrant community, and helps maintain the project's high standards of quality. Together, we can shape the future of the projects and make a lasting impact in the open-source community.
  • Thank you for considering sponsoring my work!

Sponsor

Connect With Me

GitHub: nixrajput Linkedin: nixrajput Instagram: nixrajput Twitter: nixrajput07 Telegram: nixrajput Gmail: nkr.nikhi.nkr@gmail.com

Activities

Alt