Skip to content

Commit

Permalink
Adding upload progress
Browse files Browse the repository at this point in the history
  • Loading branch information
seanconnollydev committed Oct 6, 2021
1 parent e4d91fe commit 03e791f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ build/
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages

# .fvm
.fvm
11 changes: 10 additions & 1 deletion example/lib/src/image_picker_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class _ImagePickerExampleState extends State<ImagePickerExample> {
final picker = ImagePicker();
PickedFile _pickedFile;
bool _uploading = false;
double _uploadingPercentage = 0.0;

Future getImage() async {
final image = await picker.getImage(source: ImageSource.gallery);
Expand Down Expand Up @@ -54,7 +55,9 @@ class _ImagePickerExampleState extends State<ImagePickerExample> {
_buildImage(),
TextButton(
onPressed: _uploading ? null : _upload,
child: _uploading ? Text('Uploading...') : Text('Upload'),
child: _uploading
? Text('${_uploadingPercentage.toStringAsFixed(2)}%')
: Text('Upload'),
),
],
);
Expand All @@ -75,6 +78,11 @@ class _ImagePickerExampleState extends State<ImagePickerExample> {
'caption': 'An example image',
},
),
onSendProgress: (count, total) {
setState(() {
_uploadingPercentage = (count / total) * 100;
});
},
);
print(res);
} on CloudinaryException catch (e) {
Expand All @@ -84,6 +92,7 @@ class _ImagePickerExampleState extends State<ImagePickerExample> {

setState(() {
_uploading = false;
_uploadingPercentage = 0.0;
});
}

Expand Down
16 changes: 14 additions & 2 deletions lib/src/cloudinary_public.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import 'dart:async';
import 'dart:convert';

import 'package:cloudinary_public/cloudinary_public.dart';
import 'package:cloudinary_public/src/exceptions/cloudinary_exception.dart';
import 'package:cloudinary_public/src/progress_callback.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;

import './cloudinary_response.dart';
import '../cloudinary_public.dart';
import 'multipart_request.dart';

/// The base class for this package
class CloudinaryPublic {
Expand Down Expand Up @@ -62,6 +65,7 @@ class CloudinaryPublic {
Future<CloudinaryResponse> uploadFile(
CloudinaryFile file, {
String? uploadPreset,
ProgressCallback? onSendProgress,
}) async {
if (cache) {
assert(file.identifier != null, 'identifier is required for caching');
Expand All @@ -74,9 +78,12 @@ class CloudinaryPublic {
'${describeEnum(file.resourceType).toLowerCase()}'
'/upload';

final request = http.MultipartRequest(
final request = MultipartRequest(
'POST',
Uri.parse(url),
onProgress: (count, total) {
onSendProgress?.call(count, total);
},
);

request.headers.addAll({
Expand Down Expand Up @@ -147,8 +154,13 @@ class CloudinaryPublic {
Future<CloudinaryResponse> uploadFutureFile(
Future<CloudinaryFile> file, {
String? uploadPreset,
ProgressCallback? onSendProgress,
}) async {
return uploadFile(await file, uploadPreset: uploadPreset);
return uploadFile(
await file,
uploadPreset: uploadPreset,
onSendProgress: onSendProgress,
);
}

/// Upload multiple files using simultaneously [uploadFutureFile]
Expand Down
36 changes: 36 additions & 0 deletions lib/src/multipart_request.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'dart:async';

import 'package:http/http.dart' as http;

class MultipartRequest extends http.MultipartRequest {
/// Creates a new [MultipartRequest].
MultipartRequest(
String method,
Uri url, {
this.onProgress,
}) : super(method, url);

final void Function(int bytes, int totalBytes)? onProgress;

/// Freezes all mutable fields and returns a
/// single-subscription [http.ByteStream]
/// that will emit the request body.
@override
http.ByteStream finalize() {
final byteStream = super.finalize();
if (onProgress == null) return byteStream;

final total = contentLength;
var bytes = 0;

final t = StreamTransformer.fromHandlers(
handleData: (List<int> data, EventSink<List<int>> sink) {
bytes += data.length;
onProgress?.call(bytes, total);
sink.add(data);
},
);
final stream = byteStream.transform(t);
return http.ByteStream(stream);
}
}
1 change: 1 addition & 0 deletions lib/src/progress_callback.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
typedef ProgressCallback = void Function(int count, int total);
8 changes: 4 additions & 4 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.6.1"
version: "2.8.1"
boolean_selector:
dependency: transitive
description:
Expand All @@ -28,7 +28,7 @@ packages:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.1"
clock:
dependency: transitive
description:
Expand Down Expand Up @@ -87,7 +87,7 @@ packages:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.7.0"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -148,7 +148,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.0"
version: "0.4.2"
typed_data:
dependency: transitive
description:
Expand Down

0 comments on commit 03e791f

Please sign in to comment.