upload method

  1. @override
Future<CloudFile> upload(
  1. String path,
  2. Uint8List bytes, {
  3. String? contentType,
  4. void onProgress(
    1. double sent
    )?,
})
override

Uploads bytes to path. onProgress gets the fraction sent (0–1) as the upload goes, when the storage can tell.

Implementation

@override
Future<CloudFile> upload(String path, Uint8List bytes, {String? contentType, void Function(double sent)? onProgress}) async {
  final ref = _ref(path);
  final type = contentType ?? CloudFilePaths.contentTypeFor(ref.name.split('.').last);
  final task = ref.putData(bytes, SettableMetadata(contentType: type));
  final progress = onProgress == null
      ? null
      : task.snapshotEvents.listen((s) {
          if (s.totalBytes > 0) onProgress(s.bytesTransferred / s.totalBytes);
        }, onError: (_) {});
  try {
    await task;
  } finally {
    await progress?.cancel();
  }
  onProgress?.call(1);
  return CloudFile(path: ref.fullPath, url: await ref.getDownloadURL(), name: ref.name, size: bytes.length, contentType: type);
}