-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
143 lines (141 loc) · 4.43 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
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict';
var https = require('https');
var http = require('http');
var stream = require('readable-stream');
var url = require('url');
var qs = require('querystring');
var debug = require('debug')('cartodb-uploader');
var FormData = require('form-data');
var geojsonStream = require('geojson-stream');
var once = require('once');
var zlib = require('zlib');
module.exports = exports = cartodbUploader;
function getUploadState(credentials, destination, callback) {
var rawUrl = createBaseUrl(credentials) + '/api/v1/imports/' + destination + '?' + qs.stringify({
api_key: credentials.key
});
https.get(rawUrl, function (res) {
var data = [];
debug(res.statusCode);
res.on('error', callback)
.on('data', function (d){
debug(d.toString());
data.push(d);
})
.on('end', function (){
var out = Buffer.concat(data).toString();
if (res.statusCode > 299) {
debug(res.headers);
callback(out);
} else {
try {
out = JSON.parse(out);
} catch(e) {
return callback(e);
}
if (out.state === 'complete') {
callback(null, out);
} else if (out.state === 'failure') {
callback(out);
} else {
setTimeout(getUploadState, 100, credentials, destination, callback);
}
}
});
});
}
function noop(){}
function createBaseUrl(credentials) {
if (!credentials.domain && !credentials.subdomainless) {
return `https://${credentials.user}.carto.com`;
}
if (credentials.domain) {
if (credentials.subdomainless) {
return `https://${credentials.domain}/user/${credentials.user}`;
} else {
return `https://${credentials.user}.${credentials.domain}`;
}
} else if (credentials.subdomainless) {
return `https://carto.com/user/${credentials.user}`;
}
}
function cartodbUploader(credentials, fileName, callback) {
debug('starting upload');
var compress = false;
// if (['zip', '.gz', 'bz2', 'tgz', 'kmz', 'lsx', 'ods'].indexOf(fileName.slice(-3)) === -1) {
// fileName = fileName += '.gz';
// compress = true;
// }
callback = once(callback || noop);
var passthrough = compress ? zlib.createGzip() : new stream.PassThrough();
passthrough.on('error', callback);
var form = new FormData();
form.append('file', passthrough, {
filename: fileName,
contentType: 'application/octet-stream'
});
form.on('error', callback);
var fullUrl = createBaseUrl(credentials) + '/api/v1/imports?';
//var fullUrl = 'http://cartodb.calvin:8080/';
fullUrl += qs.stringify({
api_key: credentials.key
});
var datas = [];
var len = 0;
form.pipe(new stream.Transform({
transform: function (chunk, _, next){
datas.push(chunk);
len += chunk.length;
next();
},
flush: function (done) {
var parsedUrl = url.parse(fullUrl);
parsedUrl.method = 'POST';
parsedUrl.headers = form.getHeaders({
'content-length': len
});
debug(parsedUrl);
var req = https.request(parsedUrl, function (res) {
var data = [];
debug(res.statusCode);
res
.on('error', callback)
.on('data', function (d){
debug(d.toString());
data.push(d);
})
.on('end', function (){
var out = Buffer.concat(data).toString().trim();
if (res.statusCode > 299) {
debug(res.headers);
callback(out || new Error(http.STATUS_CODES[res.statusCode]));
} else {
try {
out = JSON.parse(out);
} catch(e) {
return callback(e);
}
if (!out.success) {
return callback(new Error('failure'));
}
getUploadState(credentials, out.item_queue_id, callback);
}
});
}).on('error', callback);
datas.forEach(function (item) {
req.write(item);
});
req.end();
}
}));
return passthrough;
}
exports.geojson = uploadGeoJson;
function uploadGeoJson(credentials, destination, callback) {
debug('starting geojson');
callback = once(callback);
var inStream = geojsonStream.stringify();
inStream.on('error', callback)
.pipe(cartodbUploader(credentials, destination + '.geojson', callback));
return inStream;
}