From 546703fe168f1714e75cbdbb06dc5560ba55e082 Mon Sep 17 00:00:00 2001
From: Alexey Lunin <alexey.lunin0@gmail.com>
Date: Mon, 25 Feb 2019 17:12:37 +0400
Subject: [PATCH] Added manual create and put methods

---
 javascript/src/iframe/viamapi-iframe.js | 43 +++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/javascript/src/iframe/viamapi-iframe.js b/javascript/src/iframe/viamapi-iframe.js
index cf62bf4..3018b57 100644
--- a/javascript/src/iframe/viamapi-iframe.js
+++ b/javascript/src/iframe/viamapi-iframe.js
@@ -897,6 +897,49 @@ const connection = Penpal.connectToParent({
 
       return encodeResponse("200", response.data, "Email signed");
     },
+    createDocument: async (path, passportUUID, contenttype) => {
+      const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
+      if (
+        !authenticationPublicKey ||
+        !window.loadedIdentities[authenticationPublicKey] ||
+        !extendPinCodeTtl(authenticationPublicKey)
+      ) {
+        return encodeResponse("400", "", "Identity not authenticated");
+      }
+
+      const headers = window.viamApi.getConfig().headers;
+      headers.path = path;
+      headers.passportuuid = passportUUID;
+      headers.contenttype = contenttype;
+
+      const response = await executeRestfulFunction(
+        "private", window.viamApi, window.viamApi.documentCreateDocument);
+
+      return encodeResponse("200", response.data, "Document created");
+    },
+    putDocument: async (passportUUID, resourceid, file) => {
+      const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
+      if (
+        !authenticationPublicKey ||
+        !window.loadedIdentities[authenticationPublicKey] ||
+        !extendPinCodeTtl(authenticationPublicKey)
+      ) {
+        return encodeResponse("400", "", "Identity not authenticated");
+      }
+
+      const data = new FormData();
+      data.append('file', file);
+
+      const config = window.viamApi.getConfig();
+      config.headers.passportuuid = passportUUID;
+      config.headers.resourceid = resourceid;
+      config.data = data;
+
+      const response = await executeRestfulFunction(
+        "private", window.viamApi, window.viamApi.documentCreateDocument);
+
+      return encodeResponse("200", response.data, "Document created");
+    },
     hasSession() {
       return new Penpal.Promise(result => {
         const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
-- 
GitLab