Skip to content

Commit ebd88ba

Browse files
committed
ExportAsStream implemented!
1 parent 521580e commit ebd88ba

File tree

6 files changed

+182
-116
lines changed

6 files changed

+182
-116
lines changed

.eslintrc.json

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
{
2-
"extends": "airbnb-base",
3-
"rules": {
4-
"no-underscore-dangle": "off",
5-
"no-console": [
6-
"warn",
7-
{
8-
"allow": ["warn", "error"]
9-
}
10-
]
11-
}
1+
{
2+
"extends": "airbnb-base",
3+
"rules": {
4+
"no-underscore-dangle": "off",
5+
"no-console": [
6+
"warn",
7+
{
8+
"allow": ["warn", "error"]
9+
}
10+
],
11+
"linebreak-style": 0,
12+
"quotes":"off"
13+
}
1214
}

example/export_as_stream.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Exporting a chart with best quality
2+
3+
const path = require('path');
4+
5+
// Require FusionExport
6+
const { ExportManager, ExportConfig } = require('../');
7+
8+
// Instantiate ExportManager
9+
const exportManager = new ExportManager();
10+
11+
// Instantiate ExportConfig and add the required configurations
12+
const exportConfig = new ExportConfig();
13+
14+
exportConfig.set('chartConfig', path.join(__dirname, 'resources', 'single.json'));
15+
exportConfig.set('quality', 'best');
16+
17+
// provide the export config
18+
exportManager.exportAsStream(exportConfig, '.', true).then((exportedFiles) => {
19+
Object.keys(exportedFiles).forEach((key) => {
20+
console.log(key, exportedFiles[key]);
21+
});
22+
}).catch((err) => {
23+
console.log(err);
24+
});

example/run_all.js

100755100644
File mode changed.

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"glob": "^7.1.2",
3232
"jsdom": "^11.5.1",
3333
"lodash": "^4.17.15",
34-
"nodemailer": "^6.3.0",
34+
"nodemailer": "^6.3.1",
3535
"request": "^2.88.0",
3636
"tmp": "0.0.33",
3737
"winston": "^2.4.0"

src/ExportManager.js

Lines changed: 140 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,140 @@
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

Comments
 (0)