Browse Source

Get file mimeType and pass it for being checked on UploadFileOperation class. Take into account if file extension comes in content Uri

jabarros 10 years ago
parent
commit
5d784d98b4

+ 9 - 0
src/com/owncloud/android/datamodel/OCFile.java

@@ -156,6 +156,15 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
         return mRemotePath;
     }
 
+    /**
+     * Can be used to set the path where the file will be stored
+     * 
+     * @param remote_path to set
+     */
+    public void setRemotePath(String remote_path) {
+        mRemotePath = remote_path;
+    }
+
     /**
      * Can be used to check, whether or not this file exists in the database
      * already

+ 23 - 1
src/com/owncloud/android/operations/UploadFileOperation.java

@@ -61,6 +61,10 @@ public class UploadFileOperation extends RemoteOperation {
 
     private static final String TAG = UploadFileOperation.class.getSimpleName();
 
+    private static final String URI_CONTENT_SCHEME = "content://";
+    private static final String MIME_TYPE_PDF = "application/pdf";
+    private static final String FILE_EXTENSION_PDF = ".pdf";
+
     private Account mAccount;
     private OCFile mFile;
     private OCFile mOldFile;
@@ -221,6 +225,12 @@ public class UploadFileOperation extends RemoteOperation {
                 } else {
 
                     String temporalPath = FileStorageUtils.getTemporalPath(mAccount.name) + mFile.getRemotePath();
+
+                    if (isPdfFileFromContentProviderWithoutExtension()){
+                        temporalPath += FILE_EXTENSION_PDF;
+                        mFile.setRemotePath(mFile.getRemotePath() + FILE_EXTENSION_PDF);
+                    }
+
                     mFile.setStoragePath(temporalPath);
                     temporalFile = new File(temporalPath);
 
@@ -239,7 +249,8 @@ public class UploadFileOperation extends RemoteOperation {
 
                     try {
 
-                        if (mOriginalStoragePath.startsWith("content://")) {
+                        // In case document provider schema as 'content://'
+                        if (mOriginalStoragePath.startsWith(URI_CONTENT_SCHEME)) {
 
                             Uri uri = Uri.parse(mOriginalStoragePath);
 
@@ -442,4 +453,15 @@ public class UploadFileOperation extends RemoteOperation {
     public void cancel() {
         mUploadOperation.cancel();
     }
+
+    /**
+     * Checks if content provider, using the content:// scheme, returns a file with mime-type 
+     * 'application/pdf' but file has not extension
+     * @return true if is needed to add the pdf file extension to the file
+     */
+    private boolean isPdfFileFromContentProviderWithoutExtension() {
+        return mOriginalStoragePath.startsWith(URI_CONTENT_SCHEME) && 
+                mFile.getMimetype().equals(MIME_TYPE_PDF) && 
+                !mFile.getFileName().endsWith(FILE_EXTENSION_PDF);
+    }
 }

+ 30 - 8
src/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -158,7 +158,7 @@ OnSslUntrustedCertListener, OnEnforceableRefreshListener {
     
     private OCFile mWaitingToSend;
 
-    private static final String URI_TYPE_OF_CONTENT_PROVIDER = "content://";
+    private static final String URI_CONTENT_SCHEME = "content://";
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -662,8 +662,12 @@ OnSslUntrustedCertListener, OnEnforceableRefreshListener {
 
     private void requestSimpleUpload(Intent data, int resultCode) {
         String filepath = null;
+        String mimeType = null;
+
+        Uri selectedImageUri = data.getData();
+
         try {
-            Uri selectedImageUri = data.getData();
+            mimeType = getContentResolver().getType(selectedImageUri);
 
             String filemanagerstring = selectedImageUri.getPath();
             String selectedImagePath = getPath(selectedImageUri);
@@ -696,9 +700,8 @@ OnSslUntrustedCertListener, OnEnforceableRefreshListener {
         if (!remotepath.endsWith(OCFile.PATH_SEPARATOR))
             remotepath += OCFile.PATH_SEPARATOR;
 
-        if (filepath.startsWith(URI_TYPE_OF_CONTENT_PROVIDER)) {
-            // The query, since it only applies to a single document, will only return
-            // one row. 
+        if (filepath.startsWith(URI_CONTENT_SCHEME)) {
+
             Cursor cursor = MainApp.getAppContext().getContentResolver()
                     .query(Uri.parse(filepath), null, null, null, null, null);
 
@@ -706,9 +709,10 @@ OnSslUntrustedCertListener, OnEnforceableRefreshListener {
                 if (cursor != null && cursor.moveToFirst()) {
                     String displayName = cursor.getString(
                             cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
-                    Log.i(TAG, "Display Name: " + displayName);
+                    Log.i(TAG, "Display Name: " + displayName + "; mimeType: " + mimeType);
+
+                    remotepath += displayName + getComposedFileExtension(filepath);
 
-                    remotepath += displayName;
                 }
             } finally {
                 cursor.close();
@@ -720,6 +724,7 @@ OnSslUntrustedCertListener, OnEnforceableRefreshListener {
 
         i.putExtra(FileUploader.KEY_LOCAL_FILE, filepath);
         i.putExtra(FileUploader.KEY_REMOTE_FILE, remotepath);
+        i.putExtra(FileUploader.KEY_MIME_TYPE, mimeType);
         i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
         if (resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)
             i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE);
@@ -1034,7 +1039,7 @@ OnSslUntrustedCertListener, OnEnforceableRefreshListener {
      * @return Whether the Uri is from a content provider as kind "content://..."
      */
     public static boolean isContentDocument(Uri uri) {
-        return uri.toString().startsWith(URI_TYPE_OF_CONTENT_PROVIDER);
+        return uri.toString().startsWith(URI_CONTENT_SCHEME);
     }
 
     /**
@@ -1946,4 +1951,21 @@ OnSslUntrustedCertListener, OnEnforceableRefreshListener {
     private void sortByName(boolean ascending){
         getListOfFilesFragment().sortByName(ascending);
     }
+
+    /**
+     * Get the file extension if it is on path as type "content://.../DocInfo.doc"
+     * @param filepath: Content Uri converted to string format
+     * @return String: fileExtension (type '.pdf'). Empty if no extension
+     */
+    private String getComposedFileExtension(String filepath) {
+        String fileExtension = "";
+        String fileNameInContentUri = filepath.substring(filepath.lastIndexOf("/"));
+
+        // Check if extension is included in uri
+        int pos = fileNameInContentUri.lastIndexOf('.');
+        if (pos >= 0) {
+            fileExtension = fileNameInContentUri.substring(pos);
+        }
+        return fileExtension;
+    }
 }