Last active
December 11, 2023 14:39
-
-
Save leviyehonatan/9edb3dd2c1b0a1c3dcd6d55605e44d1e to your computer and use it in GitHub Desktop.
uploading parallel with status
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void _process() async { | |
final uuid = Uuid(); | |
final random = uuid.v4(); | |
final outputFilename = | |
"${(await getTemporaryDirectory()).path}/$random.png"; | |
await compute(stitch, [widget.images, outputFilename]); | |
setState(() { | |
_outputFilename = outputFilename; | |
}); | |
img.Image? output = await img.decodeImageFile(outputFilename); | |
setState(() { | |
_outputImage = output; | |
}); | |
setState(() { | |
_uploading = true; | |
}); | |
List<String> uploadFiles = [...widget.images, _outputFilename!]; | |
final fileSizes = await Future.wait( | |
uploadFiles.map((filename) async => await File(filename).length())); | |
List<int> bytesDownloaded = List.generate(fileSizes.length, (index) => 0); | |
int sumUsingReduce(List<int> numbers) { | |
return numbers.reduce((value, element) => value + element); | |
} | |
final totalBytes = sumUsingReduce(fileSizes); | |
await uploadFilesToBucket( | |
uploadFiles, random, (int index, TaskSnapshot snapshot) { | |
bytesDownloaded[index] = snapshot.bytesTransferred; | |
final sumDownloaded = sumUsingReduce(bytesDownloaded); | |
final percent = sumDownloaded / totalBytes; | |
setState(() { | |
_uploadStatus = | |
"Uploading ${getFileSizeString(bytes: totalBytes)} (${(percent*100).truncate()}%)"; | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'package:path/path.dart' as path; | |
import 'package:firebase_storage/firebase_storage.dart'; | |
Future<void> uploadFilesToBucket( | |
List<String> filenames, String storageBucketPath, void Function(int,TaskSnapshot) onProgress) async { | |
final FirebaseStorage storage = FirebaseStorage.instance; | |
final Reference storageReference = storage.ref(storageBucketPath); | |
await Future.wait(filenames.asMap().entries.map((entry) async { | |
int index = entry.key; | |
String filename = entry.value; | |
String paddedNumberString = index == filenames.length - 1 | |
? "output" | |
: (index + 1).toString().padLeft(3, "0"); | |
String extension = path.extension(filename); | |
String bucketPath = "$paddedNumberString$extension"; | |
try { | |
UploadTask uploadTask = storageReference.child(bucketPath).putFile( | |
File(filename)); | |
uploadTask.snapshotEvents.listen((snapshot) { | |
onProgress(index, snapshot); | |
}); | |
await uploadTask; | |
} on FirebaseException catch (e) { | |
print('Error uploading $filename: $e'); | |
} | |
})); | |
} | |
Future<int> getTotalFileSize(List<String> filePaths) async { | |
int totalSize = 0; | |
for (String filePath in filePaths) { | |
totalSize += await getFileSize(filePath); | |
} | |
return totalSize; | |
} | |
Future<int> getFileSize(String filePath) async { | |
File file = File(filePath); | |
if (await file.exists()) { | |
int size = await file.length(); | |
return size; | |
} else { | |
print('File not found: $filePath'); | |
return 0; | |
} | |
} | |
String getFileSizeString({required int bytes, int decimals = 0}) { | |
const suffixes = ["b", "kb", "mb", "gb", "tb"]; | |
if (bytes == 0) return '0${suffixes[0]}'; | |
var i = (log(bytes) / log(1024)).floor(); | |
return ((bytes / pow(1024, i)).toStringAsFixed(decimals)) + suffixes[i]; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment