Ver código fonte

OC-3314: Modify CreateShareOperation#run in the application so that it uses GetRemoteSharesForFileOperation() to get a remote link in the server (if exists) and use it without creating a new one

masensio 11 anos atrás
pai
commit
a043bcd8de

+ 1 - 1
owncloud-android-library

@@ -1 +1 @@
-Subproject commit 30acd4875dda3fd0bec83daaad522f3d5a02ead6
+Subproject commit b7c136286e8f2ef17f99da891f3e9fc600c25aed

+ 47 - 26
src/com/owncloud/android/operations/CreateShareOperation.java

@@ -30,7 +30,9 @@ import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.resources.shares.OCShare;
+import com.owncloud.android.lib.common.operations.RemoteOperation;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
+import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation;
 import com.owncloud.android.lib.resources.shares.ShareType;
 import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation;
 import com.owncloud.android.lib.resources.files.FileUtils;
@@ -40,6 +42,9 @@ import com.owncloud.android.utils.Log_OC;
 public class CreateShareOperation extends SyncOperation {
 
     private static final String TAG = CreateShareOperation.class.getSimpleName();
+    
+    // String to build the link with the token of a share: server address + "/public.php?service=files&t=" + token
+    private final String SHARING_LINK_TOKEN = "/public.php?service=files&t=";
 
     protected FileDataStorageManager mStorageManager;
 
@@ -83,39 +88,31 @@ public class CreateShareOperation extends SyncOperation {
 
     @Override
     protected RemoteOperationResult run(OwnCloudClient client) {
-        CreateRemoteShareOperation operation = new CreateRemoteShareOperation(mPath, mShareType, mShareWith, mPublicUpload, mPassword, mPermissions);
-        RemoteOperationResult result = operation.execute(client);
+        RemoteOperation operation = null;
+        
+        // Check if the share link already exists
+        operation = new GetRemoteSharesForFileOperation(mPath, false, false);
+        RemoteOperationResult result = ((GetRemoteSharesForFileOperation)operation).execute(client);
 
         if (result.isSuccess()) {
-
             if (result.getData().size() > 0) {
                 OCShare share = (OCShare) result.getData().get(0);
-
-                // Update DB with the response
-                share.setPath(mPath);
-                if (mPath.endsWith(FileUtils.PATH_SEPARATOR)) {
-                    share.setIsFolder(true);
-                } else {
-                    share.setIsFolder(false);
-                }
-                share.setPermissions(mPermissions);
-                
-                getStorageManager().saveShare(share);
-                
-                // Update OCFile with data from share: ShareByLink  and publicLink
-                OCFile file = getStorageManager().getFileByPath(mPath);
-                if (file!=null) {
-                    mSendIntent.putExtra(Intent.EXTRA_TEXT, share.getShareLink());
-                    file.setPublicLink(share.getShareLink());
-                    file.setShareByLink(true);
-                    getStorageManager().saveFile(file);
-                    Log_OC.d(TAG, "Public Link = " + file.getPublicLink());
-
+                // Update the link, build it with the token: server address + "/public.php?service=files&t=" + token
+                share.setShareLink(client.getBaseUri() + SHARING_LINK_TOKEN + share.getToken());
+                Log_OC.d(TAG, "Build Share link= " + share.getShareLink());
+                updateData(share);
+            } else {
+                operation = new CreateRemoteShareOperation(mPath, mShareType, mShareWith, mPublicUpload, mPassword, mPermissions);
+                result = ((CreateRemoteShareOperation)operation).execute(client);
+
+                if (result.isSuccess()) {
+                    if (result.getData().size() > 0) {
+                        OCShare share = (OCShare) result.getData().get(0);
+                        updateData(share);
+                    }
                 }
             }
         }
-
-
         return result;
     }
     
@@ -123,5 +120,29 @@ public class CreateShareOperation extends SyncOperation {
     public Intent getSendIntent() {
         return mSendIntent;
     }
+    
+    private void updateData(OCShare share) {
+        // Update DB with the response
+        share.setPath(mPath);
+        if (mPath.endsWith(FileUtils.PATH_SEPARATOR)) {
+            share.setIsFolder(true);
+        } else {
+            share.setIsFolder(false);
+        }
+        share.setPermissions(mPermissions);
+        
+        getStorageManager().saveShare(share);
+        
+        // Update OCFile with data from share: ShareByLink  and publicLink
+        OCFile file = getStorageManager().getFileByPath(mPath);
+        if (file!=null) {
+            mSendIntent.putExtra(Intent.EXTRA_TEXT, share.getShareLink());
+            file.setPublicLink(share.getShareLink());
+            file.setShareByLink(true);
+            getStorageManager().saveFile(file);
+            Log_OC.d(TAG, "Public Link = " + file.getPublicLink());
+
+        }
+    }
 
 }