jbn6972

Untitled

Jan 4th, 2023
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.42 KB | None | 0 0
  1. // ignore_for_file: invalid_return_type_for_catch_error
  2.  
  3. import 'package:cloud_firestore/cloud_firestore.dart';
  4. import 'package:encrypt/encrypt.dart';
  5. import 'device_info.dart';
  6.  
  7. class Database {
  8.   final _firestore = FirebaseFirestore.instance;
  9.   bool shareSecurely(
  10.     String deviceId,
  11.     String receptentDeviceId,
  12.     String validty,
  13.     List<String> urls,
  14.     String key,
  15.     List<String> types,
  16.   ) {
  17.     List<String> sharedItem = [];
  18.  
  19.     for (String url in urls) {
  20.       final encrypter = Encrypter(AES(Key.fromUtf8(key.substring(0, 32))));
  21.       final iv = IV.fromUtf8(key.substring(0, 16));
  22.       final encrypted = encrypter.encrypt(url, iv: iv);
  23.       sharedItem.add(encrypted.base64);
  24.     }
  25.  
  26.     _firestore.collection('Device IDs').doc(deviceId).collection('Shared').add({
  27.       "receptentDeviceId": receptentDeviceId,
  28.       "validity": validty,
  29.       "sharedItem": sharedItem,
  30.       "key": key,
  31.       "types": types,
  32.     }).then((value) {
  33.       // print("Data Sent Sucessfully");
  34.  
  35.       _firestore
  36.           .collection('Device IDs')
  37.           .doc(receptentDeviceId)
  38.           .collection('Received')
  39.           .doc(value.id)
  40.           .set({
  41.         'senderDeviceId': deviceId,
  42.         "validity": validty,
  43.         "sharedItem": sharedItem,
  44.         "key": key,
  45.         "types": types,
  46.       }).then((_) {
  47.         // print("Data Received Sucessfully");
  48.         return true;
  49.       }).catchError((e) {
  50.         // print(e);
  51.         return false;
  52.       });
  53.     }).catchError((e) {
  54.       // print(e);
  55.       return false;
  56.     });
  57.     return true;
  58.   }
  59.  
  60.   void createDeviceId(String deviceId) {
  61.     _firestore.collection('Device IDs').doc(deviceId).set({
  62.       "deviceId": deviceId,
  63.     });
  64.   }
  65.  
  66.   Future<List<String>> getDeviceIds() async {
  67.     late List<String> deviceIds = [];
  68.     await _firestore.collection('Device IDs').get().then((value) {
  69.       for (var i in value.docs) {
  70.         deviceIds.add(i.id.toString());
  71.       }
  72.     });
  73.     return deviceIds;
  74.   }
  75.  
  76.   void upDateFiles(String docId, List<String> urls, List<String> types,
  77.       String receiverId, String key) {
  78.     List<String> sharedItem = [];
  79.  
  80.     for (String url in urls) {
  81.       final encrypter = Encrypter(AES(Key.fromUtf8(key.substring(0, 32))));
  82.       final iv = IV.fromUtf8(key.substring(0, 16));
  83.       final encrypted = encrypter.encrypt(url, iv: iv);
  84.       sharedItem.add(encrypted.base64);
  85.     }
  86.     // print(urls);
  87.     if (urls.isEmpty) {
  88.       // print("deleting empty");
  89.       _firestore
  90.           .collection('Device IDs')
  91.           .doc(DeviceInfo().deviceID)
  92.           .collection('Shared')
  93.           .doc(docId)
  94.           .delete()
  95.           .then((value) {
  96.         _firestore
  97.             .collection('Device IDs')
  98.             .doc(receiverId)
  99.             .collection('Received')
  100.             .doc(docId)
  101.             .delete();
  102.       });
  103.       return;
  104.     }
  105.  
  106.     _firestore
  107.         .collection('Device IDs')
  108.         .doc(DeviceInfo().deviceID)
  109.         .collection('Shared')
  110.         .doc(docId)
  111.         .update({"sharedItem": sharedItem, "types": types}).then((value) {
  112.       _firestore
  113.           .collection('Device IDs')
  114.           .doc(receiverId)
  115.           .collection('Received')
  116.           .doc(docId)
  117.           .update({"sharedItem": sharedItem, "types": types});
  118.     });
  119.   }
  120.  
  121.   // Future<List<String>> senderDeviceIDs()async{
  122.   //   String deviceId = DeviceInfo().deviceID;
  123.  
  124.   // }
  125. }
  126.  
Add Comment
Please, Sign In to add comment