Skip to content

Commit

Permalink
Added/updated detection for text based files
Browse files Browse the repository at this point in the history
  • Loading branch information
SoftCreatR committed Oct 19, 2018
1 parent a6cddb6 commit 3460c76
Showing 1 changed file with 53 additions and 16 deletions.
69 changes: 53 additions & 16 deletions src/SoftCreatR/MimeDetector/MimeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,22 +355,6 @@ public function getFileType(): array
];
}

if ($this->checkString('<?xml ')) {
if ($this->searchForBytes($this->toBytes('<!doctype svg'), 6) !== -1 ||
$this->searchForBytes($this->toBytes('<svg'), 6) !== -1
) {
return [
'ext' => 'svg',
'mime' => 'image/svg+xml'
];
}

return [
'ext' => 'xml',
'mime' => 'application/xml'
];
}

if ($this->checkForBytes([0x1F, 0x8B, 0x8])) {
return [
'ext' => 'gz',
Expand Down Expand Up @@ -980,6 +964,59 @@ public function getFileType(): array
];
}

// this class is intended to detect binary files, only. But there's nothing wrong in
// trying to detect text files aswell.
if ($this->checkString('<?xml ')) {
if ($this->searchForBytes($this->toBytes('<!doctype svg'), 6) !== -1 ||
$this->searchForBytes($this->toBytes('<!DOCTYPE svg'), 6) !== -1 ||
$this->searchForBytes($this->toBytes('<svg'), 6) !== -1
) {
return [
'ext' => 'svg',
'mime' => 'image/svg+xml'
];
}

if ($this->searchForBytes($this->toBytes('<!doctype html'), 6) !== -1 ||
$this->searchForBytes($this->toBytes('<!DOCTYPE html'), 6) !== -1 ||
$this->searchForBytes($this->toBytes('<html'), 6) !== -1
) {
return [
'ext' => 'html',
'mime' => 'text/html'
];
}

if ($this->searchForBytes($this->toBytes('<rdf:RDF'), 6) !== -1) {
return [
'ext' => 'rdf',
'mime' => 'application/rdf+xml'
];
}

if ($this->searchForBytes($this->toBytes('<rss version="2.0"'), 6) !== -1) {
return [
'ext' => 'rss',
'mime' => 'application/rss+xml'
];
}

return [
'ext' => 'xml',
'mime' => 'application/xml'
];
}

if ($this->checkString('<!doctype html') ||
$this->checkString('<!DOCTYPE html') ||
$this->checkString('<html')
) {
return [
'ext' => 'html',
'mime' => 'text/html'
];
}

return [];
}

Expand Down

0 comments on commit 3460c76

Please sign in to comment.