From d73e85181a414b8b21a38e3a04098f88439017e3 Mon Sep 17 00:00:00 2001
From: Yogmatee Roochun <yroochun@ebi.ac.uk>
Date: Tue, 5 Mar 2019 16:06:18 +0000
Subject: [PATCH] xsweet task - ongoing

---
 package.json                      |   3 +
 server/xsweet-conversion/index.js | 140 ++++++++++++++++++++++++++++--
 yarn.lock                         |  49 ++++++++++-
 3 files changed, 181 insertions(+), 11 deletions(-)

diff --git a/package.json b/package.json
index f1d5c2a44..2e021c8e7 100755
--- a/package.json
+++ b/package.json
@@ -26,6 +26,7 @@
     "babel-core": "^6.26.0",
     "chokidar": "^2.0.4",
     "config": "^1.26.2",
+    "curl-request": "^1.1.1",
     "dateformat": "^3.0.3",
     "decode-html": "^2.0.0",
     "dotenv": "^6.0.0",
@@ -85,6 +86,8 @@
     "redux": "^3.6.0",
     "redux-form": "^7.0.3",
     "redux-logger": "^3.0.1",
+    "request": "^2.88.0",
+    "request-promise": "^4.2.4",
     "rfr": "^1.2.3",
     "supertest": "^3.0.0",
     "tar": "^4.4.8",
diff --git a/server/xsweet-conversion/index.js b/server/xsweet-conversion/index.js
index e66c3be4d..47fcc08aa 100644
--- a/server/xsweet-conversion/index.js
+++ b/server/xsweet-conversion/index.js
@@ -1,13 +1,135 @@
-// const Xsweet = require('@pubsweet/job-xsweet')
+const fs = require('fs')
+const request = require('request')
+const rp = require('request-promise')
+const tar = require('../utils/unTar.js')
+const logger = require('@pubsweet/logger')
+const mime = require('mime-types')
+const getUser = require('../utils/user.js')
+const uuidv4 = require('uuid/v4')
+const path = require('path')
+const files = require('../utils/files.js')
+const db = require('../utils/db.js')
 
-/*
-curl -X POST \
-  'http://localhost:3000/convertDocxToHTML?=' \
-  -H 'cache-control: no-cache' \
-  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-  -F docx=@/Users/juretriglav/src/pdk-tesms/pubsweet/packages/components/job-xsweet/src/test.docx
+// '/home/yogmatee/projects/xmlValidation_files/xweet_files/tiana.docx'
 
+xsweetConvert(
+  '/home/yogmatee/projects/xmlValidation_files/xweet_files/tiana.docx',
+  'EMS90002',
+)
 
-http://localhost:3000/convertDocxToHTML?=cache-control: no-cache
+function xsweetConvert(fileUrl, manuscriptId) {
+  const options = {
+    method: 'POST',
+    uri: 'http://localhost:3000/convertDocxToHTML',
+    formData: {
+      docx: fs.createReadStream(
+        '/home/yogmatee/projects/xmlValidation_files/xweet_files/tiana.docx',
+      ),
+    },
+    headers: {
+      /* 'content-type': 'multipart/form-data' */
+      // Is set automatically
+    },
+  }
 
