2222
2323import com .termux .api .TermuxApiReceiver ;
2424import com .termux .api .util .ResultReturner ;
25+ import com .termux .shared .errors .Error ;
26+ import com .termux .shared .file .FileUtils ;
2527import com .termux .shared .logger .Logger ;
28+ import com .termux .shared .termux .file .TermuxFileUtils ;
2629
2730import java .io .File ;
2831import java .io .FileOutputStream ;
@@ -43,16 +46,32 @@ public static void onReceive(TermuxApiReceiver apiReceiver, final Context contex
4346 Logger .logDebug (LOG_TAG , "onReceive" );
4447
4548 final String filePath = intent .getStringExtra ("file" );
46- final File outputFile = new File (filePath );
47- final File outputDir = outputFile .getParentFile ();
4849 final String cameraId = Objects .toString (intent .getStringExtra ("camera" ), "0" );
4950
5051 ResultReturner .returnData (apiReceiver , intent , stdout -> {
51- if (!(outputDir .isDirectory () || outputDir .mkdirs ())) {
52- stdout .println ("Not a folder (and unable to create it): " + outputDir .getAbsolutePath ());
53- } else {
54- takePicture (stdout , context , outputFile , cameraId );
52+ if (filePath == null || filePath .isEmpty ()) {
53+ stdout .println ("ERROR: " + "File path not passed" );
54+ return ;
5555 }
56+
57+ // Get canonical path of filePath
58+ String photoFilePath = TermuxFileUtils .getCanonicalPath (filePath , null , true );
59+ String photoDirPath = FileUtils .getFileBasename (photoFilePath );
60+
61+ // If workingDirectory is not a directory, or is not readable or writable, then just return
62+ // Creation of missing directory and setting of read, write and execute permissions are only done if workingDirectory is
63+ // under allowed termux working directory paths.
64+ // We try to set execute permissions, but ignore if they are missing, since only read and write permissions are required
65+ // for working directories.
66+ Error error = TermuxFileUtils .validateDirectoryFileExistenceAndPermissions ("photo directory" , photoDirPath ,
67+ true , true , true ,
68+ false , true );
69+ if (error != null ) {
70+ stdout .println ("ERROR: " + error .getErrorLogString ());
71+ return ;
72+ }
73+
74+ takePicture (stdout , context , new File (photoFilePath ), cameraId );
5675 });
5776 }
5877
0 commit comments