فهرست منبع

Fix wrong renaming of unkown file types
Fix #7962

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>

tobiasKaminsky 4 سال پیش
والد
کامیت
d4aaebe5ac

+ 19 - 20
src/main/java/com/owncloud/android/ui/helpers/UriUploader.java

@@ -30,7 +30,6 @@ import com.owncloud.android.operations.UploadFileOperation;
 import com.owncloud.android.ui.activity.FileActivity;
 import com.owncloud.android.ui.asynctasks.CopyAndUploadContentUrisTask;
 import com.owncloud.android.ui.fragment.TaskRetainerFragment;
-import com.owncloud.android.utils.DisplayUtils;
 import com.owncloud.android.utils.UriUtils;
 
 import java.util.ArrayList;
@@ -40,27 +39,27 @@ import androidx.fragment.app.FragmentManager;
 
 /**
  * This class examines URIs pointing to files to upload and then requests {@link FileUploader} to upload them.
- *
- * URIs with scheme file:// do not require any previous processing, their path is sent to {@link FileUploader}
- * to find the source file.
- *
- * URIs with scheme content:// are handling assuming that file is in private storage owned by a different app,
- * and that persistency permission is not granted. Due to this, contents of the file are temporary copied by
- * the OC app, and then passed {@link FileUploader}.
+ * <p>
+ * URIs with scheme file:// do not require any previous processing, their path is sent to {@link FileUploader} to find
+ * the source file.
+ * <p>
+ * URIs with scheme content:// are handling assuming that file is in private storage owned by a different app, and that
+ * persistence permission is not granted. Due to this, contents of the file are temporary copied by the OC app, and then
+ * passed {@link FileUploader}.
  */
 public class UriUploader {
 
     private final String TAG = UriUploader.class.getSimpleName();
 
-    private FileActivity mActivity;
-    private List<Parcelable> mUrisToUpload;
-    private CopyAndUploadContentUrisTask.OnCopyTmpFilesTaskListener mCopyTmpTaskListener;
+    private final FileActivity mActivity;
+    private final List<Parcelable> mUrisToUpload;
+    private final CopyAndUploadContentUrisTask.OnCopyTmpFilesTaskListener mCopyTmpTaskListener;
 
-    private int mBehaviour;
+    private final int mBehaviour;
 
-    private String mUploadPath;
-    private Account mAccount;
-    private boolean mShowWaitingDialog;
+    private final String mUploadPath;
+    private final Account mAccount;
+    private final boolean mShowWaitingDialog;
 
     private UriUploaderResultCode mCode = UriUploaderResultCode.OK;
 
@@ -102,6 +101,11 @@ public class UriUploader {
                 Uri sourceUri = (Uri) sourceStream;
                 if (sourceUri != null) {
                     String displayName = UriUtils.getDisplayNameForUri(sourceUri, mActivity);
+
+                    if (displayName == null) {
+                        throw new IllegalStateException("DisplayName may not be null!");
+                    }
+
                     String remotePath = mUploadPath + displayName;
 
                     if (ContentResolver.SCHEME_CONTENT.equals(sourceUri.getScheme())) {
@@ -138,11 +142,6 @@ public class UriUploader {
         return mCode;
     }
 
-    private String generateDiplayName() {
-        return mActivity.getString(R.string.common_unknown) +
-                "-" + DisplayUtils.unixTimeToHumanReadable(System.currentTimeMillis());
-    }
-
     /**
      * Requests the upload of a file in the local file system to {@link FileUploader} service.
      *

+ 52 - 90
src/main/java/com/owncloud/android/utils/UriUtils.kt

@@ -13,116 +13,78 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ * along with this program.  If not, see <http:></http:>//www.gnu.org/licenses/>.
  */
+package com.owncloud.android.utils
 
-package com.owncloud.android.utils;
-
-import android.content.ContentResolver;
-import android.content.Context;
-import android.database.Cursor;
-import android.net.Uri;
-import android.provider.MediaStore;
-import android.webkit.MimeTypeMap;
-
-import com.owncloud.android.lib.common.utils.Log_OC;
-
-import java.util.Locale;
-
+import android.content.ContentResolver
+import android.content.Context
+import android.net.Uri
+import android.provider.MediaStore
+import com.owncloud.android.lib.common.utils.Log_OC
 
 /**
  * A helper class for some Uri operations.
  */
-public final class UriUtils {
-
-    private static final String TAG = UriUtils.class.getSimpleName();
-
-    public static final String URI_CONTENT_SCHEME = "content://";
-
-    private UriUtils() {
-        // utility class -> private constructor
-    }
-
-    public static String getDisplayNameForUri(Uri uri, Context context) {
+object UriUtils {
+    private val TAG = UriUtils::class.java.simpleName
+    const val URI_CONTENT_SCHEME = "content://"
 
-        if (uri == null || context == null) {
-            throw new IllegalArgumentException("Received NULL!");
-        }
-
-        String displayName;
-
-        if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
-            displayName = uri.getLastPathSegment();     // ready to return
+    @JvmStatic
+    fun getDisplayNameForUri(uri: Uri?, context: Context?): String? {
+        require(!(uri == null || context == null)) { "Received NULL!" }
+        var displayName: String?
 
-        } else {
+        if (ContentResolver.SCHEME_CONTENT == uri.scheme) {
             // content: URI
-
-            displayName = getDisplayNameFromContentResolver(uri, context);
-
-            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;
-                    }
-                }
-
-            } catch (Exception e) {
-                Log_OC.e(TAG, "No way to get a display name for " + uri.toString());
+            displayName = getDisplayNameFromContentResolver(uri, context)
+            if (displayName == null) {
+                // last chance to have a name
+                displayName = uri.lastPathSegment?.replace("\\s".toRegex(), "")
             }
+        } else {
+            displayName = uri.lastPathSegment // ready to return
         }
 
         // Replace path separator characters to avoid inconsistent paths
-        return displayName.replaceAll("/", "-");
+        return displayName?.replace("/".toRegex(), "-")
     }
 
-
-    private static String getDisplayNameFromContentResolver(Uri uri, Context context) {
-        String displayName = null;
-        String mimeType = context.getContentResolver().getType(uri);
+    private fun getDisplayNameFromContentResolver(uri: Uri, context: Context): String? {
+        var displayName: String? = null
+        val mimeType = context.contentResolver.getType(uri)
         if (mimeType != null) {
-            String displayNameColumn;
-            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;
+            val displayNameColumn: String = when {
+                MimeTypeUtil.isImage(mimeType) -> {
+                    MediaStore.Images.ImageColumns.DISPLAY_NAME
+                }
+                MimeTypeUtil.isVideo(mimeType) -> {
+                    MediaStore.Video.VideoColumns.DISPLAY_NAME
+                }
+                MimeTypeUtil.isAudio(mimeType) -> {
+                    MediaStore.Audio.AudioColumns.DISPLAY_NAME
+                }
+                else -> {
+                    MediaStore.Files.FileColumns.DISPLAY_NAME
+                }
             }
-
-            try (Cursor cursor = context.getContentResolver().query(
-                uri,
-                new String[]{displayNameColumn},
-                null,
-                null,
-                null
-            )) {
-                if (cursor != null) {
-                    cursor.moveToFirst();
-                    displayName = cursor.getString(cursor.getColumnIndex(displayNameColumn));
+            try {
+                context.contentResolver.query(
+                    uri, arrayOf(displayNameColumn),
+                    null,
+                    null,
+                    null
+                ).use { cursor ->
+                    if (cursor != null) {
+                        cursor.moveToFirst()
+                        displayName = cursor.getString(cursor.getColumnIndex(displayNameColumn))
+                    }
                 }
-
-            } catch (Exception e) {
-                Log_OC.e(TAG, "Could not retrieve display name for " + uri.toString());
+            } catch (e: Exception) {
+                Log_OC.e(TAG, "Could not retrieve display name for $uri")
                 // nothing else, displayName keeps null
-
             }
         }
-        return displayName;
+        return displayName
     }
-
-
 }

+ 0 - 1
src/main/res/values/strings.xml

@@ -121,7 +121,6 @@
     <string name="common_save">Save</string>
     <string name="common_error">Error</string>
     <string name="common_loading">Loading…</string>
-    <string name="common_unknown">unknown</string>
     <string name="common_error_unknown">Unknown error</string>
     <string name="common_pending">Pending</string>
     <string name="common_delete">Delete</string>