-*/
+  rp(options)
+    .then(async function(body) {
+      try {
+        const tmpPath = await tar.createTempDir()
+        const user = await getUser.getAdminUser()
+        fs.writeFileSync(`${tmpPath}/${manuscriptId}.html`, body)
+        console.log('The file has been saved!', tmpPath)
+        const fileInfo = getFileInfo(
+          `${tmpPath}/${manuscriptId}.html`,
+          manuscriptId,
+          user,
+        )
+        const uuid = uuidv4()
+        // upload to minio
+        files.uploadFileToMinio(
+          `${uuid}${fileInfo.extension}`,
+          fileInfo.filename,
+          fileInfo.url,
+          fileInfo.mimeType,
+        )
+        delete fileInfo.extension
+        await db.createFiles([fileInfo])
+        console.log('file has been uploaded to Minio and to db')
+      } catch (err) {
+        throw err
+      }
+    })
+    .catch(function(err) {
+      logger.error('Conversion failed:', err)
+    })
+}
+
+function xsweetConvert_XXX(fileUrl, manuscriptId) {
+  const formData = {
+    docx: fs.createReadStream(fileUrl),
+  }
+  request.post(
+    { url: 'http://localhost:3000/convertDocxToHTML', formData: formData },
+    async function callbackFunc(err, httpResponse, body) {
+      return new Promise(async (resolve, reject) => {
+        if (err) {
+          logger.error('Conversion failed:', err)
+          reject(err)
+        }
+
+        const tmpPath = await tar.createTempDir()
+        const user = await getUser.getAdminUser()
+
+        fs.writeFile(
+          `${tmpPath}/${manuscriptId}.html`,
+          body,
+          'utf8',
+          async err => {
+            if (err) throw err
+            console.log('The file has been saved!', tmpPath)
+
+            const fileInfo = getFileInfo(
+              `${tmpPath}/${manuscriptId}.html`,
+              manuscriptId,
+              user,
+            )
+            const uuid = uuidv4()
+
+            // upload to minio
+            files.uploadFileToMinio(
+              `${uuid}${fileInfo.extension}`,
+              fileInfo.filename,
+              fileInfo.url,
+              fileInfo.mimeType,
+            )
+
+            delete fileInfo.extension
+            const test = await db.createFiles(fileInfo)
+            console.log(test)
+            console.log('file has been uploaded to Minio and to db')
+            resolve(true)
+          },
+        )
+      })
+    },
+  )
+}
+
+function getFileInfo(filepath, manuscriptId, user) {
+  const filename = filepath.substring(filepath.lastIndexOf('/') + 1)
+  const fileExt = path.extname(filepath)
+  const fileSize = fs.statSync(filepath).size
+
+  const fileInfo = {
+    url: filepath,
+    filename,
+    type: 'source',
+    label: '',
+    size: fileSize,
+    extension: fileExt,
+    mimeType: `${mime.contentType(fileExt)}`,
+    manuscriptId,
+    updatedBy: user.id,
+  }
+  return fileInfo
+}
diff --git a/yarn.lock b/yarn.lock
index 6e7f202ab..86be4f9cf 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -3649,6 +3649,15 @@ cssstyle@^1.0.0:
   dependencies:
     cssom "0.3.x"
 
+curl-request@^1.1.1:
+  version "1.1.1"
+  resolved "https://registry.yarnpkg.com/curl-request/-/curl-request-1.1.1.tgz#72c785e3345d0ec1a67c68fb982f46c8b1aa1550"
+  integrity sha512-Rna1zumGl5YwwGEmQTggWGFogUjY6BGXrco7B0/o6WKubNS4vWmjoOSFXtNrPm5ffehOM09DgRw+QeA0n9a+9Q==
+  dependencies:
+    net "^1.0.2"
+    node-libcurl "^1.2.0"
+    querystring "^0.2.0"
+
 currently-unhandled@^0.4.1:
   version "0.4.1"
   resolved "https://registry.yarnpkg.com/currently-unhandled/-/currently-unhandled-0.4.1.tgz#988df33feab191ef799a61369dd76c17adf957ea"
@@ -5526,6 +5535,15 @@ fs-constants@^1.0.0:
   resolved "https://registry.yarnpkg.com/fs-constants/-/fs-constants-1.0.0.tgz#6be0de9be998ce16af8afc24497b9ee9b7ccd9ad"
   integrity sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==
 
+fs-extra@5.0.0:
+  version "5.0.0"
+  resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-5.0.0.tgz#414d0110cdd06705734d055652c5411260c31abd"
+  integrity sha512-66Pm4RYbjzdyeuqudYqhFiNBbCIuI9kgRqLPSHIlXHidW8NIQtVdkM1yeZ4lXwuhbTETv3EUGMNHAAw6hiundQ==
+  dependencies:
+    graceful-fs "^4.1.2"
+    jsonfile "^4.0.0"
+    universalify "^0.1.0"
+
 fs-extra@^4.0.2:
   version "4.0.3"
   resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-4.0.3.tgz#0d852122e5bc5beb453fb028e9c0c9bf36340c94"
@@ -8788,6 +8806,11 @@ neo-async@^2.5.0:
   resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.0.tgz#b9d15e4d71c6762908654b5183ed38b753340835"
   integrity sha512-MFh0d/Wa7vkKO3Y3LlacqAEeHK0mckVqzDieUKTT+KGxi+zIpeVsFxymkIiRpbpDziHc290Xr9A1O4Om7otoRA==
 
