mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
fix(android): guard against NPEs in FileUtils and FileUploaderPlugin
This commit is contained in:
@@ -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) {
|
||||||
byte[] buffer = new byte[4096];
|
throw new java.io.IOException("Unable to open input stream for URI: " + uri);
|
||||||
int length;
|
}
|
||||||
while ((length = inputStream.read(buffer)) > 0) {
|
try (FileOutputStream outputStream = new FileOutputStream(file)) {
|
||||||
outputStream.write(buffer, 0, length);
|
byte[] buffer = new byte[4096];
|
||||||
|
int length;
|
||||||
|
while ((length = inputStream.read(buffer)) > 0) {
|
||||||
|
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;
|
||||||
|
|||||||
Reference in New Issue
Block a user