I'm looking for an efficient way to upload and process images and videos in parallel using Firebase Storage and Firestore. Currently, my approach involves looping through assets, conditionally processing each asset using separate methods for images and videos, compressing with VideoCompress and FlutterImageCompress, uploading to Firebase Storage, retrieving download URLs, and then saving asset documents using a WriteBatch. However, I'm facing two key issues:
- Slow upload performance
- Inadequate progress tracking
I aim to accomplish the following steps quickly:
- Compress images/videos with VideoCompress and FlutterImageCompress
- Upload to Firebase Storage
- Retrieve download URLs
- Create Asset objects and write them to Firestore
Here's my current implementation:
List<AssetEntity> assetsList = [];
WriteBatch batch = FirebaseFirestore.instance.batch();
int totalFiles = assets.length;
int uploadedFiles = 0;
for (final asset in assets) {
late Uint8List? compressedThumbnailData;
late Uint8List? compressedData;
late String contentType;
late String filename;
late String downloadUrl;
late String thumbnailUrl;
if (asset.type == AssetType.video) {} else {}
// conditionally check if asset is video or image, compress it, then assign it
// a contentType, docId, filename, and the compressedData
downloadUrl = await mediaServices.uploadImage(
image: compressedData,
type: contentType,
path: bucketPath,
filename: filename,
);
GeoPoint? location = await getAssetGeoPoint(asset);
final asset = Asset(
... assign all of the models parameters
);
assetsList.add(asset);
final assetDocRef = _firestore.collection("*").doc(docId);
batch.set(assetDocRef, asset.toMap());
uploadedFiles++;
}
await batch.commit();
I'm open to suggestions on how to parallelize the uploads and enhance progress tracking so that the overall process becomes more efficient. Any guidance or improvements would be appreciated.