A flutter library for working with Uploadcare REST API

admin

Flutter Uploadcare Client

Flutter Uploadcare Client

Introduction

Uploadcare is a complete file handling platform that helps you ship products faster and focus on your business goals, not files. With Uploadcare, you can build an infrastructure, optimize content, conversions, load times, traffic, and user experience.

Implemented features:

  • authorization
    • simple,
    • regular,
  • upload,
    • base
    • multipart
    • from url
    • signed uploads,
    • cancellable upload
  • files API
    • get one file
    • get list of files
    • remove multiple files
    • store multiple files
    • gif to video
    • face recognition
  • groups API
    • get one group
    • get list of groups
    • create group
    • store all files in group
  • video encoding
    • create processing tasks
    • retrieve processing status
  • CDN API
    • image transformations
    • group transformations
    • video transformations
  • Flutter (mobile)
    • UploadcareImageProvider
  • Web: currently not working on the web, because flutter API (stable/master) differs for NetworkProvider, and File API from dart:io & dart:html is not the same.

Roadmap:

  • document conversion
  • write more tests

alt flutter uploadcare example

Example:

Note: you can omit privateKey, but in this case only Upload API will be available. (CDN API also will be available).

How to use library:

// create client with simple auth scheme
final client = UploadcareClient.withSimpleAuth(
  publicKey: 'UPLOADCARE_PUBLIC_KEY',
  privateKey: 'UPLOADCARE_PRIVATE_KEY',
  apiVersion: 'v0.5',
);
// or create client with reqular auth scheme
final client = UploadcareClient.withRegularAuth(
  publicKey: 'UPLOADCARE_PUBLIC_KEY',
  privateKey: 'UPLOADCARE_PRIVATE_KEY',
  apiVersion: 'v0.5',
);
// or more flexible
final client = UploadcareClient(
  options: ClientOptions(
    authorizationScheme: AuthSchemeRegular(
      apiVersion: 'v0.5',
      publicKey: 'UPLOADCARE_PUBLIC_KEY',
      privateKey: 'UPLOADCARE_PRIVATE_KEY',
    ),
    // rest options...
  ),
);

UploadcareClient has at the moment 4 API section

final ApiUpload upload;
final ApiFiles files;
final ApiVideoEncoding videoEncoding;
final ApiGroups groups;

You can use each api section separately, for example:

final options = ClientOptions(
  authorizationScheme: AuthSchemeRegular(
    apiVersion: 'v0.5',
    publicKey: 'UPLOADCARE_PUBLIC_KEY',
    privateKey: 'UPLOADCARE_PRIVATE_KEY',
  )
);

final upload = ApiUpload(options: options);
final fileId = await upload.base(File('...some/file'));
// ...etc.

Using with widgets

The library provides UploadcareImageProvider for more effective use in the widget ecosystem, how to use image provider:

Image(
  image: UploadcareImageProvider(
    'uploadcare-image-file-uuid',
    // optional, apply transformations to the image
    transformations: [
      BlurTransformation(50),
      GrayscaleTransformation(),
      InvertTransformation(),
      ImageResizeTransformation(Size.square(58))
    ],
    // rest image props...
  ),
)

Cancellation

You can cancel the upload process by using CancelToken, each method from the upload section (auto, base, multipart) accepts cancelToken property, which you can use to cancel the upload process. This feature works only with files upload because Uploadcare isn’t supporting interrupt upload by URL

...

final cancelToken = CancelToken();

...

try {
  final fileId = await client.upload.multipart(
    File('/some/file'),
    cancelToken: cancelToken,
  );
} on CancelUploadException catch (e) {
  // cancelled
}

...

// somewhere in code
cancelToken.cancel();

Face Recognition

...
final files = ApiFiles(options: options);

final List<Rect> faces = await files.detectFaces('image-id');

Gif to video

final file = CdnFile('gif-id-1')
  ..transform(GifToVideoTransformation([
    VideoFormatTransformation(VideoFormatTValue.Mp4),
    QualityTransformation(QualityTValue.Best),
  ]));

...

VideoPlayerController.network(file.url);

Video encoding

...

final videoEncoding = ApiVideoEncoding(options);

final VideoEncodingConvertEntity result = await videoEncoding.process({
  'video-id-1': [
    CutTransformation(
      const const Duration(seconds: 10),
      length: const Duration(
        seconds: 30,
      ),
    )
  ],
  'video-id-2': [
    VideoResizeTransformation(const Size(512, 384)),
    VideoThumbsGenerateTransformation(10),
  ],
});

final Stream<VideoEncodingJobEntity> processingStream = videoEncoding.statusAsStream(
  result.results.first.token,
  checkInterval: const Duration(seconds: 2),
)..listen((VideoEncodingJobEntity status) {
  // do something
})

Source Credit Url : https://github.com/KonstantinKai/uploadcare_client

Leave a Comment