-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathomnivore.js
58 lines (50 loc) · 2.03 KB
/
omnivore.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
var fs = require('fs');
var uploadLimits = require('mapbox-upload-limits');
var prettyBytes = require('pretty-bytes');
var path = require('path');
var queue = require('queue-async');
var invalid = require('../invalid');
var sniffer = require('@mapbox/mapbox-file-sniff');
var getMetadata = require('@mapbox/mapnik-omnivore').digest;
module.exports = function validateOmnivore(opts, callback) {
var dir = path.dirname(opts.filepath);
var ext = path.extname(opts.filepath);
var base = path.basename(opts.filepath, ext);
var limits;
var files = ext === '.shp' ?
[ opts.filepath, path.join(dir, base + '.dbf') ] :
[ opts.filepath ];
sniffer.fromFile(opts.filepath, function(err, fileinfo) {
if (err) return callback(err);
if (fileinfo.type === 'tif') limits = opts.limits || uploadLimits.tif;
else if (fileinfo.type === 'csv') limits = opts.limits || uploadLimits.csv;
else if (fileinfo.type === 'geojson') limits = opts.limits || uploadLimits.geojson;
else if (fileinfo.type === 'kml') limits = opts.limits || uploadLimits.kml;
else limits = opts.limits || uploadLimits.omnivoreOther;
getMetadata(opts.filepath, function(err, metadata) {
if (err) return callback(err);
// Stopgap while only 8 bit TIFFs are supported
if (fileinfo.type === 'kml') {
var layers = metadata.layers.length;
if (layers > uploadLimits.kml.layers) {
return callback(invalid('%d layers found. Maximum of %d layers allowed.', layers, uploadLimits.kml.layers));
}
}
var q = queue();
files.forEach(function(f) {
q.defer(fs.stat.bind(fs), f);
});
q.awaitAll(function(err, stats) {
if (err) return callback(err);
var size = stats.reduce(function(memo, stat) {
memo += stat.size;
return memo;
}, 0);
if (size > limits.max_filesize) {
return callback(invalid('File is larger than ' + prettyBytes(limits.max_filesize) + '. Too big to process.'));
}
callback();
});
});
});
};