Преглед на файлове

Better policies to handle possible errors in remote MIME type values; avoid crashes when MIME types is trying to be found for files without extension

David A. Velasco преди 12 години
родител
ревизия
121a462bdf

+ 1 - 1
AndroidManifest.xml

@@ -18,7 +18,7 @@
  -->
 <manifest package="eu.alefzero.owncloud"
     android:versionCode="1"
-    android:versionName="0.1.137B" xmlns:android="http://schemas.android.com/apk/res/android">
+    android:versionName="0.1.138B" xmlns:android="http://schemas.android.com/apk/res/android">
 
     <uses-permission android:name="android.permission.GET_ACCOUNTS" />
     <uses-permission android:name="android.permission.USE_CREDENTIALS" />

+ 10 - 3
src/eu/alefzero/owncloud/files/PhotoTakenBroadcastReceiver.java

@@ -111,9 +111,16 @@ public class PhotoTakenBroadcastReceiver extends BroadcastReceiver {
                         Intent upload_intent = new Intent(context, InstantUploadService.class);
                         Account account = new Account(account_name, AccountAuthenticator.ACCOUNT_TYPE);
                         
-                        String mimeType = MimeTypeMap.getSingleton()
-                                .getMimeTypeFromExtension(
-                                        f.getName().substring(f.getName().lastIndexOf('.') + 1));
+                        String mimeType;
+                        try {
+                            mimeType = MimeTypeMap.getSingleton()
+                                    .getMimeTypeFromExtension(
+                                            f.getName().substring(f.getName().lastIndexOf('.') + 1));
+                        
+                        } catch (IndexOutOfBoundsException e) {
+                            Log.e(TAG, "Trying to find out MIME type of a file without extension: " + f.getName());
+                            mimeType = "application/octet-stream";
+                        }
                         
                         upload_intent.putExtra(InstantUploadService.KEY_ACCOUNT, account);
                         upload_intent.putExtra(InstantUploadService.KEY_FILE_PATH, file_path);

+ 11 - 3
src/eu/alefzero/owncloud/files/services/FileUploader.java

@@ -167,10 +167,18 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
         Log.d(TAG, "Will upload " + mTotalDataToSend + " bytes, with " + mLocalPaths.length + " files");
         
         for (int i = 0; i < mLocalPaths.length; ++i) {
-            String mimeType = MimeTypeMap.getSingleton()
-                    .getMimeTypeFromExtension(
-                            mLocalPaths[i].substring(mLocalPaths[i]
+            
+            String mimeType;
+            try {
+                mimeType = MimeTypeMap.getSingleton()
+                        .getMimeTypeFromExtension(
+                                mLocalPaths[i].substring(mLocalPaths[i]
                                     .lastIndexOf('.') + 1));
+            } catch (IndexOutOfBoundsException e) {
+                Log.e(TAG, "Trying to find out MIME type of a file without extension: " + mLocalPaths[i]);
+                mimeType = "application/octet-stream";
+            }
+            
             mResult = false;
             mCurrentIndexUpload = i;
             if (wc.putFile(mLocalPaths[i], mRemotePaths[i], mimeType)) {

+ 1 - 10
src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java

@@ -143,16 +143,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
         OCFile file = new OCFile(we.path());
         file.setCreationTimestamp(we.createTimestamp());
         file.setFileLength(we.contentLength());
-        
-        // dvelasco; looks like server is not sending very precise mimeTypes; mp3 file results un application/oct
-        String filename = file.getFileName();
-        String mimeType = MimeTypeMap.getSingleton()
-                .getMimeTypeFromExtension(filename.substring(filename.lastIndexOf('.') + 1));
-        if (mimeType == null)
-            file.setMimetype(we.contentType());
-        else
-            file.setMimetype(mimeType);
-        
+        file.setMimetype(we.contentType());
         file.setModificationTimestamp(we.modifiedTimesamp());
         file.setLastSyncDate(mCurrentSyncTime);
         return file;

+ 35 - 6
src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java

@@ -39,6 +39,7 @@ import android.view.LayoutInflater;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.ViewGroup;
+import android.webkit.MimeTypeMap;
 import android.widget.Button;
 import android.widget.ImageView;
 import android.widget.TextView;
@@ -236,20 +237,48 @@ public class FileDetailFragment extends SherlockFragment implements
                     
                 } catch (Throwable t) {
                     preview.setVisibility(View.INVISIBLE);
-                    Log.e(TAG, "Unexpected error while creating image preview " + mFile.getFileLength());
+                    Log.e(TAG, "Unexpected error while creating image preview " + mFile.getFileLength(), t);
                 }
                 downloadButton.setText(R.string.filedetails_open);
                 downloadButton.setOnClickListener(new OnClickListener() {
                     @Override
                     public void onClick(View v) {
-                        Intent i = new Intent(Intent.ACTION_VIEW);
-                        i.setDataAndType(Uri.parse("file://"+mFile.getStoragePath()), mFile.getMimetype());
-                        i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
+                        String storagePath = mFile.getStoragePath();
                         try {
+                            Intent i = new Intent(Intent.ACTION_VIEW);
+                            i.setDataAndType(Uri.parse("file://"+ storagePath), mFile.getMimetype());
+                            i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                             startActivity(i);
                             
-                        } catch (ActivityNotFoundException e) {
-                            Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show();
+                        } catch (Throwable t) {
+                            Log.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile.getMimetype());
+                            boolean toastIt = true; 
+                            String mimeType = "";
+                            try {
+                                Intent i = new Intent(Intent.ACTION_VIEW);
+                                mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
+                                if (mimeType != mFile.getMimetype()) {
+                                    i.setDataAndType(Uri.parse("file://"+mFile.getStoragePath()), mimeType);
+                                    i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
+                                    startActivity(i);
+                                    toastIt = false;
+                                }
+                                
+                            } catch (IndexOutOfBoundsException e) {
+                                Log.e(TAG, "Trying to find out MIME type of a file without extension: " + storagePath);
+                                
+                            } catch (ActivityNotFoundException e) {
+                                Log.e(TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension");
+                                
+                            } catch (Throwable th) {
+                                Log.e(TAG, "Unexpected problem when opening: " + storagePath, th);
+                                
+                            } finally {
+                                if (toastIt) {
+                                    Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show();
+                                }
+                            }
+                            
                         }
                     }
                 });

+ 4 - 0
src/eu/alefzero/webdav/WebdavEntry.java

@@ -52,6 +52,10 @@ public class WebdavEntry {
             prop = propSet.get(DavPropertyName.GETCONTENTTYPE);
             if (prop != null) {
                 mContentType = (String) prop.getValue();
+                // dvelasco: some builds of ownCloud server 4.0.x added a trailing ';' to the MIME type ; if looks fixed, but let's be cautious
+                if (mContentType.indexOf(";") >= 0) {
+                    mContentType = mContentType.substring(0, mContentType.indexOf(";"));
+                }
             } else {
                 mContentType = "DIR";
                 /*