/
http-send-multipart-form.js
202 lines (169 loc) · 5.87 KB
/
http-send-multipart-form.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// require in libs
var mustache = require('mustache'),
request = require('request'),
formData = require('form-data'),
fs = require('fs');
const fileType = require('file-type');
// require in libs
var fileData = ""; // initializing file
var debug = false;
module.exports = function (RED) {
function httpSendMultipart(n) {
// Setup node
RED.nodes.createNode(this, n);
var node = this;
var nodeUrl = n.url;
var isTemplatedUrl = (nodeUrl || "").indexOf("{{") != -1;
var isTemplatedUrl = (nodeUrl || "").indexOf("{{") != -1;
this.ret = n.ret || "txt"; // default return type is text
if (RED.settings.httpRequestTimeout) {
this.reqTimeout = parseInt(RED.settings.httpRequestTimeout) || 60000;
} else {
this.reqTimeout = 60000;
}
// 1) Process inputs to Node
this.on("input", function (msg) {
// TODO: add ability to select other input types (not just files)
if(msg.debug !== undefined) {
debug = msg.debug;
}
if(debug) {
if(msg.payload.formOptions !== undefined) {
for (x in msg.payload.formOptions) {
console.log(x + "->" + msg.payload.formOptions[x]);
}
}
}
if (!msg.payload.fileData) {
// throw an error if no file
node.warn(RED._("Error: no file found to send."));
msg.error = "File was not defined";
msg.statusCode = 400;
node.send(msg); // TODO: make sure this escapes entirely; need better error-handling here
} else {
fileData = msg.payload.fileData;
}
node.status({
fill: "blue",
shape: "dot",
text: "Sending multipart request..."
});
var url = nodeUrl; // TODO add ability to take this from the settings.js config file
if (isTemplatedUrl) {
url = mustache.render(nodeUrl, msg);
}
if (!url) {
node.error(RED._("httpSendMultipart.errors.no-url"), msg);
node.status({
fill: "red",
shape: "ring",
text: (RED._("httpSendMultipart.errors.no-url"))
});
return;
}
// Add auth if it exists
if (this.credentials && this.credentials.user) {
var urlTail = url.substring(url.indexOf('://') + 3); // hacky but it works. don't judge me
var username = this.credentials.user,
password = this.credentials.password;
url = 'https://' + username + ':' + password + '@' + urlTail;
}
var FormData = require('form-data');
var formData = new FormData();
var buffer, fileName = 'default', fileMime = 'unknown', fileDataType;
if(msg.payload.fileName !== undefined) {
fileName = msg.payload.fileName;
}
fileDataType = n.filetype;
if(msg.payload.fileDataType !== undefined) {
fileDataType = msg.payload.fileDataType;
}
if(debug) console.log("fileDataType: "+fileDataType);
if (fileDataType !== 'base64' && fileDataType !== 'binary'){
node.error(RED._("node-red-contrib-send-form .errors.no-file-data-type") + " ["+fileDataType+"]", msg); //
node.status({
fill: "red",
shape: "ring",
text: (RED._("node-red-contrib-send-form .errors.no-file-data-type") + " ["+fileDataType+"]")
});
return;
}
if(debug) console.log("msg.payload.fileData " +msg.payload.fileData.length);
if(msg.payload.fileData !== undefined) {
{
if (fileDataType === 'base64')
buffer = Buffer.from(msg.payload.fileData, 'base64');
else
buffer = msg.payload.fileData;
}
var fileTypeInfo = fileType(buffer);
fileMime = fileTypeInfo.mime;
fileName += "."+fileTypeInfo.ext;
if(debug) console.log(fileType(buffer));
if(debug) console.log(url);
if(msg.payload.formOptions !== undefined) {
for (x in msg.payload.formOptions) {
if(debug) console.log(x + "->" + msg.payload.formOptions[x]);
formData.append(x, msg.payload.formOptions[x]);
}
}
var formFileField = msg.payload.formFileField;
if(debug) console.log(formFileField + " "+msg.payload.fileData.length+" "+buffer.length);
if(debug) console.log('contentType '+ fileMime + ' filename '+ fileName);
formData.append(formFileField, buffer, { // 'photo'
'contentType': fileMime, //'image/png',
'filename': fileName //'nb-3x256.png'
});
//formData.append('chat_id', '457840189');
//formData.append('caption', 'photo251');
//formData.append('photo', buffer, {'contentType': 'image/png', 'filename': 'nb-3x256.png'});
// formData.submit('https://api.telegram.org/bot<BOT_ID>/sendPhoto',
formData.submit(url,
function (err, res) { // StarBot
if (err || !res) {
// node.error(RED._("httpSendMultipart.errors.no-url"), msg);
var statusText = "Unexpected error";
if (err) {
statusText = err;
} else if (!resp) {
statusText = "No response object";
}
node.status({
fill: "red",
shape: "ring",
text: statusText
});
} else {
res.resume();
msg.payload = res;
node.send(msg);
if(debug) console.log("msg.statusCode "+msg.payload.statusCode);
if(msg.payload.statusCode !== 200){
if(debug) console.log("msg.statusCode "+msg.payload.statusCode);
node.status({
fill: "red",
shape: "ring",
text: (RED._("node-red-contrib-send-form.errors.error-status-code") + " ["+msg.payload.statusCode+"]")
});
} else {
if(debug) console.log("msg.statusCode "+msg.payload.statusCode);
node.status({
});
}
}
});
} //else
}); // end of on.input
} // end of httpSendMultipart fxn
// Register the Node
RED.nodes.registerType("http-send-multipart-form", httpSendMultipart, {
credentials: {
user: {
type: "text"
},
password: {
type: "password"
}
}
});
}; // end module.exports