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

src: Add support for AI files (Adobe Illustrator) #323

Merged
Merged
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
3 changes: 2 additions & 1 deletion core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ declare namespace core {
| 'mp1'
| 'it'
| 's3m'
| 'xm';
| 'xm'
| 'ai';

type MimeType =
| 'image/jpeg'
Expand Down
20 changes: 20 additions & 0 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ 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, {length: 512, mayBeLess: true});

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

async function fromTokenizer(tokenizer) {
try {
return _fromTokenizer(tokenizer);
Expand All @@ -71,6 +80,7 @@ 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 @@ -548,6 +558,16 @@ async function _fromTokenizer(tokenizer) {
}

if (checkString('%PDF')) {
// Check if this is an Adobe Illustrator file
const isAiFile = await checkSequence('Adobe Illustrator', 1350);
if (isAiFile) {
return {
ext: 'ai',
mime: 'application/postscript'
};
}

// Assume this is just a normal PDF
return {
ext: 'pdf',
mime: 'application/pdf'
Expand Down
Loading