From f6650c1b3a52c446a455c7fba0fd32dcb051eaba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?L=C3=A9a=20Klein?= <lklein@nuxeo.com>
Date: Fri, 19 Oct 2018 12:05:49 +0200
Subject: [PATCH] NXDRIVE-1349: WIP Direct Edit on non-main blobs

---
 .../nuxeo/drive/seam/NuxeoDriveActions.java   | 34 +++++++++++++++++++
 .../resources/OSGI-INF/nuxeodrive-actions.xml |  3 +-
 .../nuxeo-drive/nuxeo-drive-edit-button.html  |  4 +--
 3 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/nuxeo-drive-jsf/src/main/java/org/nuxeo/drive/seam/NuxeoDriveActions.java b/nuxeo-drive-jsf/src/main/java/org/nuxeo/drive/seam/NuxeoDriveActions.java
index c5e6ef79..788fde13 100644
--- a/nuxeo-drive-jsf/src/main/java/org/nuxeo/drive/seam/NuxeoDriveActions.java
+++ b/nuxeo-drive-jsf/src/main/java/org/nuxeo/drive/seam/NuxeoDriveActions.java
@@ -25,6 +25,7 @@
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
 import java.util.ArrayList;
+import java.util.Formatter;
 import java.util.List;
 import java.util.Set;
 
@@ -58,6 +59,7 @@
 import org.nuxeo.ecm.core.api.NuxeoException;
 import org.nuxeo.ecm.core.api.NuxeoPrincipal;
 import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
+import org.nuxeo.ecm.core.api.blobholder.DocumentBlobHolder;
 import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
 import org.nuxeo.ecm.core.api.security.SecurityConstants;
 import org.nuxeo.ecm.core.io.download.DownloadService;
@@ -235,6 +237,38 @@ public String getDriveEditURL(DocumentModel currentDocument) {
         return sb.toString();
     }
 
+    public String getDriveEditURL(DocumentModel doc, String xPath) {
+        if (doc == null) {
+            return null;
+        }
+
+        Blob blob = (Blob) doc.getPropertyValue(xPath);
+        if (blob == null) {
+            throw new NuxeoException(String.format("Document %s (%s) has no blob, cannot get Drive Edit URL.",
+                    doc.getPathAsString(), doc.getId()));
+        }
+
+        String editURL = "%s://%s/%suser/%s/repo/%s/nxdocid/%s/filename/%s/downloadUrl/%s";
+        ServletRequest servletRequest = (ServletRequest) FacesContext.getCurrentInstance()
+                                                                     .getExternalContext()
+                                                                     .getRequest();
+        String baseURL = VirtualHostHelper.getBaseURL(servletRequest).replaceFirst("://", "/");
+
+        String user = documentManager.getPrincipal().getName();
+        String repo = documentManager.getRepositoryName();
+        String docId = doc.getId();
+        String filename = blob.getFilename();
+        filename = filename.replaceAll("(/|\\\\|\\*|<|>|\\?|\"|:|\\|)", "-");
+        filename = URIUtils.quoteURIPathComponent(filename, true);
+        DownloadService downloadService = Framework.getService(DownloadService.class);
+        String downloadUrl = downloadService.getDownloadUrl(doc, xPath, filename);
+
+        StringBuilder sb = new StringBuilder();
+        Formatter fmt = new Formatter(sb);
+        fmt.format(editURL, NXDRIVE_PROTOCOL, PROTOCOL_COMMAND_EDIT, baseURL, user, repo, docId, filename, downloadUrl);
+        return sb.toString();
+    }
+
     public String navigateToUserCenterNuxeoDrive() {
         return getUserCenterNuxeoDriveView();
     }
diff --git a/nuxeo-drive-jsf/src/main/resources/OSGI-INF/nuxeodrive-actions.xml b/nuxeo-drive-jsf/src/main/resources/OSGI-INF/nuxeodrive-actions.xml
index f60d1d45..254ba1fc 100644
--- a/nuxeo-drive-jsf/src/main/resources/OSGI-INF/nuxeodrive-actions.xml
+++ b/nuxeo-drive-jsf/src/main/resources/OSGI-INF/nuxeodrive-actions.xml
@@ -28,12 +28,11 @@
       </filter-id>
     </action>
 
-    <action id="driveEdit" link="#{nuxeoDriveActions.driveEditURL}"
+    <action id="driveEdit" link="#{nuxeoDriveActions.getDriveEditURL(document, blobXPath)}"
       order="200" label="command.driveEdit" type="bare_link" icon="/icons/drive_edit.png">
       <category>BLOB_ACTIONS</category>
       <filter-id>has_one_auth_token</filter-id>
       <filter-id>can_drive_edit</filter-id>
-      <filter-id>isMainBlob</filter-id>
     </action>
 
     <action id="driveEditListing" link="#{nuxeoDriveActions.getDriveEditURL(actionContextDocument)}"
diff --git a/nuxeo-drive-web-ui/src/main/resources/web/nuxeo.war/ui/nuxeo-drive/nuxeo-drive-edit-button.html b/nuxeo-drive-web-ui/src/main/resources/web/nuxeo.war/ui/nuxeo-drive/nuxeo-drive-edit-button.html
index 53389b44..7721742c 100644
--- a/nuxeo-drive-web-ui/src/main/resources/web/nuxeo.war/ui/nuxeo-drive/nuxeo-drive-edit-button.html
+++ b/nuxeo-drive-web-ui/src/main/resources/web/nuxeo.war/ui/nuxeo-drive/nuxeo-drive-edit-button.html
@@ -70,8 +70,8 @@ <h1>[[i18n('driveEditButton.dialog.heading')]]</h1>
           return false;
         }
         var isExcluded = false; //['Collection'].indexOf(this.document.type) !== -1;
-        var isMainBlob = this.document.properties['file:content'] === blob;
-        return !isExcluded && isMainBlob;
+        // var isMainBlob = this.document.properties['file:content'] === blob;
+        return !isExcluded; // && isMainBlob;
       },
 
       _go: function() {
