From d78518706a60c7e9efcb1de9c747909487e1397b Mon Sep 17 00:00:00 2001
From: Alexey Lunin <alexey.lunin@vereign.com>
Date: Thu, 28 Feb 2019 22:48:53 +0400
Subject: [PATCH] Added wopi put file

---
 javascript/src/iframe/viamapi-iframe.js | 40 ++++++++++++++++++++
 javascript/src/iframe/wopiapi-iframe.js | 23 ++++++++++++
 main.go                                 | 49 +++----------------------
 3 files changed, 68 insertions(+), 44 deletions(-)

diff --git a/javascript/src/iframe/viamapi-iframe.js b/javascript/src/iframe/viamapi-iframe.js
index b6d0af8..811b242 100644
--- a/javascript/src/iframe/viamapi-iframe.js
+++ b/javascript/src/iframe/viamapi-iframe.js
@@ -1074,9 +1074,49 @@ const connection = Penpal.connectToParent({
         result(res)
       })
     },
+
+    // Collabora APIs
     collaboraDiscovery() {
       return collaboraApi.discovery().then(apps => apps);
     },
+
+    // WOPI APIs
+    // TODO rewrite with async await
+    getPassports: function(fileID) {
+      return new Penpal.Promise(function(result) {
+        const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
+
+        if (
+          !authenticationPublicKey ||
+          !window.loadedIdentities[authenticationPublicKey] ||
+          !extendPinCodeTtl(authenticationPublicKey)
+        ) {
+          return encodeResponse("400", "", "Identity not authenticated");
+        }
+
+        wopiAPI.getPassports(fileID).then(function(response) {
+          result(response.data);
+        });
+      });
+    },
+    // TODO rewrite with async await
+    wopiPutFile: function (fileId, accessToken, file) {
+      return new Penpal.Promise(function(result) {
+        const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
+
+        if (
+          !authenticationPublicKey ||
+          !window.loadedIdentities[authenticationPublicKey] ||
+          !extendPinCodeTtl(authenticationPublicKey)
+        ) {
+          return encodeResponse("400", "", "Identity not authenticated");
+        }
+
+        wopiAPI.putDocument(fileId, accessToken, file).then(function(response) {
+          result(response.data);
+        });
+      });
+    },
     ...penpalMethods
   }
 });
diff --git a/javascript/src/iframe/wopiapi-iframe.js b/javascript/src/iframe/wopiapi-iframe.js
index 8d5b72a..3607cae 100644
--- a/javascript/src/iframe/wopiapi-iframe.js
+++ b/javascript/src/iframe/wopiapi-iframe.js
@@ -19,4 +19,27 @@ WopiAPI.prototype.getPassports = function (fileID) {
   return axios(requestConfig);
 };
 
+WopiAPI.prototype.putDocument = function (fileId, accessToken, file) {
+  const { publicKey, uuid, token, deviceHash } = window.viamApi.getConfig().headers;
+  const requestConfig = {
+    url: `${window.WOPI_URL}files/${fileId}/contents`,
+    method: 'POST',
+    headers: {
+      publicKey,
+      uuid,
+      token,
+      deviceHash
+    },
+    params: {
+      access_token: accessToken
+    },
+    data: file
+  };
+
+  return axios(requestConfig);
+};
+
+// TODO
+// add put file
+
 module.exports = WopiAPI;
diff --git a/main.go b/main.go
index 83c1917..9403462 100644
--- a/main.go
+++ b/main.go
@@ -43,7 +43,6 @@ func buildPenpalMethods() string {
 			"export default {\n"
 
 	methods := generatePenpalRemoteMethods(endPoints)
-	methods += getWopiAPIPenpalMethods()
 
 	result += methods
 
@@ -79,7 +78,6 @@ func buildViamAPI() string {
 		"    }\n" +
 		"}\n\n"
 
-
 	result += "ViamAPI.prototype.setSessionData = function(uuid, token) {\n" +
 		"    this.config.headers.uuid = uuid;\n" +
 		"    this.config.headers.token = token;\n" +
@@ -621,12 +619,12 @@ func generatePenpalRemoteMethods(endPoints map[string]*server.EndPoint) string {
 		}
 
 		if endPoints[url].Url == "/document/createDocument" {
-            continue
-        }
+			continue
+		}
 
-        if endPoints[url].Url == "/document/putDocument" {
-            continue
-        }
+		if endPoints[url].Url == "/document/putDocument" {
+			continue
+		}
 
 		if url == "/identity/getIdentityProfileData" {
 			privateCheckSnippet = `
@@ -677,7 +675,6 @@ func generatePenpalRemoteMethods(endPoints map[string]*server.EndPoint) string {
 				}
 			}
 
-
 			snippet := ""
 
 			if endPoints[url].HandlerType == "private" {
@@ -739,39 +736,3 @@ func generatePenpalRemoteMethods(endPoints map[string]*server.EndPoint) string {
 
 	return methods
 }
-
-func getWopiAPIPenpalMethods() string {
-	return `getPassports: function(fileID) {
-		return new Penpal.Promise(function(result) {
-			const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
-			if (authenticationPublicKey === null) {
-				result({
-					"data" : "",
-					"code" : "400",
-					"status" : "Identity not authenticated"
-				});
-			}
-
-			if (loadedIdentities[authenticationPublicKey] === null) {
-				result({
-					"data" : "",
-					"code" : "400",
-					"status" : "Identity not authenticated"
-				});
-			}
-
-			const success = extendPinCodeTtl(authenticationPublicKey);
-
-			if (success === false) {
-				result({"data" : "",
-					"code" : "400",
-					"status" : "Identity not authenticated"
-				});
-			}
-
-			wopiAPI.getPassports(fileID).then(function(response) {
-				result(response.data);
-			});
-		});
-	}`
-}
-- 
GitLab