Skip to content

Commit

Permalink
Merge pull request #34082 from owncloud/regex-for-sharedobject-file-s…
Browse files Browse the repository at this point in the history
…table10

[stable10] Backport of Add regex for shared object file names
  • Loading branch information
phil-davis authored Feb 7, 2019
2 parents 8816494 + 7d74dd5 commit a12b47b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
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

0 comments on commit a12b47b

Please sign in to comment.