decryptFile method
Implementation
@override
FutureOr<File> decryptFile({required File file, required String writeToDirectory, String moduleKey = defaultModuleKey}) async {
bool isFileExist = await file.exists();
if(!isFileExist){throw NUICryptoException(cause: NUICryptoError.FILE_TO_ENCRYPT_NOT_FOUND);}
// file/path/to/image.jpg.encrypted -> image.jpg.encrypted
String baseFilename = file.path.split('/').last;
// image.jpg.encrypted -> "encrypted"
String extension = baseFilename.split('.').last;
// image.jpg.encrypted -> ["image","jpg","encrypted"]
List<String> extensions = baseFilename.split('.');
// [image","jpg","encrypted"] -> ["image","jpg"]
extensions.removeLast();
// ["image","jpg"] -> image.jpg
String filename = extensions.join(".");
if(extension != 'encrypted'){throw NUICryptoException(cause: NUICryptoError.FILE_TO_ENCRYPT_NOT_FOUND);}
String writeTo = writeToDirectory + "${writeToDirectory.endsWith('/') ? "" : "/"}" + filename;
//Info to pass into isolate
if(!module.containsKey(moduleKey)){
throw NUICryptoException(cause: NUICryptoError.KEY_NOT_FOUND);
}
final sink = File(writeTo).openWrite();
try{
final streamData = file.openRead();
final decryptedStream = await _doDecryptionFile(streamData, module[moduleKey]!);
await sink.addStream(decryptedStream);
}on Exception catch(e){
logNUI("NUICryptography in Flutter Isolate", "Encryption Exception ${e.toString()}");
rethrow;
}finally {
sink.close();
return File(writeTo);
}
}