Skip to content

Add object detection, tracking, mobile notifications, and search to any security camera.

upload as get req, send data once

rorycleard362a54cee87
diff --git a/clearcam/Notification.m b/clearcam/Notification.m
index 13608c9..360ae6c 100644
--- a/clearcam/Notification.m
+++ b/clearcam/Notification.m
@@ -58,72 +58,42 @@
}
}
- NSString *endpoint = @"/upload";
- NSData *imageData = nil;
- NSString *filePathToSend = imagePath;
-
- // Generate a blank image if no path is provided
- if (imagePath.length == 0) {
- CGSize imageSize = CGSizeMake(1280, 720);
- UIGraphicsBeginImageContext(imageSize);
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
- CGContextFillRect(context, CGRectMake(0, 0, imageSize.width, imageSize.height));
- UIImage *blankImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
-
- imageData = UIImageJPEGRepresentation(blankImage, 1.0);
- NSString *tempPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"test_blank_img.jpg"];
- [imageData writeToFile:tempPath atomically:YES];
- filePathToSend = tempPath;
- } else {
- imageData = [NSData dataWithContentsOfFile:imagePath];
- }
+ if (imagePath.length == 0) return;
+ NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
if (!imageData) return;
- NSData *fileData = imageData;
+ NSString *fileName = [imagePath lastPathComponent];
BOOL encryptImage = ![[NSUserDefaults standardUserDefaults] boolForKey:@"use_own_server_enabled"];
if (encryptImage) {
NSString *encryptionKey = [[SecretManager sharedManager] getEncryptionKey];
if (!encryptionKey) return;
- fileData = [[SecretManager sharedManager] encryptData:imageData withKey:encryptionKey];
- if (!fileData) return;
- filePathToSend = [filePathToSend stringByAppendingString:@".aes"];
+ NSData *encryptedData = [[SecretManager sharedManager] encryptData:imageData withKey:encryptionKey];
+ if (!encryptedData) return;
+ fileName = [fileName stringByAppendingString:@".aes"];
+ imageData = encryptedData;
}
- NSString *boundary = [NSString stringWithFormat:@"Boundary-%@", [[NSUUID UUID] UUIDString]];
- NSMutableData *bodyData = [NSMutableData data];
-
- // Add "file" field (image)
- [bodyData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
- [bodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"\r\n", [filePathToSend lastPathComponent]] dataUsingEncoding:NSUTF8StringEncoding]];
- [bodyData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
- [bodyData appendData:fileData];
- [bodyData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
-
- // Include session_token if not using own notification server
- if (![[NSUserDefaults standardUserDefaults] boolForKey:@"use_own_server_enabled"]) {
- NSString *sessionToken = [[StoreManager sharedInstance] retrieveSessionTokenFromKeychain];
- if (sessionToken) {
- [bodyData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
- [bodyData appendData:[@"Content-Disposition: form-data; name=\"session_token\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
- [bodyData appendData:[sessionToken dataUsingEncoding:NSUTF8StringEncoding]];
- [bodyData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
- }
+ // Retrieve session token
+ NSString *sessionToken = [[StoreManager sharedInstance] retrieveSessionTokenFromKeychain];
+ if (!sessionToken) {
+ NSLog(@"❌ No session token available");
+ return;
}
- [bodyData appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
- // πŸ”Ή STEP 1: Ask your server for a presigned R2 URL
- NSURL *uploadRequestURL = [NSURL URLWithString:[server stringByAppendingString:endpoint]];
+ // πŸ”Ή STEP 1: Request a Presigned R2 URL from the Server
+ NSURLComponents *components = [NSURLComponents componentsWithString:[server stringByAppendingString:@"/upload"]];
+ NSURLQueryItem *fileItem = [NSURLQueryItem queryItemWithName:@"filename" value:fileName];
+ NSURLQueryItem *sessionItem = [NSURLQueryItem queryItemWithName:@"session_token" value:sessionToken];
+ components.queryItems = @[fileItem, sessionItem]; // Include session_token here
+
+ NSURL *uploadRequestURL = components.URL;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:uploadRequestURL];
- [request setHTTPMethod:@"POST"];
- [request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];
- [request setHTTPBody:bodyData];
+ [request setHTTPMethod:@"GET"];
NSURLSessionDataTask *uploadTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
- NSLog(@"❌ Server upload failed: %@", error.localizedDescription);
+ NSLog(@"❌ Server request failed: %@", error.localizedDescription);
return;
}
@@ -136,12 +106,12 @@
NSString *presignedR2URL = responseDict[@"url"]; // πŸ”₯ URL for R2 upload
- // πŸ”Ή STEP 2: Upload the same file to Cloudflare R2
+ // πŸ”Ή STEP 2: Upload the File to Cloudflare R2
NSMutableURLRequest *r2Request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:presignedR2URL]];
[r2Request setHTTPMethod:@"PUT"];
[r2Request setValue:@"application/octet-stream" forHTTPHeaderField:@"Content-Type"];
- NSURLSessionUploadTask *r2UploadTask = [[NSURLSession sharedSession] uploadTaskWithRequest:r2Request fromData:fileData completionHandler:^(NSData *r2Data, NSURLResponse *r2Response, NSError *r2Error) {
+ NSURLSessionUploadTask *r2UploadTask = [[NSURLSession sharedSession] uploadTaskWithRequest:r2Request fromData:imageData completionHandler:^(NSData *r2Data, NSURLResponse *r2Response, NSError *r2Error) {
if (r2Error) {
NSLog(@"❌ R2 upload failed: %@", r2Error.localizedDescription);
} else {
@@ -157,8 +127,10 @@
[r2UploadTask resume]; // Start the R2 upload
}];
- [uploadTask resume]; // Start the initial server upload request
+ [uploadTask resume]; // Start the request to get the presigned URL
}
+
+
@end