Browse Source

Upload thumbnail along with the file

Vishnu Mohandas 4 years ago
parent
commit
2e39f2e90a
2 changed files with 26 additions and 2 deletions
  1. 17 1
      lib/photo_sync_manager.dart
  2. 9 1
      lib/utils/crypto_util.dart

+ 17 - 1
lib/photo_sync_manager.dart

@@ -4,6 +4,7 @@ import 'dart:io' as io;
 import 'dart:math';
 import 'dart:math';
 
 
 import 'package:logging/logging.dart';
 import 'package:logging/logging.dart';
+import 'package:photos/core/constants.dart';
 import 'package:photos/core/event_bus.dart';
 import 'package:photos/core/event_bus.dart';
 import 'package:photos/db/files_db.dart';
 import 'package:photos/db/files_db.dart';
 import 'package:photos/events/photo_upload_event.dart';
 import 'package:photos/events/photo_upload_event.dart';
@@ -245,13 +246,25 @@ class PhotoSyncManager {
   }
   }
 
 
   Future<File> _uploadEncryptedFile(File file) async {
   Future<File> _uploadEncryptedFile(File file) async {
+    final key = Configuration.instance.getKey("hello");
+
     final filePath = (await (await file.getAsset()).originFile).path;
     final filePath = (await (await file.getAsset()).originFile).path;
     final encryptedFileName = file.generatedID.toString() + ".aes";
     final encryptedFileName = file.generatedID.toString() + ".aes";
     final encryptedFilePath = _encryptedFilesDirectory + encryptedFileName;
     final encryptedFilePath = _encryptedFilesDirectory + encryptedFileName;
     final fileIV = CryptoUtil.getBase64EncodedSecureRandomString(length: 16);
     final fileIV = CryptoUtil.getBase64EncodedSecureRandomString(length: 16);
-    final key = Configuration.instance.getKey("hello");
     await CryptoUtil.encryptFile(filePath, encryptedFilePath, key, fileIV);
     await CryptoUtil.encryptFile(filePath, encryptedFilePath, key, fileIV);
 
 
+    final thumbnailData = (await (await file.getAsset())
+        .thumbDataWithSize(THUMBNAIL_LARGE_SIZE, THUMBNAIL_LARGE_SIZE));
+    final encryptedThumbnailName =
+        file.generatedID.toString() + "_thumbnail.aes";
+    final encryptedThumbnailPath =
+        _encryptedFilesDirectory + encryptedThumbnailName;
+    final thumbnailIV =
+        CryptoUtil.getBase64EncodedSecureRandomString(length: 16);
+    await CryptoUtil.encryptData(
+        thumbnailData, encryptedThumbnailPath, key, thumbnailIV);
+
     final metadata = jsonEncode(file.getMetadata());
     final metadata = jsonEncode(file.getMetadata());
     final metadataIV =
     final metadataIV =
         CryptoUtil.getBase64EncodedSecureRandomString(length: 16);
         CryptoUtil.getBase64EncodedSecureRandomString(length: 16);
@@ -260,6 +273,9 @@ class PhotoSyncManager {
       "file": MultipartFile.fromFileSync(encryptedFilePath,
       "file": MultipartFile.fromFileSync(encryptedFilePath,
           filename: encryptedFileName),
           filename: encryptedFileName),
       "fileIV": fileIV,
       "fileIV": fileIV,
+      "thumbnail": MultipartFile.fromFileSync(encryptedThumbnailPath,
+          filename: encryptedThumbnailName),
+      "thumbnailIV": thumbnailIV,
       "metadata": encryptedMetadata,
       "metadata": encryptedMetadata,
       "metadataIV": metadataIV,
       "metadataIV": metadataIV,
     });
     });

+ 9 - 1
lib/utils/crypto_util.dart

@@ -19,7 +19,6 @@ class CryptoUtil {
     return encrypter.decrypt(Encrypted.fromBase64(cipherText));
     return encrypter.decrypt(Encrypted.fromBase64(cipherText));
   }
   }
 
 
-  // Encrypts a file and returns the IV that was used
   static Future<void> encryptFile(String sourcePath, String destinationPath,
   static Future<void> encryptFile(String sourcePath, String destinationPath,
       String base64Key, String base64IV) async {
       String base64Key, String base64IV) async {
     final encrypter = AesCrypt("hello");
     final encrypter = AesCrypt("hello");
@@ -28,4 +27,13 @@ class CryptoUtil {
     encrypter.setOverwriteMode(AesCryptOwMode.on);
     encrypter.setOverwriteMode(AesCryptOwMode.on);
     await encrypter.encryptFile(sourcePath, destinationPath);
     await encrypter.encryptFile(sourcePath, destinationPath);
   }
   }
+
+  static Future<void> encryptData(Uint8List source, String destinationPath,
+      String base64Key, String base64IV) async {
+    final encrypter = AesCrypt("hello");
+    encrypter.aesSetParams(Key.fromBase64(base64Key).bytes,
+        IV.fromBase64(base64IV).bytes, AesMode.cbc);
+    encrypter.setOverwriteMode(AesCryptOwMode.on);
+    await encrypter.encryptDataToFile(source, destinationPath);
+  }
 }
 }