uploadFile static method
Future<Tuple2<UploadInfo, Uint8List>>
uploadFile(
- String memberMediumDocumentID,
- String filePath,
- String appId,
- String ownerId,
- String packageName,
- Map<String, String> customMetaData, {
- String? homeURL,
- FeedbackProgress? feedbackProgress,
})
Implementation
static Future<Tuple2<UploadInfo, Uint8List>> uploadFile(
String memberMediumDocumentID,
String filePath,
String appId,
String ownerId,
String packageName,
Map<String, String> customMetaData,
{String? homeURL,
FeedbackProgress? feedbackProgress}) async {
// var file = File(filePath);
try {
var baseName = BaseNameHelper.baseName(memberMediumDocumentID, filePath);
print('baseName = $baseName');
var ref = '$appId/$packageName/$ownerId/$baseName';
firebase_storage.UploadTask uploadTask;
Uint8List bytes;
if (kIsWeb) {
// todo: make parameter
var myUri =
Uri.parse('${homeURL ?? 'https://minkey.io'}/assets/$filePath');
final response = await http.get(myUri);
print('uploadTask = ');
bytes = response.bodyBytes;
} else {
bytes = File(filePath).readAsBytesSync();
}
uploadTask = firebase_storage.FirebaseStorage.instance.ref(ref).putData(
bytes,
firebase_storage.SettableMetadata(customMetadata: customMetaData));
uploadTask.snapshotEvents.listen((event) {
if (feedbackProgress != null) {
feedbackProgress(event.bytesTransferred / event.totalBytes);
}
});
var uploadedTask = await uploadTask;
var url = await uploadedTask.ref.getDownloadURL();
var uploadInfo = UploadInfo(url, ref);
return Tuple2(uploadInfo, bytes);
} on firebase_storage.FirebaseException catch (e) {
throw Exception(
'Exception during file upload. code = $e.code, message = $e.message');
}
}