Consistify local caching logic and allow local, non-relative shapefiles to be cached properly.

This commit is contained in:
Young Hahn 2011-07-20 19:20:21 -04:00
parent 51d6768ac4
commit 193c99e4f8

View File

@ -49,9 +49,9 @@ function External(env, uri, alias) {
if (local && path.dirname(uri).search(env.data_dir) === 0) {
this.destPath = uri;
} else if (env.alias && alias) {
this.destPath = path.join(env.data_dir, alias + path.extname(uri));
this.destPath = path.join(env.data_dir, alias, alias + path.extname(uri));
} else {
this.destPath = path.join(env.data_dir, hash + path.extname(uri));
this.destPath = path.join(env.data_dir, hash, hash + path.extname(uri));
}
this.destPath = this.destination(this.destPath);
@ -164,6 +164,45 @@ External.mkdirp = function mkdirP(p, mode, f) {
});
};
// Utility function. Implements a `cp -r` like method in node.
External.cpr = function cpr(from, to, callback) {
fs.stat(from, function(err, stat) {
if (err) return callback(err);
if (stat.isFile()) {
External.mkdirp(path.dirname(to), 0755, function(err) {
if (err) callback(err);
fs.readFile(from, function(err, data) {
if (err) return callback(err);
fs.writeFile(to, data, callback);
});
});
} else if (stat.isDirectory()) {
External.mkdirp(to, stat.mode, function(err) {
if (err) return callback(err);
fs.readdir(from, function(err, files) {
if (err) return callback(err);
if (!files.length) return callback();
var remaining = files.length;
for (var i = 0; i < files.length; i++) {
cpr(path.join(from, files[i]),
path.join(to, files[i]),
function(err) {
if (err) {
remaining = -1;
return callback(err);
} else if (remaining > 0) {
remaining--;
if (!remaining) callback();
}
}
);
}
});
});
}
});
};
External.types = [
//for datafile: function(d, c) - d is an External object, c is a function taking (err, filename)
{
@ -177,7 +216,7 @@ External.types = [
},
{
extension: /\.shp/,
datafile: function(d, c) { c(null, d.path()); },
datafile: function(d, c) { d.findOneByExtension('.shp', c); },
ds_options: {
type: 'shape'
}
@ -246,45 +285,19 @@ External.destinations = {};
External.destinations['default'] = function(destPath) {
return destPath;
};
External.destinations['.shp'] = function(destPath) {
return path.dirname(destPath);
};
External.destinations['.zip'] = function(destPath) {
return path.join(
path.dirname(destPath),
path.basename(destPath, path.extname(destPath))
);
return path.dirname(destPath);
};
External.processors = {};
External.processors['default'] = function(ext, callback) {
var tempPath = ext.tempPath;
var destPath = ext.destPath;
path.exists(tempPath, function(exists) {
if (!exists) {
callback(new Error('File ' + tempPath + ' does not exist'));
} else if (tempPath === destPath) {
callback(null);
} else {
fs.readFile(tempPath, function(err, data) {
if (err) return callback(err);
fs.writeFile(destPath, data, callback);
});
// @TODO: This fails on rare occasions for reasons that are hard
// to determine (try running expresso test/external.test.js many
// times).
//var temp = fs.createReadStream(tempPath);
//// node 0.4
//if (temp.pipe) {
// temp.pipe(fs.createWriteStream(destPath));
// temp.once('end', callback);
// temp.once('error', callback);
//// node 0.2
//} else {
// var dest = fs.createWriteStream(dest);
// dest.on('open', function(fd){
// require('sys').pump(temp, dest, callback);
// });
//}
}
});
External.cpr(ext.tempPath, ext.destPath, callback);
};
External.processors['.shp'] = function(ext, callback) {
External.cpr(path.dirname(tempPath), path.dirname(destPath), callback);
};
External.processors['.zip'] = function(ext, callback) {
var tempPath = ext.tempPath;