Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ignore_for_file: invalid_return_type_for_catch_error
- import 'package:cloud_firestore/cloud_firestore.dart';
- import 'package:encrypt/encrypt.dart';
- import 'device_info.dart';
- class Database {
- final _firestore = FirebaseFirestore.instance;
- bool shareSecurely(
- String deviceId,
- String receptentDeviceId,
- String validty,
- List<String> urls,
- String key,
- List<String> types,
- ) {
- List<String> sharedItem = [];
- for (String url in urls) {
- final encrypter = Encrypter(AES(Key.fromUtf8(key.substring(0, 32))));
- final iv = IV.fromUtf8(key.substring(0, 16));
- final encrypted = encrypter.encrypt(url, iv: iv);
- sharedItem.add(encrypted.base64);
- }
- _firestore.collection('Device IDs').doc(deviceId).collection('Shared').add({
- "receptentDeviceId": receptentDeviceId,
- "validity": validty,
- "sharedItem": sharedItem,
- "key": key,
- "types": types,
- }).then((value) {
- // print("Data Sent Sucessfully");
- _firestore
- .collection('Device IDs')
- .doc(receptentDeviceId)
- .collection('Received')
- .doc(value.id)
- .set({
- 'senderDeviceId': deviceId,
- "validity": validty,
- "sharedItem": sharedItem,
- "key": key,
- "types": types,
- }).then((_) {
- // print("Data Received Sucessfully");
- return true;
- }).catchError((e) {
- // print(e);
- return false;
- });
- }).catchError((e) {
- // print(e);
- return false;
- });
- return true;
- }
- void createDeviceId(String deviceId) {
- _firestore.collection('Device IDs').doc(deviceId).set({
- "deviceId": deviceId,
- });
- }
- Future<List<String>> getDeviceIds() async {
- late List<String> deviceIds = [];
- await _firestore.collection('Device IDs').get().then((value) {
- for (var i in value.docs) {
- deviceIds.add(i.id.toString());
- }
- });
- return deviceIds;
- }
- void upDateFiles(String docId, List<String> urls, List<String> types,
- String receiverId, String key) {
- List<String> sharedItem = [];
- for (String url in urls) {
- final encrypter = Encrypter(AES(Key.fromUtf8(key.substring(0, 32))));
- final iv = IV.fromUtf8(key.substring(0, 16));
- final encrypted = encrypter.encrypt(url, iv: iv);
- sharedItem.add(encrypted.base64);
- }
- // print(urls);
- if (urls.isEmpty) {
- // print("deleting empty");
- _firestore
- .collection('Device IDs')
- .doc(DeviceInfo().deviceID)
- .collection('Shared')
- .doc(docId)
- .delete()
- .then((value) {
- _firestore
- .collection('Device IDs')
- .doc(receiverId)
- .collection('Received')
- .doc(docId)
- .delete();
- });
- return;
- }
- _firestore
- .collection('Device IDs')
- .doc(DeviceInfo().deviceID)
- .collection('Shared')
- .doc(docId)
- .update({"sharedItem": sharedItem, "types": types}).then((value) {
- _firestore
- .collection('Device IDs')
- .doc(receiverId)
- .collection('Received')
- .doc(docId)
- .update({"sharedItem": sharedItem, "types": types});
- });
- }
- // Future<List<String>> senderDeviceIDs()async{
- // String deviceId = DeviceInfo().deviceID;
- // }
- }
Add Comment
Please, Sign In to add comment