+net@^1.0.2:
+  version "1.0.2"
+  resolved "https://registry.yarnpkg.com/net/-/net-1.0.2.tgz#d1757ec9a7fb2371d83cf4755ce3e27e10829388"
+  integrity sha1-0XV+yaf7I3HYPPR1XOPifhCCk4g=
+
 next-tick@1:
   version "1.0.0"
   resolved "https://registry.yarnpkg.com/next-tick/-/next-tick-1.0.0.tgz#ca86d1fe8828169b0120208e3dc8424b9db8342c"
@@ -8839,7 +8862,7 @@ node-fetch@^2.2.0:
   resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.3.0.tgz#1a1d940bbfb916a1d3e0219f037e89e71f8c5fa5"
   integrity sha512-MOd8pV3fxENbryESLgVIeaGKrdl+uaYhCSSVkjeOb/31/njTpcis5aWfdqgNlHIrKOLRbMnfPINPOML2CIFeXA==
 
-node-gyp@^3.8.0:
+node-gyp@3.8.0, node-gyp@^3.8.0:
   version "3.8.0"
   resolved "https://registry.yarnpkg.com/node-gyp/-/node-gyp-3.8.0.tgz#540304261c330e80d0d5edce253a68cb3964218c"
   integrity sha512-3g8lYefrRRzvGeSowdJKAKyks8oUpLEd/DyPV4eMhVlhJ0aNaZqIrNUIPuEWWTAoPqyFkfGrM67MC69baqn6vA==
@@ -8862,6 +8885,18 @@ node-int64@^0.4.0:
   resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
   integrity sha1-h6kGXNs1XTGC2PlM4RGIuCXGijs=
 
+node-libcurl@^1.2.0:
+  version "1.3.3"
+  resolved "https://registry.yarnpkg.com/node-libcurl/-/node-libcurl-1.3.3.tgz#0a2545813e4cfe288313335f94e8559700109eec"
+  integrity sha512-8YqAm45I5piDHjjx2aXGBNB5cVM9dwmQZ2anAJ2JmCXVdCtvB6cZRB1MArU7e67wQLVt1+L/bw0SD4sZiyT08w==
+  dependencies:
+    debug "3.1.0"
+    fs-extra "5.0.0"
+    nan "2.10.0"
+    node-gyp "3.8.0"
+    node-pre-gyp "0.11.0"
+    npmlog "4.1.2"
+
 node-libs-browser@^2.0.0:
   version "2.2.0"
   resolved "https://registry.yarnpkg.com/node-libs-browser/-/node-libs-browser-2.2.0.tgz#c72f60d9d46de08a940dedbb25f3ffa2f9bbaa77"
@@ -9121,7 +9156,7 @@ npm-which@^3.0.1:
     npm-path "^2.0.2"
     which "^1.2.10"
 
-"npmlog@0 || 1 || 2 || 3 || 4", npmlog@^4.0.0, npmlog@^4.0.2:
+"npmlog@0 || 1 || 2 || 3 || 4", npmlog@4.1.2, npmlog@^4.0.0, npmlog@^4.0.2:
   version "4.1.2"
   resolved "https://registry.yarnpkg.com/npmlog/-/npmlog-4.1.2.tgz#08a7f2a8bf734604779a9efa4ad5cc717abb954b"
   integrity sha512-2uUqazuKlTaSI/dC8AzicUck7+IrEaOnN/e0jd3Xtt1KcGpwx30v50mL7oPyr/h9bL3E4aZccVwpwP+5W9Vjkg==
@@ -12066,6 +12101,16 @@ request-promise-native@^1.0.5:
     stealthy-require "^1.1.1"
     tough-cookie "^2.3.3"
 
+request-promise@^4.2.4:
+  version "4.2.4"
+  resolved "https://registry.yarnpkg.com/request-promise/-/request-promise-4.2.4.tgz#1c5ed0d71441e38ad58c7ce4ea4ea5b06d54b310"
+  integrity sha512-8wgMrvE546PzbR5WbYxUQogUnUDfM0S7QIFZMID+J73vdFARkFy+HElj4T+MWYhpXwlLp0EQ8Zoj8xUA0he4Vg==
+  dependencies:
+    bluebird "^3.5.0"
+    request-promise-core "1.1.2"
+    stealthy-require "^1.1.1"
+    tough-cookie "^2.3.3"
+
 request@^2.83.0, request@^2.87.0, request@^2.88.0:
   version "2.88.0"
   resolved "https://registry.yarnpkg.com/request/-/request-2.88.0.tgz#9c2fca4f7d35b592efe57c7f0a55e81052124fef"
-- 
GitLab