2011-01-05 02:07:27 +08:00
|
|
|
var fs = require('fs'),
|
|
|
|
netlib = require('./netlib'),
|
|
|
|
url = require('url'),
|
|
|
|
path = require('path'),
|
2011-01-25 06:23:18 +08:00
|
|
|
crypto = require('crypto'),
|
|
|
|
assert = require('assert'),
|
|
|
|
zip = require('zipfile'),
|
|
|
|
Step = require('step');
|
2011-01-05 02:07:27 +08:00
|
|
|
|
2011-01-21 08:06:38 +08:00
|
|
|
// node compatibility for mkdirs below
|
|
|
|
var constants = {};
|
2011-01-25 06:23:18 +08:00
|
|
|
if (!process.EEXIST >= 1)
|
2011-01-21 08:06:38 +08:00
|
|
|
constants = require('constants');
|
|
|
|
else
|
|
|
|
constants.EEXIST = process.EEXIST;
|
|
|
|
|
2011-01-05 02:07:27 +08:00
|
|
|
|
2011-01-25 06:23:18 +08:00
|
|
|
function External(env, uri, callback) {
|
2011-01-25 07:14:54 +08:00
|
|
|
// assert.ok(data_dir in env);
|
|
|
|
// assert.ok(local_data_dir in env);
|
2011-01-13 06:18:03 +08:00
|
|
|
|
2011-01-25 06:23:18 +08:00
|
|
|
this.env = env;
|
|
|
|
this.callback = callback;
|
|
|
|
this.uri = uri;
|
|
|
|
this.format = path.extname(uri).toLowerCase();
|
2011-01-13 06:18:03 +08:00
|
|
|
|
2011-01-25 07:17:35 +08:00
|
|
|
External.mkdirp(this.env.data_dir, 0755);
|
2011-01-13 06:18:03 +08:00
|
|
|
|
2011-01-25 06:23:18 +08:00
|
|
|
if (/^https?:\/\//i.test(uri)) {
|
|
|
|
this.downloadFile();
|
|
|
|
} else {
|
|
|
|
this.localFile();
|
|
|
|
}
|
|
|
|
}
|
2011-01-05 02:07:27 +08:00
|
|
|
|
2011-01-25 06:23:18 +08:00
|
|
|
External.prototype.localFile = function() {
|
|
|
|
// Only treat files as local that don't have to be processed.
|
|
|
|
this.isLocal = !External.processors[this.format];
|
2011-01-05 08:57:35 +08:00
|
|
|
|
2011-01-25 06:23:18 +08:00
|
|
|
this.tempPath = path.join(this.env.local_data_dir, this.uri);
|
|
|
|
fs.stat(this.tempPath, this.processFile.bind(this));
|
|
|
|
};
|
2011-01-19 06:35:33 +08:00
|
|
|
|
2011-01-25 06:23:18 +08:00
|
|
|
External.prototype.downloadFile = function() {
|
|
|
|
this.tempPath = path.join(
|
2011-01-25 07:17:35 +08:00
|
|
|
this.env.data_dir,
|
2011-01-25 06:23:18 +08:00
|
|
|
crypto.createHash('md5').update(this.uri).digest('hex') + path.extname(this.uri));
|
|
|
|
|
2011-01-25 07:14:54 +08:00
|
|
|
console.log('downloading to ', this.tempPath);
|
|
|
|
|
2011-01-25 06:23:18 +08:00
|
|
|
fs.stat(this.path(), function(err, stats) {
|
|
|
|
if (err) {
|
|
|
|
// This file does not yet exist. Download it!
|
|
|
|
netlib.download(
|
|
|
|
this.uri,
|
|
|
|
this.tempPath,
|
|
|
|
External.encodings[this.format] || External.encodings['default'],
|
|
|
|
this.processFile.bind(this)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.callback(null, this);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
};
|
2011-01-05 02:07:27 +08:00
|
|
|
|
2011-01-25 06:23:18 +08:00
|
|
|
External.prototype.processFile = function(err) {
|
|
|
|
if (err) {
|
|
|
|
this.callback(err);
|
|
|
|
} else {
|
|
|
|
if (this.isLocal) {
|
|
|
|
this.callback(null, this);
|
|
|
|
} else {
|
|
|
|
(External.processors[this.format] || External.processors['default'])(
|
|
|
|
this.tempPath,
|
|
|
|
this.path(),
|
|
|
|
function(err) {
|
|
|
|
if (err) {
|
|
|
|
this.callback(err);
|
|
|
|
} else {
|
|
|
|
this.callback(null, this);
|
|
|
|
}
|
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2011-01-05 02:07:27 +08:00
|
|
|
|
|
|
|
|
2011-01-25 06:23:18 +08:00
|
|
|
External.prototype.path = function() {
|
|
|
|
if (this.isLocal) {
|
|
|
|
return this.tempPath;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return path.join(
|
|
|
|
this.env.data_dir,
|
|
|
|
(External.destinations[this.format] || External.destinations['default'])(this.uri)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2011-01-05 02:07:27 +08:00
|
|
|
|
2011-01-25 07:14:54 +08:00
|
|
|
External.prototype.shapeFile = function shapeFile() {
|
|
|
|
try {
|
|
|
|
var dir = this.path();
|
|
|
|
var unzipped = fs.readdirSync(dir);
|
|
|
|
var shp = _.detect(unzipped,
|
|
|
|
function(f) {
|
|
|
|
return path.extname(f).toLowerCase() == '.shp';
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (!shp) {
|
|
|
|
var dirs = _.select(unzipped,
|
|
|
|
function(f) {
|
|
|
|
return fs.statSync(path.join(dir, f)).isDirectory();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (dirs) {
|
|
|
|
for (var i = 0, l = dirs.length; i < l; i++) {
|
|
|
|
var located = shapeFile.call(this, path.join(dir, dirs[i]));
|
|
|
|
if (located) {
|
|
|
|
return located;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return path.join(dir, shp);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-01-25 06:23:18 +08:00
|
|
|
// https://gist.github.com/707661
|
|
|
|
External.mkdirp = function mkdirP (p, mode, f) {
|
|
|
|
var cb = f || function () {};
|
|
|
|
if (p.charAt(0) != '/') { cb('Relative path: ' + p); return }
|
|
|
|
|
|
|
|
var ps = path.normalize(p).split('/');
|
|
|
|
path.exists(p, function (exists) {
|
|
|
|
if (exists) cb(null);
|
|
|
|
else mkdirP(ps.slice(0,-1).join('/'), mode, function (err) {
|
|
|
|
if (err && err.errno != process.EEXIST) cb(err)
|
|
|
|
else {
|
|
|
|
fs.mkdir(p, mode, cb);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2011-01-05 08:57:35 +08:00
|
|
|
|
|
|
|
|
2011-01-15 03:33:40 +08:00
|
|
|
|
2011-01-25 06:23:18 +08:00
|
|
|
External.encodings = {
|
|
|
|
'.zip': 'binary',
|
|
|
|
'.shp': 'binary',
|
|
|
|
'default': 'utf-8'
|
|
|
|
};
|
2011-01-05 02:07:27 +08:00
|
|
|
|
2011-01-25 06:23:18 +08:00
|
|
|
// Destinations are names in the data_dir/cache directory.
|
|
|
|
External.destinations = {};
|
|
|
|
External.destinations['default'] = function(uri) {
|
|
|
|
return crypto.createHash('md5').update(uri).digest('hex') + path.extname(uri);
|
|
|
|
};
|
|
|
|
External.destinations['.zip'] = function(uri) {
|
|
|
|
return crypto.createHash('md5').update(uri).digest('hex');
|
|
|
|
};
|
2011-01-16 05:36:30 +08:00
|
|
|
|
2011-01-15 03:33:40 +08:00
|
|
|
|
2011-01-25 06:23:18 +08:00
|
|
|
External.processors = {};
|
|
|
|
External.processors['default'] = function(tempPath, destPath, callback) {
|
|
|
|
if (tempPath === destPath) {
|
|
|
|
callback(null);
|
|
|
|
} else {
|
|
|
|
fs.rename(tempPath, destPath, callback);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
External.processors['.zip'] = function(tempPath, destPath, callback) {
|
|
|
|
try {
|
|
|
|
var zf = new zip.ZipFile(tempPath);
|
|
|
|
// This is blocking, sequential and synchronous.
|
|
|
|
zf.names.forEach(function(name) {
|
|
|
|
var uncompressed = path.join(destPath, name);
|
|
|
|
External.mkdirp(path.dirname(uncompressed), 0755, function(err) {
|
|
|
|
if (err && err.errno != constants.EEXIST) {
|
|
|
|
throw "Couldn't create directory " + path.dirname(name);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (path.extname(name)) {
|
|
|
|
var buffer = zf.readFileSync(name);
|
|
|
|
console.log('saving to: ' + uncompressed);
|
|
|
|
fd = fs.openSync(uncompressed, 'w');
|
|
|
|
fs.writeSync(fd, buffer, 0, buffer.length, null);
|
|
|
|
fs.closeSync(fd);
|
2011-01-05 02:07:27 +08:00
|
|
|
}
|
2011-01-25 06:23:18 +08:00
|
|
|
else {
|
|
|
|
throw 'UNIMPLEMENTED';
|
2011-01-05 02:07:27 +08:00
|
|
|
}
|
|
|
|
}
|
2011-01-21 08:06:38 +08:00
|
|
|
});
|
2011-01-25 06:23:18 +08:00
|
|
|
});
|
|
|
|
callback(null);
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
callback(err);
|
|
|
|
}
|
2011-01-05 02:07:27 +08:00
|
|
|
};
|
|
|
|
|
2011-01-25 06:23:18 +08:00
|
|
|
|
|
|
|
|
2011-01-05 02:07:27 +08:00
|
|
|
module.exports = External;
|