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

[stable10] Backport of Add regex for shared object file names #34082

Merged
merged 2 commits into from
Feb 7, 2019
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
28 changes: 26 additions & 2 deletions lib/private/Files/Type/Detection.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,26 @@ private function loadMappings() {
$this->registerTypeArray($mimetypeMapping);
}

/**
* Detect if the file is a shared object file. We detect this file
* with a regex.
*
* @param string $fileName
* @return bool true if file is a shared object else false
*/
private function detectSharedObjFileName($fileName) {
\preg_match("/(.*?)(\.so)([\.]*?.*)/i", $fileName, $match);
/**
* 1. Exactly 4 indexes to match ( check the regex grouping above preg_match )
* 2. Check if the filename has '.so'
* 3. Check if the filname has extension after '.so', say .so.1. or .so.
* That is anything like '.so.*'. This match is made in the 4th index.
* No extension after '.so' is considered to be shared object file
*/
return (\count($match) === 4) && ($match[2] === '.so')
&& ($match[3] === '' || ($match[3] !== '' && \strpos($match[3], '.') === 0));
}

/**
* @return array
*/
Expand All @@ -171,8 +191,12 @@ public function detectPath($path) {
// note: leading dot doesn't qualify as extension
if (\strpos($fileName, '.') > 0) {
//try to guess the type by the file extension
$extension = \strtolower(\strrchr($fileName, '.'));
$extension = \substr($extension, 1); //remove leading .
if ($this->detectSharedObjFileName($fileName)) {
$extension = 'so';
} else {
$extension = \strtolower(\strrchr($fileName, '.'));
$extension = \substr($extension, 1); //remove leading .
}
return (isset($this->mimetypes[$extension], $this->mimetypes[$extension][0]))
? $this->mimetypes[$extension][0]
: 'application/octet-stream';
Expand Down
3 changes: 2 additions & 1 deletion resources/config/mimetypemapping.dist.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,6 @@
"yml": ["application/yaml", "text/plain"],
"zip": ["application/zip"],
"so": ["application/x-sharedlib"],
"dll": ["application/x-msdos-program"]
"dll": ["application/x-msdos-program"],
"dylib": ["application/x-sharedlib"]
}
7 changes: 7 additions & 0 deletions tests/lib/Files/Type/DetectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,14 @@ public function testDetectPath() {
$this->assertEquals('image/png', $this->detection->detectPath('.hidden/foo.png'));
$this->assertEquals('image/png', $this->detection->detectPath('test.jpg/foo.png'));
$this->assertEquals('application/octet-stream', $this->detection->detectPath('.png'));
$this->assertEquals('application/x-sharedlib', $this->detection->detectPath('test.so'));
$this->assertEquals('application/x-sharedlib', $this->detection->detectPath('test.so.0.0.1'));
$this->assertEquals('application/x-sharedlib', $this->detection->detectPath('test.1.2.so.0.0.1'));
$this->assertEquals('application/x-sharedlib', $this->detection->detectPath('test.1.2.so.1'));
$this->assertEquals('application/x-sharedlib', $this->detection->detectPath('foo.so.'));
$this->assertEquals('application/octet-stream', $this->detection->detectPath('foo'));
$this->assertEquals('application/octet-stream', $this->detection->detectPath('foo.somany'));
$this->assertEquals('application/octet-stream', $this->detection->detectPath('foo.so*'));
$this->assertEquals('application/octet-stream', $this->detection->detectPath(''));
}

Expand Down