diff options
author | Stefan Niedermann <info@niedermann.it> | 2020-06-23 18:45:03 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2020-06-23 18:45:03 +0300 |
commit | 5f721cc9c574127126a6253d51aff505ab1f646d (patch) | |
tree | 55eecb3a2fdb281c9bc97c1e8327c6f4253c8e1a /app/src/main/java/it/niedermann/nextcloud/deck/util | |
parent | 4033dd7f178f438062b5b23bf4b854c679fadb5d (diff) |
Improved logging and exception handling for attachment uploads
Diffstat (limited to 'app/src/main/java/it/niedermann/nextcloud/deck/util')
3 files changed, 58 insertions, 59 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/util/AttachmentUtil.java b/app/src/main/java/it/niedermann/nextcloud/deck/util/AttachmentUtil.java index 6d8280bec..6b2d6925f 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/util/AttachmentUtil.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/util/AttachmentUtil.java @@ -26,7 +26,7 @@ public class AttachmentUtil { return accountUrl + "/index.php/apps/deck/cards/" + cardRemoteId + "/attachment/" + attachmentRemoteId; } - public static File copyContentUriToTempFile(@NonNull Context context, @NonNull Uri currentUri, long accountId, Long localId) throws IOException { + public static File copyContentUriToTempFile(@NonNull Context context, @NonNull Uri currentUri, long accountId, Long localId) throws IOException, IllegalArgumentException { String fullTempPath = context.getApplicationContext().getFilesDir().getAbsolutePath() + "/attachments/account-" + accountId + "/card-" + (localId == null ? "pending-creation" : localId) + '/' + UriUtils.getDisplayNameForUri(currentUri, context); DeckLog.verbose("----- fullTempPath: " + fullTempPath); InputStream inputStream = context.getContentResolver().openInputStream(currentUri); diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/util/MimeTypeUtil.java b/app/src/main/java/it/niedermann/nextcloud/deck/util/MimeTypeUtil.java index 79b227b0f..9a13cf5e5 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/util/MimeTypeUtil.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/util/MimeTypeUtil.java @@ -3,6 +3,10 @@ package it.niedermann.nextcloud.deck.util; import java.util.Locale; public class MimeTypeUtil { + + public static final String TEXT_PLAIN = "text/plain"; + public static final String APPLICATION_OCTET_STREAM = "application/octet-stream"; + /** * @return 'True' if the mime type defines image */ @@ -28,7 +32,7 @@ public class MimeTypeUtil { /** * @return 'True' if mime type defines text */ - private static boolean isText(String mimeType) { + public static boolean isText(String mimeType) { return mimeType != null && mimeType.toLowerCase(Locale.ROOT).startsWith("text/"); } } diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/util/UriUtils.java b/app/src/main/java/it/niedermann/nextcloud/deck/util/UriUtils.java index 9b0444560..692058c49 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/util/UriUtils.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/util/UriUtils.java @@ -5,7 +5,6 @@ import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.MediaStore; -import android.util.Log; import android.webkit.MimeTypeMap; import androidx.annotation.NonNull; @@ -13,47 +12,43 @@ import androidx.annotation.NonNull; import java.util.Locale; /** - * A helper class for some Uri operations. + * A helper class for some {@link Uri} operations. */ public final class UriUtils { - private static final String TAG = UriUtils.class.getSimpleName(); - private UriUtils() { // utility class -> private constructor } @NonNull - public static String getDisplayNameForUri(@NonNull Uri uri, @NonNull Context context) { + public static String getDisplayNameForUri(@NonNull Uri uri, @NonNull Context context) throws IllegalArgumentException { String displayName; if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { - displayName = uri.getLastPathSegment(); // ready to return - - } else { - // content: URI - - displayName = getDisplayNameFromContentResolver(uri, context); - + displayName = uri.getLastPathSegment(); // ready to return + if (displayName == null) { + throw new IllegalArgumentException("Given uri is no content uri, but path is null. [" + uri + "]"); + } + } else { // content:// URI try { - if (displayName == null) { - // last chance to have a name - displayName = uri.getLastPathSegment().replaceAll("\\s", ""); - } - - // Add best possible extension - int index = displayName.lastIndexOf('.'); - if (index == -1 || MimeTypeMap.getSingleton(). - getMimeTypeFromExtension(displayName.substring(index + 1).toLowerCase(Locale.ROOT)) == null) { - String mimeType = context.getContentResolver().getType(uri); - String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType); - if (extension != null) { - displayName += "." + extension; - } + displayName = getDisplayNameFromContentResolver(uri, context); + } catch (IllegalArgumentException e) { + // last chance to have a name + String lastPathSegment = uri.getLastPathSegment(); + if (lastPathSegment == null) { + throw new IllegalArgumentException("Given uri is content uri, but path is null. [" + uri + "]", e); } + displayName = uri.getLastPathSegment().replaceAll("\\s", ""); + } + } - } catch (Exception e) { - Log.e(TAG, "No way to get a display name for " + uri.toString()); + // Add best possible extension + int index = displayName.lastIndexOf('.'); + if (index == -1 || MimeTypeMap.getSingleton().getMimeTypeFromExtension(displayName.substring(index + 1).toLowerCase(Locale.ROOT)) == null) { + String mimeType = context.getContentResolver().getType(uri); + String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType); + if (extension != null) { + displayName += "." + extension; } } @@ -61,40 +56,40 @@ public final class UriUtils { return displayName.replaceAll("/", "-"); } - private static String getDisplayNameFromContentResolver(Uri uri, Context context) { - String displayName = null; - String mimeType = context.getContentResolver().getType(uri); - if (mimeType != null) { - String displayNameColumn; - if (MimeTypeUtil.isImage(mimeType)) { - displayNameColumn = MediaStore.Images.ImageColumns.DISPLAY_NAME; + @NonNull + private static String getDisplayNameFromContentResolver(Uri uri, Context context) throws IllegalArgumentException { + final String displayName; - } else if (MimeTypeUtil.isVideo(mimeType)) { - displayNameColumn = MediaStore.Video.VideoColumns.DISPLAY_NAME; + final String mimeType = context.getContentResolver().getType(uri); + if (mimeType == null) { + throw new IllegalArgumentException("mimetype of given uri is null. [" + uri + "]"); + } - } else if (MimeTypeUtil.isAudio(mimeType)) { - displayNameColumn = MediaStore.Audio.AudioColumns.DISPLAY_NAME; - } else { - displayNameColumn = MediaStore.Files.FileColumns.DISPLAY_NAME; - } + final String displayNameColumn; - try (Cursor cursor = context.getContentResolver().query( - uri, - new String[]{displayNameColumn}, - null, - null, - null - )) { - if (cursor != null) { - cursor.moveToFirst(); - displayName = cursor.getString(cursor.getColumnIndex(displayNameColumn)); - } - - } catch (Exception e) { - Log.e(TAG, "Could not retrieve display name for " + uri.toString()); - // nothing else, displayName keeps null + if (MimeTypeUtil.isImage(mimeType)) { + displayNameColumn = MediaStore.Images.ImageColumns.DISPLAY_NAME; + } else if (MimeTypeUtil.isVideo(mimeType)) { + displayNameColumn = MediaStore.Video.VideoColumns.DISPLAY_NAME; + } else if (MimeTypeUtil.isAudio(mimeType)) { + displayNameColumn = MediaStore.Audio.AudioColumns.DISPLAY_NAME; + } else { + displayNameColumn = MediaStore.Files.FileColumns.DISPLAY_NAME; + } + try (Cursor cursor = context.getContentResolver().query( + uri, new String[]{displayNameColumn}, + null, null, null + )) { + if (cursor == null) { + throw new IllegalArgumentException("Cursor for " + ContentResolver.class.getSimpleName() + " query is null. [" + uri + "]"); + } + if (!cursor.moveToFirst()) { + throw new IllegalArgumentException("Cursor for " + ContentResolver.class.getSimpleName() + " query is empty. [" + uri + "]"); } + displayName = cursor.getString(cursor.getColumnIndex(displayNameColumn)); + } catch (Exception e) { + throw new IllegalArgumentException("Could not retrieve display name for " + uri.toString(), e); } return displayName; } |