Add object detection, tracking, mobile notifications, and search to any security camera.
open aes files
roryclearaff28e466db8
diff --git a/clearcam/Info.plist b/clearcam/Info.plistindex 5a5a392..12fbb02 100644--- a/clearcam/Info.plist+++ b/clearcam/Info.plist@@ -6,14 +6,14 @@<array><dict><key>CFBundleTypeName</key>- <string>PGP File</string>+ <string>AES File</string><key>LSItemContentTypes</key><array>- <string>com.clearcam.pgpfile</string>+ <string>com.clearcam.aesfile</string></array><key>CFBundleTypeExtensions</key><array>- <string>pgp</string>+ <string>aes</string></array><key>LSHandlerRank</key><string>Owner</string> <!-- Makes app the default handler -->@@ -24,9 +24,9 @@<array><dict><key>UTTypeIdentifier</key>- <string>com.clearcam.pgpfile</string>+ <string>com.clearcam.aesfile</string><key>UTTypeDescription</key>- <string>PGP Encrypted File</string>+ <string>AES Encrypted File</string><key>UTTypeConformsTo</key><array><string>public.data</string>@@ -35,7 +35,7 @@<dict><key>public.filename-extension</key><array>- <string>pgp</string>+ <string>aes</string></array></dict></dict>diff --git a/clearcam/SceneDelegate.m b/clearcam/SceneDelegate.mindex 710fa7a..e09ad71 100644--- a/clearcam/SceneDelegate.m+++ b/clearcam/SceneDelegate.m@@ -1,11 +1,13 @@#import "SceneDelegate.h"#import "ViewController.h" // Import your main view controller#import <UIKit/UIKit.h>+#import <CommonCrypto/CommonCryptor.h>@interface SceneDelegate () <UIDocumentInteractionControllerDelegate>@property (strong, nonatomic) UIDocumentInteractionController *docController;@property (strong, nonatomic) NSURL *pendingURL; // Store the URL if the app is launched from scratch+@property (strong, nonatomic) NSMetadataQuery *iCloudQuery; // For monitoring iCloud downloads@end@@ -21,14 +23,252 @@self.window.rootViewController = navigationController;[self.window makeKeyAndVisible];+ // Handle URL contexts if the app is launched with a fileif (connectionOptions.URLContexts.count > 0) {- NSURL *url = connectionOptions.URLContexts.anyObject.URL;+ UIOpenURLContext *context = connectionOptions.URLContexts.anyObject;+ NSURL *url = context.URL;+ NSLog(@"App launched with URL: %@", url);+ [self handleURL:url];}}- (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts {// Handle the URL when the app is already running- NSURL *url = URLContexts.anyObject.URL;+ UIOpenURLContext *context = URLContexts.anyObject;+ NSURL *url = context.URL;+ NSLog(@"App opened URL: %@", url);+ [self handleURL:url];+}++- (void)handleURL:(NSURL *)url {+ // Handle security-scoped URL+ BOOL isSecurityScoped = [url startAccessingSecurityScopedResource];+ if (isSecurityScoped) {+ NSLog(@"Started accessing security-scoped resource.");+ }++ // Handle the .aes file+ [self handleAESFile:url];++ // Stop accessing security-scoped resource+ if (isSecurityScoped) {+ [url stopAccessingSecurityScopedResource];+ NSLog(@"Stopped accessing security-scoped resource.");+ }+}++- (NSData *)decryptData:(NSData *)encryptedData withKey:(NSString *)key {+ char keyPtr[kCCKeySizeAES256 + 1]; // Buffer for key+ bzero(keyPtr, sizeof(keyPtr)); // Zero out buffer+ [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];++ size_t bufferSize = encryptedData.length + kCCBlockSizeAES128;+ void *buffer = malloc(bufferSize);++ size_t numBytesDecrypted = 0;+ CCCryptorStatus status = CCCrypt(kCCDecrypt,+ kCCAlgorithmAES,+ kCCOptionPKCS7Padding,+ keyPtr,+ kCCKeySizeAES256,+ NULL, // IV (NULL for no IV)+ encryptedData.bytes,+ encryptedData.length,+ buffer,+ bufferSize,+ &numBytesDecrypted);++ if (status == kCCSuccess) {+ return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];+ }++ free(buffer);+ return nil;+}++- (void)handleAESFile:(NSURL *)aesFileURL {+ NSError *error = nil; // Declare error as a local variable+ NSFileManager *fileManager = [NSFileManager defaultManager];++ // Log the file path for debugging+ NSLog(@"Attempting to access file at path: %@", aesFileURL.path);++ // Check if the file is an iCloud file+ BOOL isUbiquitous = [fileManager isUbiquitousItemAtURL:aesFileURL];+ if (isUbiquitous) {+ NSLog(@"File is an iCloud file.");++ // Check the download status+ NSString *downloadStatus = nil;+ NSError *downloadError = nil; // Temporary error variable for getResourceValue+ [aesFileURL getResourceValue:&downloadStatus forKey:NSURLUbiquitousItemDownloadingStatusKey error:&downloadError];+ if (downloadError) {+ NSLog(@"Error checking iCloud download status: %@", downloadError.localizedDescription);+ [self showErrorAlertWithMessage:[NSString stringWithFormat:@"Failed to check iCloud status: %@", downloadError.localizedDescription]];+ return;+ }++ NSLog(@"iCloud download status: %@", downloadStatus);++ if (![downloadStatus isEqualToString:NSURLUbiquitousItemDownloadingStatusCurrent]) {+ // File is not downloaded locally, initiate download+ NSLog(@"File is not downloaded locally. Starting download...");+ NSError *downloadStartError = nil; // Temporary error variable for startDownloading+ [fileManager startDownloadingUbiquitousItemAtURL:aesFileURL error:&downloadStartError];+ if (downloadStartError) {+ NSLog(@"Failed to start downloading iCloud file: %@", downloadStartError.localizedDescription);+ [self showErrorAlertWithMessage:[NSString stringWithFormat:@"Failed to start download: %@", downloadStartError.localizedDescription]];+ return;+ }+ NSLog(@"Started downloading iCloud file. Monitoring download progress...");+ [self monitorICloudDownloadForURL:aesFileURL];+ [self showDownloadAlert];+ return;+ }+ } else {+ // Check if the file exists locally (for non-iCloud files)+ if (![fileManager fileExistsAtPath:aesFileURL.path]) {+ NSLog(@"File does not exist at path: %@", aesFileURL.path);+ [self showErrorAlertWithMessage:@"The file does not exist or is not accessible."];+ return;+ }+ }++ // Use NSFileCoordinator to safely read the file+ NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];+ NSError *coordinationError = nil; // Temporary error variable for coordination+ [fileCoordinator coordinateReadingItemAtURL:aesFileURL+ options:0+ error:&coordinationError+ byAccessor:^(NSURL *newURL) {+ // Read the .aes file data securely+ NSError *readError = nil; // Temporary error variable for reading+ NSData *encryptedData = [NSData dataWithContentsOfURL:newURL options:0 error:&readError];+ if (!encryptedData) {+ NSLog(@"Failed to read .aes file: %@", readError.localizedDescription);+ [self showErrorAlertWithMessage:[NSString stringWithFormat:@"Failed to read the file: %@", readError.localizedDescription]];+ return;+ }+ NSLog(@"Read .aes file successfully. Size: %lu bytes", (unsigned long)encryptedData.length);++ // Decrypt the data+ NSString *key = @"opensesame"; // Hardcoded key (insecure, consider using Keychain)+ NSData *decryptedData = [self decryptData:encryptedData withKey:key];+ if (!decryptedData) {+ NSLog(@"Failed to decrypt data.");+ [self showErrorAlertWithMessage:@"Failed to decrypt the file. The key or file may be incorrect."];+ return;+ }+ NSLog(@"Decrypted data successfully. Size: %lu bytes", (unsigned long)decryptedData.length);++ // Save the decrypted data as a .jpg file+ NSString *fileName = [[aesFileURL lastPathComponent] stringByDeletingPathExtension]; // Remove .aes extension+ NSString *jpgFileName = [fileName stringByAppendingPathExtension:@"jpg"];+ NSURL *jpgFileURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0] URLByAppendingPathComponent:jpgFileName];++ NSError *writeError = nil; // Temporary error variable for writing+ [decryptedData writeToURL:jpgFileURL options:NSDataWritingAtomic error:&writeError];+ if (writeError) {+ NSLog(@"Failed to save .jpg file: %@", writeError.localizedDescription);+ [self showErrorAlertWithMessage:[NSString stringWithFormat:@"Failed to save the decrypted file: %@", writeError.localizedDescription]];+ return;+ }++ NSLog(@"Decrypted and saved .jpg file: %@", jpgFileURL);++ // Open the .jpg file in the system's photo viewer+ [self openImageInPhotoViewer:jpgFileURL];+ }];++ if (coordinationError) {+ NSLog(@"File coordination failed: %@", coordinationError.localizedDescription);+ [self showErrorAlertWithMessage:[NSString stringWithFormat:@"File access failed: %@", coordinationError.localizedDescription]];+ }+}++- (void)openImageInPhotoViewer:(NSURL *)imageURL {+ self.docController = [UIDocumentInteractionController interactionControllerWithURL:imageURL];+ self.docController.delegate = self;+ [self.docController presentPreviewAnimated:YES];+}++- (void)monitorICloudDownloadForURL:(NSURL *)aesFileURL {+ // Stop any existing query+ if (self.iCloudQuery) {+ [self.iCloudQuery stopQuery];+ [[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidUpdateNotification object:self.iCloudQuery];+ [[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:self.iCloudQuery];+ }++ // Create a new query+ self.iCloudQuery = [[NSMetadataQuery alloc] init];+ self.iCloudQuery.searchScopes = @[NSMetadataQueryUbiquitousDocumentsScope];+ self.iCloudQuery.predicate = [NSPredicate predicateWithFormat:@"%K == %@", NSMetadataItemURLKey, aesFileURL];++ // Observe query updates+ [[NSNotificationCenter defaultCenter] addObserver:self+ selector:@selector(queryDidUpdate:)+ name:NSMetadataQueryDidUpdateNotification+ object:self.iCloudQuery];++ [[NSNotificationCenter defaultCenter] addObserver:self+ selector:@selector(queryDidFinish:)+ name:NSMetadataQueryDidFinishGatheringNotification+ object:self.iCloudQuery];++ [self.iCloudQuery startQuery];+}++- (void)queryDidUpdate:(NSNotification *)notification {+ NSMetadataQuery *query = notification.object;+ [query enumerateResultsUsingBlock:^(NSMetadataItem *result, NSUInteger idx, BOOL *stop) {+ NSString *downloadStatus = [result valueForAttribute:NSMetadataUbiquitousItemDownloadingStatusKey];+ NSLog(@"Download status updated: %@", downloadStatus);++ if ([downloadStatus isEqualToString:NSURLUbiquitousItemDownloadingStatusCurrent]) {+ // File is downloaded, stop the query and handle the file+ [query stopQuery];+ [[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidUpdateNotification object:query];+ [[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:query];+ self.iCloudQuery = nil;+ [self handleAESFile:[result valueForAttribute:NSMetadataItemURLKey]];+ }+ }];+}++- (void)queryDidFinish:(NSNotification *)notification {+ NSMetadataQuery *query = notification.object;+ [query enumerateResultsUsingBlock:^(NSMetadataItem *result, NSUInteger idx, BOOL *stop) {+ NSString *downloadStatus = [result valueForAttribute:NSMetadataUbiquitousItemDownloadingStatusKey];+ NSLog(@"Download status finished: %@", downloadStatus);++ if ([downloadStatus isEqualToString:NSURLUbiquitousItemDownloadingStatusCurrent]) {+ // File is downloaded, stop the query and handle the file+ [query stopQuery];+ [[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidUpdateNotification object:query];+ [[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:query];+ self.iCloudQuery = nil;+ [self handleAESFile:[result valueForAttribute:NSMetadataItemURLKey]];+ }+ }];+}++- (void)showDownloadAlert {+ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Downloading File"+ message:@"The file is being downloaded from iCloud. Please wait a moment and try again."+ preferredStyle:UIAlertControllerStyleAlert];+ UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];+ [alert addAction:okAction];+ [self.window.rootViewController presentViewController:alert animated:YES completion:nil];+}++- (void)showErrorAlertWithMessage:(NSString *)message {+ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Error"+ message:message+ preferredStyle:UIAlertControllerStyleAlert];+ UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];+ [alert addAction:okAction];+ [self.window.rootViewController presentViewController:alert animated:YES completion:nil];}#pragma mark - UIDocumentInteractionControllerDelegate@@ -40,8 +280,8 @@- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller {// This method is called when the user is done viewing the image- // You can add any cleanup or navigation logic here if neededNSLog(@"User is done viewing the image.");+ self.docController = nil; // Clear the controller}@enddiff --git a/clearcam/ViewController.m b/clearcam/ViewController.mindex b4ebaf9..2749bea 100644--- a/clearcam/ViewController.m+++ b/clearcam/ViewController.m@@ -49,7 +49,6 @@ NSMutableDictionary *classColorMap;- (void)viewDidLoad {[super viewDidLoad];self.recordPressed = NO;- //PGP *pgp = [[PGP alloc] init];self.scene = [[SceneState alloc] init];self.segmentQueue = dispatch_queue_create("com.example.segmentQueue", DISPATCH_QUEUE_SERIAL);self.current_segment_squares = [[NSMutableArray alloc] init];