forked from lykmapipo/xls2xform-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
130 lines (106 loc) · 3.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
//dependencies
var path = require('path');
var fs = require('fs');
var _ = require('lodash');
var async = require('async');
var xls2xform = require('xls2xform');
var multer = require('multer');
var tmp = require('tmp');
tmp.setGracefulCleanup();
module.exports = function(options) {
//normalize options
options = options || {};
//default options
var defaults = {
//field name to be used to extract xlsform from multiparty request
fieldName: 'xlsform',
//file uploading destination to be used with multer
dest: tmp.dirSync({
//enable recursively removes the created temporary directory,
//even when it's not empty
//@see https://www.npmjs.com/package/tmp#options
unsafeCleanup: true
}).name
};
//merge options with defaults
options = _.merge(defaults, options);
//prepare multer upload middleware
var upload = multer(options);
/**
* @description parse request for the xlsform
* @param {Object} request valid http request
* @param {Object} response valid http response
* @param {Function} next next middleware to invoke
*/
function xlsFormBodyParser(request, response, next) {
//parse xlsform content from request body
//if sent as base64 encode
var isBase64 =
_.isPlainObject(request.body[options.fieldName]) && !request.file;
if (isBase64) {
var file = request.body[options.fieldName];
//normalize request file
request.file = {};
//set full path of generated file
if (!request.file.path) {
request.file.path =
path.join(options.dest, file.name);
}
//write file to path for convertion
var data = new Buffer(file.base64, 'base64');
//write file in temp
fs.writeFile(request.file.path, data, next);
}
//handle multiparty request
else {
async.waterfall([
function readFile(after) {
fs.readFile(request.file.path, after);
},
function updateFileDetails(buffer, after) {
//extend request body with xlsform file details
var file = {};
file.base64 = buffer.toString('base64');
file.name = request.file.originalname;
file.type = request.file.mimetype;
file.size = request.file.size;
//extend request body with file details
request.body[options.fieldName] = file;
//reset file path
request.file.path =
path.join(request.file.destination, request.file.originalname);
fs.writeFile(request.file.path, buffer, after);
}
], next);
}
}
/**
* @description extend request with xls2xform convertor
* @param {Object} request valid http request
* @param {Object} response valid http response
* @param {Function} next next middleware to invoke
*/
function xform(request, response, next) {
//convert xls form to xform
xls2xform(request.file.path, function(error, xform) {
if (error) {
next(error);
} else {
//extend request body with xform
request.body[options.fieldName] =
_.merge(request.body[options.fieldName], {
xform: xform
});
next();
}
});
}
//return middlwares stack to be used
//to parse and extend request
return [
upload.single(options.fieldName),
xlsFormBodyParser,
xform
];
};