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

Fix error with corrupted PDF image download #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/response-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,32 @@ class ResponseHandler {
return (response) => {
if (debug) { console.log(response); } // eslint-disable-line no-console

let result = '';
let isJson = response.headers['content-type'] == 'text/json';
let isTiff = response.headers['content-type'] == 'image/tiff';
let isPdf = response.headers['content-type'] == 'application/pdf';
let isImage = isTiff || isPdf;
let result = isImage ? [] : '';

let isLocation = response.headers['location'] !== undefined;

response.on('data', function(chunk) {
result += chunk;
if (isImage) {
result.push(chunk);
} else {
result += chunk;
}
});

response.on('end', function() {
if (debug) { console.log(result); } // eslint-disable-line no-console

if (isLocation) { result = new Location(response.headers['location']); }
else if (isImage) { result = new Image(result, response.headers['content-type']); }
else if (isJson && result.length > 0) { result = JSON.parse(result); }
else if (isJson && result.length == 0) { result = null; }
else if (isImage) {
const data = Buffer.concat(result);
result = new Image(data, response.headers['content-type']);
}

if (response.statusCode >= 300) {
emitter.emit('reject', result);
Expand Down
Binary file added tests/fixtures/pdf-sample.pdf
Binary file not shown.
6 changes: 4 additions & 2 deletions tests/response-handler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ResponseHandler from '../src/response-handler.js';
import Image from '../src/image.js';
import EventEmitter from 'events';
import fs from 'fs';

import { expect } from 'chai';

Expand Down Expand Up @@ -52,9 +53,10 @@ describe('ResponseHandler', () => {
});

it('should process an image response', (done) => {
let pdfData = fs.readFileSync('tests/fixtures/pdf-sample.pdf');
let emitter = new EventEmitter();
emitter.on('resolve', (result) => {
expect(result).to.eql({ 'data': 'Hello World!', 'contentType' : 'application/pdf', 'extension' : 'pdf' });
expect(result.data).to.eql(pdfData);
expect(result).to.be.instanceof(Image);
done();
});
Expand All @@ -65,7 +67,7 @@ describe('ResponseHandler', () => {

handler(response);

response.emit('data', 'Hello World!');
response.emit('data', pdfData);
response.emit('end');
});

Expand Down