Browse Source

Merge pull request #3435 from nextcloud/DocumentProviderDeleteFile

Document provider: delete file
Andy Scherzinger 6 năm trước cách đây
mục cha
commit
08cf746db7

+ 24 - 1
src/main/java/com/owncloud/android/providers/DocumentsStorageProvider.java

@@ -54,6 +54,7 @@ import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.operations.CreateFolderOperation;
+import com.owncloud.android.operations.RemoveFileOperation;
 import com.owncloud.android.operations.SynchronizeFileOperation;
 import com.owncloud.android.ui.activity.ConflictsResolveActivity;
 import com.owncloud.android.ui.activity.Preferences;
@@ -116,7 +117,7 @@ public class DocumentsStorageProvider extends DocumentsProvider {
         }
 
         if (currentStorageManager == null) {
-            throw new FileNotFoundException("File with " + documentId + " not found");
+            throw new FileNotFoundException("File with id " + documentId + " not found");
         }
 
         OCFile file = currentStorageManager.getFileById(docId);
@@ -324,6 +325,28 @@ public class DocumentsStorageProvider extends DocumentsProvider {
         return String.valueOf(newFolder.getFileId());
     }
 
+    @Override
+    public void deleteDocument(String documentId) throws FileNotFoundException {
+        long docId = Long.parseLong(documentId);
+        updateCurrentStorageManagerIfNeeded(docId);
+
+        OCFile file = currentStorageManager.getFileById(docId);
+
+        if (file == null) {
+            throw new FileNotFoundException("File " + documentId + " not found!");
+        }
+        Account account = currentStorageManager.getAccount();
+
+        RemoveFileOperation removeFileOperation = new RemoveFileOperation(file.getRemotePath(), false, account, true,
+                                                                          getContext());
+
+        RemoteOperationResult result = removeFileOperation.execute(client, currentStorageManager);
+
+        if (!result.isSuccess()) {
+            throw new FileNotFoundException("Failed to delete document with documentId " + documentId);
+        }
+    }
+
     @SuppressLint("LongLogTag")
     private void updateCurrentStorageManagerIfNeeded(long docId) {
         if (rootIdToStorageManager == null) {

+ 3 - 1
src/main/java/org/nextcloud/providers/cursors/FileCursor.java

@@ -49,7 +49,9 @@ public class FileCursor extends MatrixCursor {
         final int iconRes = MimeTypeUtil.getFileTypeIconId(file.getMimeType(), file.getFileName());
         final String mimeType = file.isFolder() ? Document.MIME_TYPE_DIR : file.getMimeType();
         final String imagePath = MimeTypeUtil.isImage(file) && file.isDown() ? file.getStoragePath() : null;
-        int flags = Document.FLAG_SUPPORTS_WRITE | (imagePath != null ? Document.FLAG_SUPPORTS_THUMBNAIL : 0);
+        int flags = Document.FLAG_SUPPORTS_DELETE |
+            Document.FLAG_SUPPORTS_WRITE |
+            (imagePath != null ? Document.FLAG_SUPPORTS_THUMBNAIL : 0);
 
         if (file.isFolder()) {
             flags = flags | Document.FLAG_DIR_SUPPORTS_CREATE;

+ 1 - 2
src/main/java/org/nextcloud/providers/cursors/RootCursor.java

@@ -46,8 +46,7 @@ public class RootCursor extends MatrixCursor {
     }
 
     public void addRoot(Account account, Context context) {
-        final FileDataStorageManager manager =
-                new FileDataStorageManager(account, context.getContentResolver());
+        final FileDataStorageManager manager = new FileDataStorageManager(account, context.getContentResolver());
         final OCFile mainDir = manager.getFileByPath("/");
 
         newRow().add(Root.COLUMN_ROOT_ID, account.name)