julien-c / epub

node.js epub reader
https://www.npmjs.com/package/epub
Other
333 stars 307 forks source link

Not loading images #16

Open Emmy02 opened 9 years ago

Emmy02 commented 9 years ago

Greetings, I want to congratulate you, because your project is very usefull, I just want to know why the images are not loaded in the getChapter function?

julien-c commented 9 years ago

Hi @Emmy02 – can you try the getFile function instead?

Emmy02 commented 9 years ago

Hi, I tried getFile function, it's download the entire file, instead rendering images.

srghma commented 2 years ago

with

function parse(epub) {
  return new Promise((resolve, reject) => {
    epub.on("error", function(error) {
      console.log("ERROR\n-----")
      throw error
    })

    epub.on("end", function(error){
      if (error) { reject({ on: "end", error }) }
      else { resolve() }
    })

    epub.parse()
  })
}

function getChapter(epub, id) {
  return new Promise((resolve, reject) => {
    epub.getChapter(id, function(error, data){
      if(error){
        reject(error)
        return
      }
      resolve(data)
    })
  })
}

function getImage(epub, id) {
  return new Promise((resolve, reject) => {
    epub.getImage(id, function(error, data, mimeType){
      if(error){
        reject(error)
        return
      }
      resolve({ data, mimeType })
    })
  })
}

var EPub = require("epub")

var epub = new EPub("elon-musk.epub", "/imagewebroot/", "/articlewebroot/")

const parseResult = await parse(epub)

console.log(parseResult)
console.log("METADATA:\n")
console.log(epub.metadata)

console.log("\nSPINE:\n")
console.log(epub.flow)

console.log("\nTOC:\n")
console.log(epub.toc)

data = await getChapter(epub, epub.spine.contents[2].id)

image = await getImage(epub, 'x001.jpg')

I get

> data = await getChapter(epub, epub.spine.contents[2].id)
'<div class="chatu">\n' +
  '    <img alt="" />\n' +
  '\n' +
  '    <p class="tushuo">马斯克的外祖父天生爱冒险,除了常自己开飞机到各地旅行,还经常带着孩子们到荒野去探险,照片中的宁静,让人对这些旅行的危险 性产生错觉。</p>\n' +
  '  </div>\n' +

as You see there is no id in <img alt="" />

srghma commented 2 years ago

I can confirm getFile works

> data = await getFile(epub, epub.spine.contents[2].id)
<Buffer 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e 3d 22 31 2e 30 22 20 65 6e 63 6f 64 69 6e 67 3d 22 75 74 66 2d 38 22 20 73 74 61 6e 64 61 6c 6f 6e 65 3d 22 6e ... 8082 more bytes>
> data.toString()
'<?xml version="1.0" encoding="utf-8" standalone="no"?>\r\n' +
  '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"\r\n' +
  '  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\r\n' +
  '\r\n' +
  '<html xmlns="http://www.w3.org/1999/xhtml">\r\n' +
  '<head>\r\n' +
  '  <title></title>\r\n' +
  '  <link href="../Styles/stylesheet_20151118.css" rel="stylesheet" type="text/css" />\r\n' +
  '  <link href="../Styles/stylesheet_zmx.css" rel="stylesheet" type="text/css" />\r\n' +
  '</head>\r\n' +
  '\r\n' +
  '<body>\r\n' +
  '  <div class="chatu">\r\n' +
  '    <img alt="" src="../Images/004.jpg" />\r\n' +
  '\r\n' +