fix(android): guard against NPEs in FileUtils and FileUploaderPlugin

This commit is contained in:
plebeius
2026-02-22 16:11:34 +08:00
parent 4647e6a090
commit bdfee1b12d
2 changed files with 23 additions and 12 deletions
@@ -133,6 +133,7 @@ public class FileUploaderPlugin extends Plugin {
attempt.put("success", res.success);
if (res.success) {
attempt.put("url", res.url);
attempts.add(attempt);
resolveWithSuccess(call, res.url, getFileName(fileUri), provider, attempts);
return;
}
@@ -144,6 +145,7 @@ public class FileUploaderPlugin extends Plugin {
attempt.put("success", res.success);
if (res.success) {
attempt.put("url", res.url);
attempts.add(attempt);
resolveWithSuccess(call, res.url, getFileName(fileUri), provider, attempts);
return;
}
@@ -226,6 +228,9 @@ public class FileUploaderPlugin extends Plugin {
return new MediaUploadResult(
false, null, "Unexpected response " + response.code());
}
if (response.body() == null) {
return new MediaUploadResult(false, null, "Empty response body");
}
String url = response.body().string();
Log.d(TAG, "Catbox upload successful. URL: " + url);
return new MediaUploadResult(true, url.trim(), null);
@@ -14,21 +14,25 @@ public class FileUtils {
String fileName = getFileName(context, uri);
File file = new File(context.getCacheDir(), fileName);
try (InputStream inputStream = context.getContentResolver().openInputStream(uri);
FileOutputStream outputStream = new FileOutputStream(file)) {
byte[] buffer = new byte[4096];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
try (InputStream inputStream = context.getContentResolver().openInputStream(uri)) {
if (inputStream == null) {
throw new java.io.IOException("Unable to open input stream for URI: " + uri);
}
try (FileOutputStream outputStream = new FileOutputStream(file)) {
byte[] buffer = new byte[4096];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.flush();
}
outputStream.flush();
return file;
}
}
private static String getFileName(Context context, Uri uri) {
String result = null;
if (uri.getScheme().equals("content")) {
if ("content".equals(uri.getScheme())) {
try (Cursor cursor = context.getContentResolver().query(uri, null, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
@@ -39,10 +43,12 @@ public class FileUtils {
}
}
if (result == null) {
result = uri.getPath();
int cut = result.lastIndexOf('/');
if (cut != -1) {
result = result.substring(cut + 1);
String path = uri.getPath();
if (path != null) {
int cut = path.lastIndexOf('/');
result = (cut != -1) ? path.substring(cut + 1) : path;
} else {
result = "unknown";
}
}
return result;