From f5c87a14fa972b147a6759c83e55e660d9e21eeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Tue, 28 Jun 2011 16:47:23 +0200 Subject: [PATCH] pass header information with a .getTile() request --- lib/mbtiles.js | 20 ++++++++++++++++---- lib/utils.js | 15 +++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/mbtiles.js b/lib/mbtiles.js index 6ddd23c..d385df6 100644 --- a/lib/mbtiles.js +++ b/lib/mbtiles.js @@ -24,14 +24,19 @@ MBTiles.utils = require('./utils'); // - callback: Will be called when the resources have been acquired // or acquisition failed. function MBTiles(uri, callback) { + var mbtiles = this; if (typeof callback !== 'function') callback = noop; if (typeof uri === 'string') uri = url.parse(uri); 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); - else callback(null, this); - }.bind(this)); + else fs.stat(mbtiles.filename, function(err, stat) { + if (err) return callback(err); + mbtiles.stat = stat; + callback(null, mbtiles); + }); + }); }; // 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) { if (!row || (err && err.errno == 1)) callback(new Error('Tile does not exist')); 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); + } }); }; diff --git a/lib/utils.js b/lib/utils.js index 1f0387c..37146ae 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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) { this.callback = callback; this.concurrency = concurrency || 10;