Bläddra i källkod

Move endpoints zip export from server to client

Sergiu Miclea 6 år sedan
förälder
incheckning
f822bcb277
7 ändrade filer med 58 tillägg och 95 borttagningar
  1. 2 1
      package.json
  2. 0 43
      server/api/DownloadZipApi.js
  3. 0 2
      server/api/router.js
  4. 8 21
      src/stores/EndpointStore.js
  5. 0 20
      src/types/ZipContent.js
  6. 2 2
      webpack.config.js
  7. 46 6
      yarn.lock

+ 2 - 1
package.json

@@ -58,7 +58,6 @@
   },
   "dependencies": {
     "@webpack-blocks/webpack2": "^0.4.0",
-    "adm-zip": "^0.4.13",
     "ansi-to-html": "^0.6.12",
     "autobind-decorator": "^2.1.0",
     "axios": "^0.18.0",
@@ -79,10 +78,12 @@
     "cross-env": "^5.0.5",
     "express": "^4.16.1",
     "file-loader": "^1.1.5",
+    "file-saver": "^2.0.2",
     "fs": "^0.0.1-security",
     "history": "^4.7.2",
     "html-webpack-plugin": "^2.30.1",
     "js-cookie": "^2.1.4",
+    "jszip": "^3.2.2",
     "lodash": "^4.17.13",
     "mobx": "^3.6.1",
     "mobx-react": "^4.4.2",

+ 0 - 43
server/api/DownloadZipApi.js

@@ -1,43 +0,0 @@
-/*
-Copyright (C) 2019  Cloudbase Solutions SRL
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU Affero General Public License as
-published by the Free Software Foundation, either version 3 of the
-License, or (at your option) any later version.
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU Affero General Public License for more details.
-You should have received a copy of the GNU Affero General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
-*/
-
-// @flow
-import AdmZip from 'adm-zip'
-import stream from 'stream'
-
-import type { ZipContent } from '../../src/types/ZipContent'
-
-export default (router: any) => {
-  router.post('/download-zip', (req, res) => {
-    try {
-      let contents: ZipContent[] = req.body.contents
-      if (!contents || !contents.length || !contents[0].filename || typeof contents[0].content !== 'string') {
-        throw new Error()
-      }
-      let zip = new AdmZip()
-      contents.forEach(content => {
-        zip.addFile(content.filename, Buffer.alloc(content.content.length, content.content))
-      })
-      let zipBuffer = zip.toBuffer()
-      let readStream = new stream.PassThrough()
-      readStream.end(zipBuffer)
-      res.set('Content-Disposition', 'attachment; filename=contents.zip')
-      res.set('Content-Type', 'text/plain')
-      readStream.pipe(res)
-    } catch (err) {
-      console.error(err)
-      res.status(500).json({ error: { message: 'Invalid request body for download zip API' } })
-    }
-  })
-}

+ 0 - 2
server/api/router.js

@@ -18,7 +18,6 @@ import express from 'express'
 import bodyParser from 'body-parser'
 
 import LogosApi from './LogosApi'
-import DownloadZipApi from './DownloadZipApi'
 import ConfigApi from './ConfigApi'
 
 import packageJson from '../../package.json'
@@ -33,7 +32,6 @@ router.get('/version', (req, res) => {
 })
 
 ConfigApi(router)
-DownloadZipApi(router)
 LogosApi(router)
 
 export default router

+ 8 - 21
src/stores/EndpointStore.js

@@ -15,11 +15,11 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 // @flow
 
 import { observable, runInAction, action } from 'mobx'
+import JSZip from 'jszip'
+import { saveAs } from 'file-saver'
 
 import type { Endpoint, Validation, StorageBackend, Storage } from '../types/Endpoint'
-import type { ZipContent } from '../types/ZipContent'
 
-import apiCaller from '../utils/ApiCaller'
 import notificationStore from './NotificationStore'
 import EndpointSource from '../sources/EndpointSource'
 
@@ -151,29 +151,16 @@ class EndpointStore {
   }
 
   @action async exportToZip(endpoints: Endpoint[]): Promise<void> {
+    const zip = new JSZip()
+
     await Promise.all(endpoints.map(async endpoint => {
       let connectionInfo = await EndpointSource.getConnectionInfo(endpoint)
       endpoint.connection_info = connectionInfo
+      zip.file(`${endpoint.name}.endpoint`, JSON.stringify(endpoint))
     }))
-    let zipContents: ZipContent[] = endpoints.map(endpoint => ({
-      filename: `${endpoint.name}.endpoint`,
-      content: JSON.stringify(endpoint),
-    }))
-    let response = await apiCaller.send({
-      url: '/api/download-zip',
-      data: { contents: zipContents },
-      method: 'POST',
-      responseType: 'blob',
-    })
-    const url = window.URL.createObjectURL(new Blob([response.data]))
-    const link = document.createElement('a')
-    link.href = url
-    link.setAttribute('download', 'coriolis-endpoints.zip')
-    if (document.body) {
-      document.body.appendChild(link)
-    }
-    link.click()
-    link.remove()
+
+    let content = await zip.generateAsync({ type: 'blob' })
+    saveAs(content, 'coriolis-endpoints.zip')
   }
 
   @action setConnectionInfo(connectionInfo: $PropertyType<Endpoint, 'connection_info'>) {

+ 0 - 20
src/types/ZipContent.js

@@ -1,20 +0,0 @@
-/*
-Copyright (C) 2019  Cloudbase Solutions SRL
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU Affero General Public License as
-published by the Free Software Foundation, either version 3 of the
-License, or (at your option) any later version.
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU Affero General Public License for more details.
-You should have received a copy of the GNU Affero General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
-*/
-
-// @flow
-
-export type ZipContent = {
-  filename: string,
-  content: string,
-}

+ 2 - 2
webpack.config.js

@@ -16,7 +16,7 @@ const path = require('path')
 const HtmlWebpackPlugin = require('html-webpack-plugin')
 const splitVendor = require('webpack-blocks-split-vendor')
 const happypack = require('webpack-blocks-happypack')
-const ProgressPlugin = require('webpack/lib/ProgressPlugin')
+// const ProgressPlugin = require('webpack/lib/ProgressPlugin')
 
 const {
   addPlugins, createConfig, entryPoint, env, setOutput,
@@ -83,7 +83,7 @@ const config = createConfig([
       filename: 'index.html',
       template: path.join(process.cwd(), 'public/index.html'),
     }),
-    new ProgressPlugin(),
+    // new ProgressPlugin(),
   ]),
   happypack([
     babel(),

+ 46 - 6
yarn.lock

@@ -317,11 +317,6 @@ adal-node@^0.1.25:
     xmldom ">= 0.1.x"
     xpath.js "~1.0.5"
 
-adm-zip@^0.4.13:
-  version "0.4.13"
-  resolved "https://registry.yarnpkg.com/adm-zip/-/adm-zip-0.4.13.tgz#597e2f8cc3672151e1307d3e95cddbc75672314a"
-  integrity sha512-fERNJX8sOXfel6qCBCMPvZLzENBEhZTzKqg6vrOW5pvoEaQuJhRU4ndTAh6lHOxn1I6jnz2NHra56ZODM751uw==
-
 agent-base@4, agent-base@^4.1.0:
   version "4.2.1"
   resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.2.1.tgz#d89e5999f797875674c07d87f260fc41e83e8ca9"
@@ -4121,6 +4116,11 @@ file-loader@^1.1.5:
     loader-utils "^1.0.2"
     schema-utils "^0.3.0"
 
+file-saver@^2.0.2:
+  version "2.0.2"
+  resolved "https://registry.yarnpkg.com/file-saver/-/file-saver-2.0.2.tgz#06d6e728a9ea2df2cce2f8d9e84dfcdc338ec17a"
+  integrity sha512-Wz3c3XQ5xroCxd1G8b7yL0Ehkf0TC9oYC6buPFkNnU9EnaPlifeAFCyCh+iewXTyFRcg0a6j3J7FmJsIhlhBdw==
+
 filename-regex@^2.0.0:
   version "2.0.1"
   resolved "https://registry.yarnpkg.com/filename-regex/-/filename-regex-2.0.1.tgz#c1c4b9bee3e09725ddb106b75c1e301fe2f18b26"
@@ -4947,6 +4947,11 @@ ignore@^3.3.3:
   version "3.3.5"
   resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.3.5.tgz#c4e715455f6073a8d7e5dae72d2fc9d71663dba6"
 
+immediate@~3.0.5:
+  version "3.0.6"
+  resolved "https://registry.yarnpkg.com/immediate/-/immediate-3.0.6.tgz#9db1dbd0faf8de6fbe0f5dd5e56bb606280de69b"
+  integrity sha1-nbHb0Pr43m++D13V5Wu2BigN5ps=
+
 immutable@^3.8.1:
   version "3.8.2"
   resolved "https://registry.yarnpkg.com/immutable/-/immutable-3.8.2.tgz#c2439951455bb39913daf281376f1530e104adf3"
@@ -5931,6 +5936,16 @@ jsx-ast-utils@^2.0.1:
   dependencies:
     array-includes "^3.0.3"
 
+jszip@^3.2.2:
+  version "3.2.2"
+  resolved "https://registry.yarnpkg.com/jszip/-/jszip-3.2.2.tgz#b143816df7e106a9597a94c77493385adca5bd1d"
+  integrity sha512-NmKajvAFQpbg3taXQXr/ccS2wcucR1AZ+NtyWp2Nq7HHVsXhcJFR8p0Baf32C2yVvBylFWVeKf+WI2AnvlPhpA==
+  dependencies:
+    lie "~3.3.0"
+    pako "~1.0.2"
+    readable-stream "~2.3.6"
+    set-immediate-shim "~1.0.1"
+
 just-extend@^1.1.26:
   version "1.1.27"
   resolved "https://registry.yarnpkg.com/just-extend/-/just-extend-1.1.27.tgz#ec6e79410ff914e472652abfa0e603c03d60e905"
@@ -6010,6 +6025,13 @@ levn@^0.3.0, levn@~0.3.0:
     prelude-ls "~1.1.2"
     type-check "~0.3.2"
 
+lie@~3.3.0:
+  version "3.3.0"
+  resolved "https://registry.yarnpkg.com/lie/-/lie-3.3.0.tgz#dcf82dee545f46074daf200c7c1c5a08e0f40f6a"
+  integrity sha512-UaiMJzeWRlEujzAuw5LokY1L5ecNQYZKfmyZ9L7wDHb/p5etKaxXhohBcrw0EYby+G/NA52vRSN4N39dxHAIwQ==
+  dependencies:
+    immediate "~3.0.5"
+
 listenercount@~1.0.1:
   version "1.0.1"
   resolved "https://registry.yarnpkg.com/listenercount/-/listenercount-1.0.1.tgz#84c8a72ab59c4725321480c975e6508342e70937"
@@ -7181,6 +7203,11 @@ pako@~0.2.0:
   version "0.2.9"
   resolved "https://registry.yarnpkg.com/pako/-/pako-0.2.9.tgz#f3f7522f4ef782348da8161bad9ecfd51bf83a75"
 
+pako@~1.0.2:
+  version "1.0.10"
+  resolved "https://registry.yarnpkg.com/pako/-/pako-1.0.10.tgz#4328badb5086a426aa90f541977d4955da5c9732"
+  integrity sha512-0DTvPVU3ed8+HNXOu5Bs+o//Mbdj9VNQMUOe9oKCwh8l0GNwpTDMKCWbRjgtD291AWnkAgkqA/LOnQS8AmS1tw==
+
 param-case@2.1.x:
   version "2.1.1"
   resolved "https://registry.yarnpkg.com/param-case/-/param-case-2.1.1.tgz#df94fd8cf6531ecf75e6bef9a0858fbc72be2247"
@@ -8253,6 +8280,19 @@ readable-stream@~2.1.5:
     string_decoder "~0.10.x"
     util-deprecate "~1.0.1"
 
+readable-stream@~2.3.6:
+  version "2.3.7"
+  resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.7.tgz#1eca1cf711aef814c04f62252a36a62f6cb23b57"
+  integrity sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==
+  dependencies:
+    core-util-is "~1.0.0"
+    inherits "~2.0.3"
+    isarray "~1.0.0"
+    process-nextick-args "~2.0.0"
+    safe-buffer "~5.1.1"
+    string_decoder "~1.1.1"
+    util-deprecate "~1.0.1"
+
 readdirp@^2.0.0:
   version "2.1.0"
   resolved "https://registry.yarnpkg.com/readdirp/-/readdirp-2.1.0.tgz#4ed0ad060df3073300c48440373f72d1cc642d78"
@@ -8783,7 +8823,7 @@ set-blocking@^2.0.0, set-blocking@~2.0.0:
   version "2.0.0"
   resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7"
 
-set-immediate-shim@^1.0.1:
+set-immediate-shim@^1.0.1, set-immediate-shim@~1.0.1:
   version "1.0.1"
   resolved "https://registry.yarnpkg.com/set-immediate-shim/-/set-immediate-shim-1.0.1.tgz#4b2b1b27eb808a9f8dcc481a58e5e56f599f3f61"