这是indexloc提供的服务,不要输入任何密码
Skip to content

Commit cc186d0

Browse files
Changed|Fixed: Fix potential NullPointerException in PhotoAPI and use FileUtils for photo directory creation
1 parent 237d36a commit cc186d0

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

app/src/main/java/com/termux/api/apis/PhotoAPI.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222

2323
import com.termux.api.TermuxApiReceiver;
2424
import com.termux.api.util.ResultReturner;
25+
import com.termux.shared.errors.Error;
26+
import com.termux.shared.file.FileUtils;
2527
import com.termux.shared.logger.Logger;
28+
import com.termux.shared.termux.file.TermuxFileUtils;
2629

2730
import java.io.File;
2831
import 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

Comments
 (0)