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); attempt.put("success", res.success);
if (res.success) { if (res.success) {
attempt.put("url", res.url); attempt.put("url", res.url);
attempts.add(attempt);
resolveWithSuccess(call, res.url, getFileName(fileUri), provider, attempts); resolveWithSuccess(call, res.url, getFileName(fileUri), provider, attempts);
return; return;
} }
@@ -144,6 +145,7 @@ public class FileUploaderPlugin extends Plugin {
attempt.put("success", res.success); attempt.put("success", res.success);
if (res.success) { if (res.success) {
attempt.put("url", res.url); attempt.put("url", res.url);
attempts.add(attempt);
resolveWithSuccess(call, res.url, getFileName(fileUri), provider, attempts); resolveWithSuccess(call, res.url, getFileName(fileUri), provider, attempts);
return; return;
} }
@@ -226,6 +228,9 @@ public class FileUploaderPlugin extends Plugin {
return new MediaUploadResult( return new MediaUploadResult(
false, null, "Unexpected response " + response.code()); false, null, "Unexpected response " + response.code());
} }
if (response.body() == null) {
return new MediaUploadResult(false, null, "Empty response body");
}
String url = response.body().string(); String url = response.body().string();
Log.d(TAG, "Catbox upload successful. URL: " + url); Log.d(TAG, "Catbox upload successful. URL: " + url);
return new MediaUploadResult(true, url.trim(), null); return new MediaUploadResult(true, url.trim(), null);
@@ -14,21 +14,25 @@ public class FileUtils {
String fileName = getFileName(context, uri); String fileName = getFileName(context, uri);
File file = new File(context.getCacheDir(), fileName); File file = new File(context.getCacheDir(), fileName);
try (InputStream inputStream = context.getContentResolver().openInputStream(uri); try (InputStream inputStream = context.getContentResolver().openInputStream(uri)) {
FileOutputStream outputStream = new FileOutputStream(file)) { 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]; byte[] buffer = new byte[4096];
int length; int length;
while ((length = inputStream.read(buffer)) > 0) { while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length); outputStream.write(buffer, 0, length);
} }
outputStream.flush(); outputStream.flush();
}
return file; return file;
} }
} }
private static String getFileName(Context context, Uri uri) { private static String getFileName(Context context, Uri uri) {
String result = null; String result = null;
if (uri.getScheme().equals("content")) { if ("content".equals(uri.getScheme())) {
try (Cursor cursor = context.getContentResolver().query(uri, null, null, null, null)) { try (Cursor cursor = context.getContentResolver().query(uri, null, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); int columnIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
@@ -39,10 +43,12 @@ public class FileUtils {
} }
} }
if (result == null) { if (result == null) {
result = uri.getPath(); String path = uri.getPath();
int cut = result.lastIndexOf('/'); if (path != null) {
if (cut != -1) { int cut = path.lastIndexOf('/');
result = result.substring(cut + 1); result = (cut != -1) ? path.substring(cut + 1) : path;
} else {
result = "unknown";
} }
} }
return result; return result;