GitToTheHub commented on code in PR #942:
URL: 
https://github.com/apache/cordova-plugin-camera/pull/942#discussion_r2724750944


##########
src/ios/CDVCamera.m:
##########
@@ -865,21 +804,116 @@ - (void)resultForImage:(CDVPictureOptions*)options
 - (CDVPluginResult*)resultForVideo:(NSDictionary*)info
 {
     NSString* moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] 
absoluteString];
+    
     // On iOS 13 the movie path becomes inaccessible, create and return a copy
     if (IsAtLeastiOSVersion(@"13.0")) {
-        moviePath = [self createTmpVideo:[[info 
objectForKey:UIImagePickerControllerMediaURL] path]];
+        moviePath = [self copyFileToTemp:[[info 
objectForKey:UIImagePickerControllerMediaURL] path]];
     }
+    
     return [CDVPluginResult resultWithStatus:CDVCommandStatus_OK 
messageAsString:moviePath];
 }
 
-- (NSString*)createTmpVideo:(NSString*)moviePath
+/**
+ Generates a unique temporary file path for a file extension.
+ 
+ The filename is prefixed with `cdv_photo_` and suffixed with the provided
+ file extension. A UNIX timestamp (seconds since 1970) is used to ensure
+ uniqueness between calls.
+ 
+ Threading: Safe to call from any thread. Uses NSTemporaryDirectory() and
+ does not perform any I/O; it only constructs a path string.
+ 
+ @param fileExtension  The desired file extension without a leading dot
+                      (for example, "jpg", "png", or the original video
+                      extension like "mov").
+ 
+ @return An absolute path string within the app's temporary directory,
+         e.g. 
`/var/mobile/Containers/Data/Application/<UUID>/tmp/cdv_photo_<timestamp>.jpg`.
+ 
+ @discussion The returned path is not created on disk. Callers are responsible
+             for writing data to the path and handling any errors.
+ 
+ @note Only files whose names start with `cdv_photo_` are cleaned up by the
+       plugin's `cleanup:` method.
+ **/
+- (NSString*)tempFilePathForExtension:(NSString*)fileExtension
+{
+    // Return a unique file name like
+    // 
`/var/mobile/Containers/Data/Application/<UUID>/tmp/cdv_photo_<timestamp>.jpg`.
+    return [NSString stringWithFormat:
+            @"%@/%@%lld.%@",
+            [NSTemporaryDirectory() stringByStandardizingPath],
+            CDV_PHOTO_PREFIX,
+            (long long)[[NSDate date] timeIntervalSince1970],
+            fileExtension];
+}
+
+- (NSString*)copyFileToTemp:(NSString*)filePath
+{
+    NSFileManager* fileManager = [[NSFileManager alloc] init];
+    NSString* tempFilePath = [self tempFilePathForExtension:[filePath 
pathExtension]];
+    NSError *error = nil;
+    
+    // Copy file to temp directory
+    BOOL copySuccess = [fileManager copyItemAtPath:filePath 
toPath:tempFilePath error:&error];
+    
+    if (!copySuccess || error) {
+        NSLog(@"CDVCamera: Failed to copy file from %@ to temporary path %@. 
Error: %@", filePath, tempFilePath, [error localizedDescription]);
+        return nil;
+    }
+    
+    // Verify the copied file exists
+    if (![fileManager fileExistsAtPath:tempFilePath]) {
+        NSLog(@"CDVCamera: Copied file does not exist at temporary path: %@", 
tempFilePath);
+        return nil;
+    }
+    
+    return [[NSURL fileURLWithPath:tempFilePath] absoluteString];
+}
+
+/**
+  Called by JS camera.cleanup()
+  Removes intermediate image files that are kept in temporary storage after
+  calling camera.getPicture.
+*/
+- (void)cleanup:(CDVInvokedUrlCommand*)command
 {
-    NSString* moviePathExtension = [moviePath pathExtension];
-    NSString* copyMoviePath = [self tempFilePath:moviePathExtension];
+    // empty the tmp directory
     NSFileManager* fileMgr = [[NSFileManager alloc] init];
-    NSError *error;
-    [fileMgr copyItemAtPath:moviePath toPath:copyMoviePath error:&error];
-    return [[NSURL fileURLWithPath:copyMoviePath] absoluteString];
+    NSError* err = nil;
+    BOOL hasErrors = NO;
+
+    // clear contents of NSTemporaryDirectory

Review Comment:
   I refactored the method to make it more readable and clear



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to