Comparing version 0.0.3 to 0.0.4
var cacher = require('epub-cache'); | ||
var fs = require('fs'); | ||
var tmpl = require('epub-tmpl'); | ||
var cat = require('./localcat.js'); | ||
var util = require('util'); | ||
var epubdir; | ||
var path, filelist, tm, endtm; | ||
var listing = []; | ||
var opened = 0; | ||
var total = 0; | ||
var finalCb = function () {}; | ||
var listeners = {}; | ||
exports.attach = function attach(cacheDir) { | ||
module.exports.maxFiles = 0; | ||
module.exports.attach = function attach(cacheDir) { | ||
@@ -19,3 +27,3 @@ cacher.init({ | ||
exports.webify = function webify(epubfile, templateName, cb) { | ||
module.exports.webify = function webify(epubfile, templateName, cb) { | ||
@@ -45,3 +53,3 @@ console.log(epubfile); | ||
exports.reader = function reader(cacheId, templateName, cb) { | ||
module.exports.reader = function reader(cacheId, templateName, cb) { | ||
@@ -74,6 +82,127 @@ | ||
exports.catalogService = function () { | ||
return cat; | ||
module.exports.buildIndex = function (dir, p, cb) { | ||
parser = p; | ||
epubdir = dir; | ||
finalCb = cb; | ||
fs.readdir(dir, opendirCallback); | ||
} | ||
module.exports.loadIndex = function (index, cb) { | ||
listing = JSON.parse(index); | ||
cb(null, listing); | ||
} | ||
module.exports.on = function (topic, listener) { | ||
if(typeof listeners[topic] == 'undefined') { | ||
listeners[topic] = []; | ||
} | ||
listeners[topic].push(listener); | ||
} | ||
function opendirCallback (err, files) { | ||
filelist = files; | ||
tm = (new Date()).getTime(); | ||
if(module.exports.maxFiles > 0) { | ||
total = module.exports.maxFiles; | ||
} else { | ||
total = filelist.length; | ||
} | ||
for(var i = 0 ; i <= total; i++) { | ||
var name = filelist[i]; | ||
if(name.match(/epub$/i)) { | ||
try { | ||
parser.open(epubdir + '/' +filelist[i], openCallback); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
} | ||
} | ||
} | ||
function openCallback(err, epubData) { | ||
if(!err) { | ||
path = epubdir + '/' +filelist[opened]; | ||
console.log('------->'+Math.floor((opened/total)*100)+'%'); | ||
console.log(parser); | ||
var o = { | ||
path: path, | ||
primaryId: epubData.easy.primaryID.value, | ||
cover: epubData.easy.epub2CoverUrl, | ||
coverData: new Buffer(parser.extractBinary(epubData.easy.epub2CoverUrl),'binary'), | ||
simpleMeta: epubData.easy.simpleMeta, | ||
epubData: epubData | ||
}; | ||
//console.log(util.inspect(process.memoryUsage())); | ||
//console.log(o); | ||
listing.push(o); | ||
notify('newItem',o); | ||
opened++; | ||
console.log('opened '+opened+' out of '+total); | ||
if(opened==total) { | ||
endtm = (new Date()).getTime(); | ||
console.log('ending read ops at '+endtm); | ||
console.log('took '+((endtm-tm)/1000/60)+' minutes'); | ||
console.log('opened '+opened+' files'); | ||
finalCb(null, listing); | ||
} | ||
} else { | ||
console.log(err); | ||
} | ||
} | ||
function notify(topic, data) { | ||
for(topic in listeners) { | ||
for(var i = 0; i < listeners[topic].length; i ++) { | ||
listeners[topic][i].apply(this, [data]); | ||
} | ||
} | ||
} | ||
function load(contentConfig, readerConfig) { | ||
@@ -80,0 +209,0 @@ |
{ | ||
"name": "epub2web", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Web server module to allow reading of Epub content directly in the browser, without plugins or specialized applications. ", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
var cat = require('../lib/localcat.js'); | ||
var cat = require('../lib/epub2web.js'); | ||
var fs = require('fs'); | ||
@@ -20,3 +20,3 @@ | ||
cat.buildIndex(catalogDir, function (err, index) { // build a new index and replace the old (if it exists) | ||
cat.buildIndex(catalogDir, parser, function (err, index) { // build a new index and replace the old (if it exists) | ||
@@ -23,0 +23,0 @@ if(err) throw err; |
4
1072744
9
274