Skip to content

Commit

Permalink
handle tiny pdfs (#580)
Browse files Browse the repository at this point in the history
Prevents throwing parsing small PDF files (where size <= 1350 bytes)
  • Loading branch information
eric-yuan-vanta authored Feb 18, 2023
1 parent 1c76b4a commit edf59f8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
27 changes: 17 additions & 10 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,17 +611,24 @@ class FileTypeParser {
}

if (this.checkString('%PDF')) {
await tokenizer.ignore(1350);
const maxBufferSize = 10 * 1024 * 1024;
const buffer = Buffer.alloc(Math.min(maxBufferSize, tokenizer.fileInfo.size));
await tokenizer.readBuffer(buffer, {mayBeLess: true});
try {
await tokenizer.ignore(1350);
const maxBufferSize = 10 * 1024 * 1024;
const buffer = Buffer.alloc(Math.min(maxBufferSize, tokenizer.fileInfo.size));
await tokenizer.readBuffer(buffer, {mayBeLess: true});

// Check if this is an Adobe Illustrator file
if (buffer.includes(Buffer.from('AIPrivateData'))) {
return {
ext: 'ai',
mime: 'application/postscript',
};
// Check if this is an Adobe Illustrator file
if (buffer.includes(Buffer.from('AIPrivateData'))) {
return {
ext: 'ai',
mime: 'application/postscript',
};
}
} catch (error) {
// Swallow end of stream error if file is too small for the Adobe AI check
if (!(error instanceof strtok3.EndOfStreamError)) {
throw error;
}
}

// Assume this is just a normal PDF
Expand Down
Binary file added fixture/fixture-minimal.pdf
Binary file not shown.
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ const names = {
'fixture-smallest', // PDF saved from Adobe Illustrator, using the preset "smallest PDF"
'fixture-fast-web', // PDF saved from Adobe Illustrator, using the default "[Illustrator Default"] preset, but enabling "Optimize for Fast Web View"
'fixture-printed', // PDF printed from Adobe Illustrator, but with a PDF printer.
'fixture-minimal', // PDF written to be as small as the spec allows
],
webm: [
'fixture-null', // EBML DocType with trailing null character
Expand Down

0 comments on commit edf59f8

Please sign in to comment.