pass header information with a .getTile() request

This commit is contained in:
Konstantin Käfer
2011-06-28 16:47:23 +02:00
parent 413ecb3b57
commit f5c87a14fa
2 changed files with 31 additions and 4 deletions

View File

@@ -24,14 +24,19 @@ MBTiles.utils = require('./utils');
// - callback: Will be called when the resources have been acquired // - callback: Will be called when the resources have been acquired
// or acquisition failed. // or acquisition failed.
function MBTiles(uri, callback) { function MBTiles(uri, callback) {
var mbtiles = this;
if (typeof callback !== 'function') callback = noop; if (typeof callback !== 'function') callback = noop;
if (typeof uri === 'string') uri = url.parse(uri); if (typeof uri === 'string') uri = url.parse(uri);
this.filename = uri.pathname; this.filename = uri.pathname;
this.db = new sqlite3.cached.Database(uri.pathname, function(err) { this.db = new sqlite3.cached.Database(mbtiles.filename, function(err) {
if (err) return callback(err); if (err) return callback(err);
else callback(null, this); else fs.stat(mbtiles.filename, function(err, stat) {
}.bind(this)); if (err) return callback(err);
mbtiles.stat = stat;
callback(null, mbtiles);
});
});
}; };
// Finds all mbtiles file in the filepath and returns their tilesource URI. // Finds all mbtiles file in the filepath and returns their tilesource URI.
@@ -279,7 +284,14 @@ MBTiles.prototype.getTile = function(z, x, y, callback) {
function(err, row) { function(err, row) {
if (!row || (err && err.errno == 1)) callback(new Error('Tile does not exist')); if (!row || (err && err.errno == 1)) callback(new Error('Tile does not exist'));
else if (err) callback(err); else if (err) callback(err);
else callback(null, row.tile_data); else {
var options = {
'Content-Type': MBTiles.utils.getMimeType(row.tile_data),
'Last-Modified': mbtiles.stat.mtime,
'E-Tag': mbtiles.stat.size + '-' + Number(mbtiles.stat.mtime)
};
callback(null, row.tile_data, options);
}
}); });
}; };

View File

@@ -20,6 +20,21 @@ utils.table = function(fields) {
}); });
}; };
utils.getMimeType = function(data) {
if (data[0] === 0x89 && data[1] === 0x50 && data[2] === 0x4E &&
data[3] === 0x47 && data[4] === 0x0D && data[5] === 0x0A &&
data[6] === 0x1A && data[7] === 0x0A) {
return 'image/png';
} else if (data[0] === 0xFF && data[1] === 0xD8 &&
data[data.length - 2] === 0xFF && data[data.length - 1] === 0xD9) {
return 'image/jpeg';
} else if (data[0] === 0x47 && data[1] === 0x49 && data[2] === 0x46 &&
data[3] === 0x38 && (data[4] === 0x39 || data[4] === 0x37) &&
data[5] === 0x61) {
return 'image/gif';
}
};
function Queue(callback, concurrency) { function Queue(callback, concurrency) {
this.callback = callback; this.callback = callback;
this.concurrency = concurrency || 10; this.concurrency = concurrency || 10;