|
1 | | -// const WebSocket = require('ws'); |
2 | | -const path = require('path'); |
3 | | -const fs = require('fs-extra'); |
4 | | -const _ = require('lodash'); |
5 | | -const AdmZip = require('adm-zip'); |
6 | | -const tmp = require('tmp'); |
7 | | -const request = require('request'); |
8 | | -const { EventEmitter } = require('events'); |
9 | | -const config = require('./config.js'); |
10 | | - |
11 | | -class ExportManager extends EventEmitter { |
12 | | - constructor(options) { |
13 | | - super(); |
14 | | - this.config = options ? Object.assign({}, config, options) : config; |
15 | | - this.config.url = `http://${this.config.host}:${this.config.port}/api/v2.0/export`; |
16 | | - } |
17 | | - |
18 | | - export(exportConfig, dirPath = '.', unzip = false) { |
19 | | - return new Promise((resolve, reject) => { |
20 | | - const formData = _.cloneDeep(exportConfig.getFormattedConfigs()); |
21 | | - |
22 | | - if (formData.payload) { |
23 | | - formData.payload = fs.createReadStream(formData.payload); |
24 | | - } |
25 | | - |
26 | | - request.post({ |
27 | | - url: this.config.url, |
28 | | - encoding: null, |
29 | | - formData, |
30 | | - }, (err, httpResponse, body) => { |
31 | | - if (err) { |
32 | | - if (err.code === 'ECONNREFUSED') { |
33 | | - const connRefusedError = new Error(`Unable to connect to FusionExport server. Make sure that your server is running on ${err.address}:${err.port}.`); |
34 | | - connRefusedError.name = 'Connection Refused'; |
35 | | - reject(connRefusedError); |
36 | | - return; |
37 | | - } |
38 | | - |
39 | | - reject(err); |
40 | | - return; |
41 | | - } |
42 | | - |
43 | | - if (httpResponse.statusCode === 500) { |
44 | | - let errMsg = body.toString(); |
45 | | - |
46 | | - try { |
47 | | - errMsg = JSON.parse(errMsg).error; |
48 | | - } catch (e) { |
49 | | - // continue regardless of error |
50 | | - } |
51 | | - |
52 | | - const serverError = new Error(errMsg); |
53 | | - serverError.name = 'Server Error'; |
54 | | - reject(serverError); |
55 | | - return; |
56 | | - } |
57 | | - |
58 | | - if (httpResponse.statusCode !== 200) { |
59 | | - reject(new Error(body.toString())); |
60 | | - return; |
61 | | - } |
62 | | - |
63 | | - const zipFile = ExportManager.saveZip(body); |
64 | | - resolve(ExportManager.saveExportedFiles(zipFile, dirPath, unzip)); |
65 | | - }); |
66 | | - }); |
67 | | - } |
68 | | - |
69 | | - static saveZip(content) { |
70 | | - const zipFile = tmp.fileSync({ postfix: '.zip' }); |
71 | | - fs.writeFileSync(zipFile.name, content); |
72 | | - return zipFile.name; |
73 | | - } |
74 | | - |
75 | | - static saveExportedFiles(exportedFile, dirPath = '.', unzip = false) { |
76 | | - if (!exportedFile) { |
77 | | - throw new Error('Exported files are missing'); |
78 | | - } |
79 | | - |
80 | | - fs.ensureDirSync(dirPath); |
81 | | - |
82 | | - const savedFiles = []; |
83 | | - |
84 | | - if (unzip) { |
85 | | - const zip = AdmZip(exportedFile); |
86 | | - zip.extractAllTo(dirPath, true); |
87 | | - const extractedFiles = zip.getEntries().map(entry => path.resolve(dirPath, entry.entryName)); |
88 | | - savedFiles.push(...extractedFiles); |
89 | | - } else { |
90 | | - const filename = 'fusioncharts-export.zip'; |
91 | | - const savedFile = path.resolve(dirPath, filename); |
92 | | - fs.copySync(exportedFile, savedFile); |
93 | | - savedFiles.push(savedFile); |
94 | | - } |
95 | | - |
96 | | - return savedFiles; |
97 | | - } |
98 | | -} |
99 | | - |
100 | | -module.exports = ExportManager; |
| 1 | +// const WebSocket = require('ws'); |
| 2 | +const path = require("path"); |
| 3 | +const fs = require("fs-extra"); |
| 4 | +const _ = require("lodash"); |
| 5 | +const AdmZip = require("adm-zip"); |
| 6 | +const tmp = require("tmp"); |
| 7 | +const request = require("request"); |
| 8 | +const { EventEmitter } = require("events"); |
| 9 | +const config = require("./config.js"); |
| 10 | + |
| 11 | +class ExportManager extends EventEmitter { |
| 12 | + constructor(options) { |
| 13 | + super(); |
| 14 | + this.config = options ? Object.assign({}, config, options) : config; |
| 15 | + this.config.url = `http://${this.config.host}:${this.config.port}/api/v2.0/export`; |
| 16 | + } |
| 17 | + |
| 18 | + export(exportConfig, dirPath = ".", unzip = false) { |
| 19 | + return new Promise((resolve, reject) => { |
| 20 | + const formData = _.cloneDeep(exportConfig.getFormattedConfigs()); |
| 21 | + if (formData.payload) { |
| 22 | + formData.payload = fs.createReadStream(formData.payload); |
| 23 | + } |
| 24 | + ExportManager.sendToServer(this.config.url, formData).then((content) => { |
| 25 | + const zipFile = ExportManager.saveZip(content); |
| 26 | + const files = ExportManager.saveExportedFiles(zipFile, dirPath, unzip); |
| 27 | + resolve(files); |
| 28 | + }).catch(err => reject(err)); |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + exportAsStream(exportConfig) { |
| 33 | + return new Promise((resolve, reject) => { |
| 34 | + const formData = _.cloneDeep(exportConfig.getFormattedConfigs()); |
| 35 | + if (formData.payload) { |
| 36 | + formData.payload = fs.createReadStream(formData.payload); |
| 37 | + } |
| 38 | + ExportManager.sendToServer(this.config.url, formData).then((content) => { |
| 39 | + const streams = ExportManager.unzipAsStream(content); |
| 40 | + resolve(streams); |
| 41 | + }).catch(err => reject(err)); |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + static sendToServer(serverUrl, formData) { |
| 46 | + return new Promise((resolve, reject) => { |
| 47 | + request.post( |
| 48 | + { |
| 49 | + url: serverUrl, |
| 50 | + encoding: null, |
| 51 | + formData, |
| 52 | + }, |
| 53 | + (err, httpResponse, body) => { |
| 54 | + if (err) { |
| 55 | + if (err.code === "ECONNREFUSED") { |
| 56 | + const connRefusedError = new Error( |
| 57 | + `Unable to connect to FusionExport server. Make sure that your server is running on ${err.address}:${err.port}.` |
| 58 | + ); |
| 59 | + connRefusedError.name = "Connection Refused"; |
| 60 | + reject(connRefusedError); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + reject(err); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + if (httpResponse.statusCode === 500) { |
| 69 | + let errMsg = body.toString(); |
| 70 | + |
| 71 | + try { |
| 72 | + errMsg = JSON.parse(errMsg).error; |
| 73 | + } catch (e) { |
| 74 | + // continue regardless of error |
| 75 | + } |
| 76 | + |
| 77 | + const serverError = new Error(errMsg); |
| 78 | + serverError.name = "Server Error"; |
| 79 | + reject(serverError); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + if (httpResponse.statusCode !== 200) { |
| 84 | + reject(new Error(body.toString())); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + resolve(body); |
| 89 | + }, |
| 90 | + ); |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + static unzipAsStream(buf) { |
| 95 | + const zip = new AdmZip(buf); |
| 96 | + const zipEntries = zip.getEntries(); |
| 97 | + |
| 98 | + const contents = {}; |
| 99 | + // eslint-disable-next-line no-plusplus |
| 100 | + for (let i = 0; i < zipEntries.length; i++) { |
| 101 | + const file = zip.readFile(zipEntries[i]); |
| 102 | + contents[zipEntries[i].entryName] = file; |
| 103 | + } |
| 104 | + return contents; |
| 105 | + } |
| 106 | + |
| 107 | + static saveZip(content) { |
| 108 | + const zipFile = tmp.fileSync({ postfix: ".zip" }); |
| 109 | + fs.writeFileSync(zipFile.name, content); |
| 110 | + return zipFile.name; |
| 111 | + } |
| 112 | + |
| 113 | + static saveExportedFiles(exportedFile, dirPath = ".", unzip = false) { |
| 114 | + if (!exportedFile) { |
| 115 | + throw new Error("Exported files are missing"); |
| 116 | + } |
| 117 | + |
| 118 | + fs.ensureDirSync(dirPath); |
| 119 | + |
| 120 | + const savedFiles = []; |
| 121 | + |
| 122 | + if (unzip) { |
| 123 | + const zip = AdmZip(exportedFile); |
| 124 | + zip.extractAllTo(dirPath, true); |
| 125 | + const extractedFiles = zip |
| 126 | + .getEntries() |
| 127 | + .map(entry => path.resolve(dirPath, entry.entryName)); |
| 128 | + savedFiles.push(...extractedFiles); |
| 129 | + } else { |
| 130 | + const filename = 'fusioncharts-export.zip'; |
| 131 | + const savedFile = path.resolve(dirPath, filename); |
| 132 | + fs.copySync(exportedFile, savedFile); |
| 133 | + savedFiles.push(savedFile); |
| 134 | + } |
| 135 | + |
| 136 | + return savedFiles; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +module.exports = ExportManager; |
0 commit comments