Эх сурвалжийг харах

allow editing of files, saves back to server

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
tobiasKaminsky 6 жил өмнө
parent
commit
fc507e0989

+ 36 - 11
src/main/java/com/owncloud/android/providers/DocumentsStorageProvider.java

@@ -150,10 +150,10 @@ public class DocumentsStorageProvider extends DocumentsProvider {
         final long docId = Long.parseLong(documentId);
         updateCurrentStorageManagerIfNeeded(docId);
 
-        OCFile file = currentStorageManager.getFileById(docId);
+        OCFile ocFile = currentStorageManager.getFileById(docId);
 
-        if (file == null) {
-            throw new FileNotFoundException("File with id " + documentId + " not found!");
+        if (ocFile == null) {
+            throw new FileNotFoundException("File not found: " + documentId);
         }
 
         Account account = currentStorageManager.getAccount();
@@ -163,11 +163,10 @@ public class DocumentsStorageProvider extends DocumentsProvider {
             throw new FileNotFoundException("Context may not be null!");
         }
 
-        if (!file.isDown()) {
-
+        if (!ocFile.isDown()) {
             Intent i = new Intent(getContext(), FileDownloader.class);
             i.putExtra(FileDownloader.EXTRA_ACCOUNT, account);
-            i.putExtra(FileDownloader.EXTRA_FILE, file);
+            i.putExtra(FileDownloader.EXTRA_FILE, ocFile);
             if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                 context.startForegroundService(i);
             } else {
@@ -178,14 +177,14 @@ public class DocumentsStorageProvider extends DocumentsProvider {
                 if (!waitOrGetCancelled(cancellationSignal)) {
                     throw new FileNotFoundException("File with id " + documentId + " not found!");
                 }
-                file = currentStorageManager.getFileById(docId);
+                ocFile = currentStorageManager.getFileById(docId);
 
-                if (file == null) {
+                if (ocFile == null) {
                     throw new FileNotFoundException("File with id " + documentId + " not found!");
                 }
-            } while (!file.isDown());
+            } while (!ocFile.isDown());
         } else {
-            OCFile finalFile = file;
+            OCFile finalFile = ocFile;
             Thread syncThread = new Thread(() -> {
                 try {
                     FileDataStorageManager storageManager =
@@ -220,7 +219,33 @@ public class DocumentsStorageProvider extends DocumentsProvider {
             }
         }
 
-        return ParcelFileDescriptor.open(new File(file.getStoragePath()), ParcelFileDescriptor.parseMode(mode));
+        File file = new File(ocFile.getStoragePath());
+        int accessMode = ParcelFileDescriptor.parseMode(mode);
+        boolean isWrite = (mode.indexOf('w') != -1);
+
+        final OCFile oldFile = ocFile;
+        final OCFile newFile = ocFile;
+
+        if (isWrite) {
+            try {
+                Handler handler = new Handler(context.getMainLooper());
+                return ParcelFileDescriptor.open(file, accessMode, handler, l -> {
+                    RemoteOperationResult result = new SynchronizeFileOperation(newFile, oldFile, account, true,
+                                                                                context)
+                        .execute(client, currentStorageManager);
+
+                    boolean success = result.isSuccess();
+
+                    if (!success) {
+                        Log_OC.e(TAG, "Failed to update document with id " + documentId);
+                    }
+                });
+            } catch (IOException e) {
+                throw new FileNotFoundException("Failed to open/edit document with id " + documentId);
+            }
+        } else {
+            return ParcelFileDescriptor.open(file, accessMode);
+        }
     }
 
     private void showToast() {

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

@@ -1,4 +1,4 @@
-/**
+/*
  *   nextCloud Android client application
  *
  *   @author Bartosz Przybylski
@@ -49,7 +49,7 @@ 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 = imagePath != null ? Document.FLAG_SUPPORTS_THUMBNAIL : 0;
+        int flags = Document.FLAG_SUPPORTS_WRITE | (imagePath != null ? Document.FLAG_SUPPORTS_THUMBNAIL : 0);
 
         if (file.isFolder()) {
             flags = flags | Document.FLAG_DIR_SUPPORTS_CREATE;