Skip to content

Commit

Permalink
Improve PDF / AI (Adobe Illustrator) recognition (#396)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
Borewit and sindresorhus committed Jan 13, 2021
1 parent 7f95cd2 commit 9736aa3
Show file tree
Hide file tree
Showing 9 changed files with 13,054 additions and 622 deletions.
18 changes: 6 additions & 12 deletions core.js
Expand Up @@ -55,15 +55,6 @@ function _check(buffer, headers, options) {
return true;
}

async function _checkSequence(sequence, tokenizer, ignoreBytes) {
const buffer = Buffer.alloc(minimumBytes);
await tokenizer.ignore(ignoreBytes);

await tokenizer.peekBuffer(buffer, {mayBeLess: true});

return buffer.includes(Buffer.from(sequence));
}

async function fromTokenizer(tokenizer) {
try {
return _fromTokenizer(tokenizer);
Expand All @@ -79,7 +70,6 @@ async function _fromTokenizer(tokenizer) {
const bytesRead = 12;
const check = (header, options) => _check(buffer, header, options);
const checkString = (header, options) => check(stringToBytes(header), options);
const checkSequence = (sequence, ignoreBytes) => _checkSequence(sequence, tokenizer, ignoreBytes);

// Keep reading until EOF if the file size is unknown.
if (!tokenizer.fileInfo.size) {
Expand Down Expand Up @@ -596,9 +586,13 @@ async function _fromTokenizer(tokenizer) {
}

if (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});

// Check if this is an Adobe Illustrator file
const isAiFile = await checkSequence('Adobe Illustrator', 1350);
if (isAiFile) {
if (buffer.includes(Buffer.from('AIPrivateData'))) {
return {
ext: 'ai',
mime: 'application/postscript'
Expand Down
Binary file added fixture/fixture-adobe-illustrator.pdf
Binary file not shown.
Binary file added fixture/fixture-fast-web.pdf
Binary file not shown.
6,465 changes: 6,465 additions & 0 deletions fixture/fixture-normal.ai

Large diffs are not rendered by default.

Binary file added fixture/fixture-printed.pdf
Binary file not shown.
Binary file added fixture/fixture-smallest.pdf
Binary file not shown.
6,572 changes: 6,572 additions & 0 deletions fixture/fixture-without-pdf-compatibility.ai

Large diffs are not rendered by default.

610 changes: 0 additions & 610 deletions fixture/fixture.ai

This file was deleted.

11 changes: 11 additions & 0 deletions test.js
Expand Up @@ -181,6 +181,17 @@ const names = {
'fixture',
'fixture.wma',
'fixture.wmv'
],
ai: [
'fixture-normal', // Normal AI
'fixture-without-pdf-compatibility' // AI without the PDF compatibility (cannot be opened by PDF viewers I guess)
],
pdf: [
'fixture',
'fixture-adobe-illustrator', // PDF saved from Adobe Illustrator, using the default "[Illustrator Default]" preset
'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.
]
};

Expand Down

0 comments on commit 9736aa3

Please sign in to comment.