forked from joooye34/gulp-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (57 loc) · 1.74 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
'use strict';
var _ = require('lodash');
var through = require('through2');
var PluginError = require('plugin-error');
var urllib = require('urllib');
var formstream = require('formstream');
var path = require('path');
// consts
var PLUGIN_NAME = 'gulp-upload';
var defaultOptions = {
method: 'post',
timeout: 5000
}
// exporting the plugin main function
module.exports = function(options) {
console.log('in the gulpupload')
if (!options) {
options = {};
}
if (!options.server) {
throw new PluginError(PLUGIN_NAME, 'Could not find server to upload.');
}
var callback = options.callback || function(){};
return through.obj(async function(file, enc, next) {
if (!file.isBuffer()) return next();
var self = this;
var content = file.contents.toString();
var form = formstream();
var data = options.data || {};
for (var key in data) {
var value = data[key];
if (typeof value === 'function') {
form.field(key, value(file));
} else {
form.field(key, data[key]);
}
}
var fileinputname = options.fileinputname || "file";
form.file(fileinputname, file.path);
var inputOptions = _.omit(options, 'server', 'data', 'fileinputname')
var requestOptions = Object.assign({}, defaultOptions, inputOptions, {
headers: form.headers(options.headers),
stream: form
})
// const { data: resData, res } = await urllib.request(options.server, requestOptions);
// console.log('resData', resData)
// console.log('res', res)
// callback(null, resData, res);
// self.push(file);
// next();
urllib.request(options.server, requestOptions, function (err, data, res) {
callback(err, data, res);
self.push(file);
next();
});
});
};