From 30bb1357aa6820a8ae19ef72cc6f01e8cc3416e5 Mon Sep 17 00:00:00 2001 From: andris9 Date: Mon, 13 Jun 2011 14:33:53 +0300 Subject: [PATCH] updates --- epub.js | 19 ++++++++-- index.html | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 2 +- server.js | 79 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 198 insertions(+), 3 deletions(-) create mode 100644 index.html create mode 100644 server.js diff --git a/epub.js b/epub.js index ef9cf67..f5c7800 100644 --- a/epub.js +++ b/epub.js @@ -514,13 +514,18 @@ EPub.prototype.walkNavMap = function(branch, path, id_list, level){ * - callback (Function): callback function * * Finds a chapter text for an id. Replaces image and link URL's, removes - * etc. elements + * etc. elements. Return only chapters with mime type application/xhtml+xml **/ EPub.prototype.getChapter = function(id, callback){ var path = this.rootFile.split("/"), keys = Object.keys(this.manifest); path.pop(); if(this.manifest[id]){ + + if((this.manifest[id]['media-type'] || "").toLowerCase().trim() != "application/xhtml+xml"){ + return callback(new Error("Inavlid mime type for chapter")); + } + this.zip.readFile(this.manifest[id].href, (function(err, data){ if(err){ callback(new Error("Reading archive failed")); @@ -605,6 +610,8 @@ EPub.prototype.getChapter = function(id, callback){ callback(null, str); }).bind(this)); + }else{ + callback(new Error("File not found")); } } @@ -615,10 +622,16 @@ EPub.prototype.getChapter = function(id, callback){ * - callback (Function): callback function * * Finds an image an id. Returns the image as Buffer. Callback gets - * an error object, image buffer and image content-type + * an error object, image buffer and image content-type. + * Return only images with mime type image/* **/ EPub.prototype.getImage = function(id, callback){ if(this.manifest[id]){ + + if((this.manifest[id]['media-type'] || "").toLowerCase().trim().substr(0,6) != "image/"){ + return callback(new Error("Inavlid mime type for image")); + } + this.zip.readFile(this.manifest[id].href, (function(err, data){ if(err){ callback(new Error("Reading archive failed")); @@ -627,5 +640,7 @@ EPub.prototype.getImage = function(id, callback){ callback(null, data, this.manifest[id]['media-type']); }).bind(this)); + }else{ + callback(new Error("File not found")); } } \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..dfc7024 --- /dev/null +++ b/index.html @@ -0,0 +1,101 @@ + + + + + EPUB reader + + + + + + + + + + + + + + + + +
+ +
+ + + \ No newline at end of file diff --git a/index.js b/index.js index 3c2a1c4..a22ff0a 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ var EPub = require("./epub"); -var epub = new EPub("img.epub", "tere", "vana"); +var epub = new EPub("tasuja.epub", "tere", "vana"); epub.on("error", function(err){ console.log("ERROR\n-----"); throw err; diff --git a/server.js b/server.js new file mode 100644 index 0000000..9464a91 --- /dev/null +++ b/server.js @@ -0,0 +1,79 @@ +var http = require('http'), + EPub = require("./epub"), + fs = require("fs"); + +var EPUBFILE = "tasuja.epub"; + +var epub = new EPub(EPUBFILE, "/epubimg", "/chapter"); +epub.on("error", function(err){ + console.log("ERROR\n-----"); + throw err; +}); + +epub.on("end", function(){ + + startserver(); + +}); + +epub.parse(); + + +function startserver(){ + http.createServer(function (req, res) { + + if(req.url.match(/^\/contents/)){ + res.writeHead(200, {'Content-Type': 'text/javascript'}); + res.end(JSON.stringify({toc: epub.toc, flow: epub.spine.contents})); + return; + } + + if(req.url.match(/^\/chapter/)){ + var parts = req.url.split("/"); + epub.getChapter(parts[2], function(err, data){ + if(err){ + res.writeHead(500, {'Content-Type': 'text/html'}); + res.end("

Error

"+err.message+"

"); + return; + } + res.writeHead(200, {'Content-Type': 'text/html'}); + res.end(data); + }); + return; + } + + if(req.url.match(/^\/epubimg/)){ + var parts = req.url.split("/"); + epub.getImage(parts[2], function(err, data, mimeType){ + if(err){ + res.writeHead(500, {'Content-Type': 'text/html'}); + res.end("

Error

"+err.message+"

"); + return; + } + res.writeHead(200, {'Content-Type': mimeType}); + res.end(data); + }); + return; + } + + if(req.url == "/"){ + fs.readFile("index.html", function(err, data){ + if(err){ + res.writeHead(500, {'Content-Type': 'text/html'}); + res.end("

Error

"+err.message+"

"); + return; + } + res.writeHead(200, {'Content-Type': 'text/html'}); + res.end(data); + }); + return; + } + + res.writeHead(200, {'Content-Type': 'text/plain'}); + res.end('Hello World\n'); + + }).listen(8080); + console.log('Server running'); +} + +