Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image download error 500 response #79

Open
azu opened this issue Oct 26, 2019 · 0 comments
Open

Image download error 500 response #79

azu opened this issue Oct 26, 2019 · 0 comments

Comments

@azu
Copy link

azu commented Oct 26, 2019

I've tested on https://learning.oreilly.com/library/view/distributed-tracing-in/9781492056621/ and get following erros:

[safari-downloader] downloaded chapter files...
[safari-downloader] downloaded stylesheet files...
[safari-downloader] an error occurred while trying to download all the image assets (err: HTTPError: Response code 500 (Internal Server Error))

I've noticed that image downloader does not includes authroized header.
I try to use Safari client and the downloads is successful.

diff --git a/lib/cli/index.js b/lib/cli/index.js
index f223f30..e568811 100644
--- a/lib/cli/index.js
+++ b/lib/cli/index.js
@@ -105,7 +105,7 @@ function start() {
   var safariClient = new Safari(program.debug);
   safariClient.fetchBookById(program.bookid, username, password, cookies, program.weblogin).then( (bookJSON) => {
     // console.log(bookJSON);
-    var ebook = new EbookWriter(bookJSON);
+    var ebook = new EbookWriter(safariClient, bookJSON);
     return ebook.save(program.output);
   }).then( () => {
     // ## finished saving
diff --git a/lib/ebook-writer/index.js b/lib/ebook-writer/index.js
index 3e66341..7ee8052 100644
--- a/lib/ebook-writer/index.js
+++ b/lib/ebook-writer/index.js
@@ -15,12 +15,13 @@ const mime = require('mime');
 * ## eBook Writer
 * sets up the ebook writer with its base settings
 *
+* @param {Safari} safariClient - Safari instance
 * @param {Object} options - the options for the ebook (e.g. content, title, name, etc.)
 *
 * @return {Object} ebook - returns a ebook object
 */
-const Ebook = function Ebook(options) {
-
+const Ebook = function Ebook(safariClient, options) {
+  this.safariClient = safariClient;
   // ## prepare base settings
   this.tempPath = "books";
   this.options = options || {};
@@ -323,7 +324,9 @@ function _writeCSS(that) {
 * @return {Promise} success - returns whether the creation was successful or not
 */
 function _downloadCoverImage(that) {
-  return download(that.options.cover).then( (data) => {
+  return that.safariClient.fetchResource(that.options.cover, {
+    binary: true
+  }).then((data) => {
     debug(`cover image successfully fetched`);
     // ## writing the file to the images directory
     // ## using writeFileSync on purpose; might be using async in the future
@@ -388,14 +391,16 @@ function _prepareImages(that) {
 * ## Download images
 * downloads all the image assets
 *
-* @param {String} that - the ebook object
+* @param {Ebook} that - the ebook object
 *
 * @return {Promise} success - returns whether the creation was successful or not
 */
 function _downloadImages(that) {
   if(!that.options.imagesToFetch) return Promise.reject("there are no images to be fetched yet; prepare first");
   return Promise.all(that.options.imagesToFetch.map( (image) => {
-    return download(image.baseUrl + image.file).then( (data) => {
+    return that.safariClient.fetchResource(image.baseUrl + image.file, {
+      binary: true
+    }).then((data) => {
       // ## the image was successfully downloded, writing to image path
       debug(`${image.file} successfully fetched`);
       // ## writing the file to the images directory
@@ -403,6 +408,9 @@ function _downloadImages(that) {
       let path = that.tempBookPath + "/OEBPS/images/" + image.path;
       fs.writeFileSync(path, data);
       return Promise.resolve();
+    }).catch(error => {
+        debug(`${imagqe.file} failure fetched`);
+        return Promise.reject(error);
     });
   })).then( () => {
     // ## the downloading was successfully finished
diff --git a/lib/safari/index.js b/lib/safari/index.js
index 7211eaa..b65e128 100644
--- a/lib/safari/index.js
+++ b/lib/safari/index.js
@@ -126,18 +126,19 @@ Safari.prototype.fetchResource = function fetchResource(url, options) {
   debug(`fetchResource called with URL: ${url}`);
   if(!url || !this.accessToken && !this.cookieString) return Promise.reject("url was not specified or user has not been authorized yet");
   // ## prepare options for resource request
-  var uri = `${this.baseUrl}/${url}`;
+  var uri = /^https?:/.test(url) ? url : `${this.baseUrl}/${url}`;
   var json = true;
   var headers = {}
   if(this.access_token) headers["authorization"] = `Bearer ${this.accessToken}`
   if(this.cookieString) headers["cookie"] = `${this.cookieString}`
-  if(options && options.json == false) json = false;
+  if(options && (options.json === false || options.binary === true)) json = false;
   if(options && options.uri) uri = options.uri;
   let settings = {
     method: 'GET',
     headers: headers,
     uri: uri,
-    json: json
+    json: json,
+    encoding: options && options.binary ? null : "utf-8"
   };
   // ## make request
   return request(settings).then( (body) => {

However, I just use curl and it is successs.
Maybe, download module is something wrong,

curl https://learning.oreilly.com/library/view/distributed-tracing-in/9781492056621/assets/cover.png -o test.png
@azu azu mentioned this issue Oct 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant