summaryrefslogtreecommitdiffstats
path: root/lib/private/Files
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2019-11-28 15:47:13 +0100
committerJoas Schilling <coding@schilljs.com>2019-12-10 09:11:40 +0100
commit1336dedd5d7946c74d1bfedc17e3510fcb744be7 (patch)
treebf556a44b3f62a3bb33fc048ecf829e9b89841e2 /lib/private/Files
parentb92ebb928a82df37ee0483861bdc4dbdcb38f816 (diff)
downloadnextcloud-server-1336dedd5d7946c74d1bfedc17e3510fcb744be7.tar.gz
nextcloud-server-1336dedd5d7946c74d1bfedc17e3510fcb744be7.zip
Cleanup the code
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib/private/Files')
-rw-r--r--lib/private/Files/Type/Detection.php88
1 files changed, 45 insertions, 43 deletions
diff --git a/lib/private/Files/Type/Detection.php b/lib/private/Files/Type/Detection.php
index 60d0eec9d9e..7d4c3408c86 100644
--- a/lib/private/Files/Type/Detection.php
+++ b/lib/private/Files/Type/Detection.php
@@ -1,4 +1,5 @@
<?php
+declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
@@ -77,8 +78,8 @@ class Detection implements IMimeTypeDetector {
*/
public function __construct(IURLGenerator $urlGenerator,
ILogger $logger,
- $customConfigDir,
- $defaultConfigDir) {
+ string $customConfigDir,
+ string $defaultConfigDir) {
$this->urlGenerator = $urlGenerator;
$this->logger = $logger;
$this->customConfigDir = $customConfigDir;
@@ -96,9 +97,9 @@ class Detection implements IMimeTypeDetector {
* @param string $mimetype
* @param string|null $secureMimeType
*/
- public function registerType($extension,
- $mimetype,
- $secureMimeType = null) {
+ public function registerType(string $extension,
+ string $mimetype,
+ ?string $secureMimeType = null): void {
$this->mimetypes[$extension] = array($mimetype, $secureMimeType);
$this->secureMimeTypes[$mimetype] = $secureMimeType ?: $mimetype;
}
@@ -112,12 +113,12 @@ class Detection implements IMimeTypeDetector {
*
* @param array $types
*/
- public function registerTypeArray($types) {
+ public function registerTypeArray(array $types): void {
$this->mimetypes = array_merge($this->mimetypes, $types);
// Update the alternative mimetypes to avoid having to look them up each time.
foreach ($this->mimetypes as $mimeType) {
- $this->secureMimeTypes[$mimeType[0]] = isset($mimeType[1]) ? $mimeType[1]: $mimeType[0];
+ $this->secureMimeTypes[$mimeType[0]] = $mimeType[1] ?? $mimeType[0];
}
}
@@ -136,7 +137,7 @@ class Detection implements IMimeTypeDetector {
/**
* Add the mimetype aliases if they are not yet present
*/
- private function loadAliases() {
+ private function loadAliases(): void {
if (!empty($this->mimeTypeAlias)) {
return;
}
@@ -148,12 +149,12 @@ class Detection implements IMimeTypeDetector {
/**
* @return string[]
*/
- public function getAllAliases() {
+ public function getAllAliases(): array {
$this->loadAliases();
return $this->mimeTypeAlias;
}
- public function getOnlyDefaultAliases() {
+ public function getOnlyDefaultAliases(): array {
$this->loadMappings();
$this->mimeTypeAlias = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypealiases.dist.json'), true);
return $this->mimeTypeAlias;
@@ -162,7 +163,7 @@ class Detection implements IMimeTypeDetector {
/**
* Add mimetype mappings if they are not yet present
*/
- private function loadMappings() {
+ private function loadMappings(): void {
if (!empty($this->mimetypes)) {
return;
}
@@ -176,7 +177,7 @@ class Detection implements IMimeTypeDetector {
/**
* @return array
*/
- public function getAllMappings() {
+ public function getAllMappings(): array {
$this->loadMappings();
return $this->mimetypes;
}
@@ -187,7 +188,7 @@ class Detection implements IMimeTypeDetector {
* @param string $path
* @return string
*/
- public function detectPath($path) {
+ public function detectPath($path): string {
$this->loadMappings();
$fileName = basename($path);
@@ -204,12 +205,10 @@ class Detection implements IMimeTypeDetector {
//try to guess the type by the file extension
$extension = strtolower(strrchr($fileName, '.'));
$extension = substr($extension, 1); //remove leading .
- return (isset($this->mimetypes[$extension]) && isset($this->mimetypes[$extension][0]))
- ? $this->mimetypes[$extension][0]
- : 'application/octet-stream';
- } else {
- return 'application/octet-stream';
+ return $this->mimetypes[$extension][0] ?? 'application/octet-stream';
}
+
+ return 'application/octet-stream';
}
/**
@@ -223,12 +222,13 @@ class Detection implements IMimeTypeDetector {
if (@is_dir($path)) {
// directories are easy
- return "httpd/unix-directory";
+ return 'httpd/unix-directory';
}
+ $mimeType = 'application/octet-stream';
if (function_exists('finfo_open')
- and function_exists('finfo_file') and $finfo = finfo_open(FILEINFO_MIME)
- ) {
+ && function_exists('finfo_file')
+ && $finfo = finfo_open(FILEINFO_MIME)) {
$info = @strtolower(finfo_file($finfo, $path));
finfo_close($finfo);
if ($info) {
@@ -237,16 +237,21 @@ class Detection implements IMimeTypeDetector {
}
}
- $isWrapped = (strpos($path, '://') !== false) and (substr($path, 0, 7) === 'file://');
- if (!$isWrapped and function_exists("mime_content_type")) {
+
+ if (strpos($path, '://') !== false && strpos($path, 'file://') === 0) {
+ // Is the file wrapped in a stream?
+ return $mimeType;
+ }
+
+ if (function_exists('mime_content_type')) {
// use mime magic extension if available
$mimeType = mime_content_type($path);
}
- if (!$isWrapped and $mimeType === 'application/octet-stream' && \OC_Helper::canExecute("file")) {
+ if ($mimeType === 'application/octet-stream' && \OC_Helper::canExecute('file')) {
// it looks like we have a 'file' command,
// lets see if it does have mime support
$path = escapeshellarg($path);
- $fp = popen("file -b --mime-type $path 2>/dev/null", "r");
+ $fp = popen("file -b --mime-type $path 2>/dev/null", 'r');
$reply = fgets($fp);
pclose($fp);
@@ -267,7 +272,7 @@ class Detection implements IMimeTypeDetector {
* @param string $path
* @return string
*/
- public function detect($path) {
+ public function detect($path): string {
$mimeType = $this->detectPath($path);
if ($mimeType !== 'application/octet-stream') {
@@ -283,20 +288,20 @@ class Detection implements IMimeTypeDetector {
* @param string $data
* @return string
*/
- public function detectString($data) {
- if (function_exists('finfo_open') and function_exists('finfo_file')) {
+ public function detectString($data): string {
+ if (function_exists('finfo_open') && function_exists('finfo_file')) {
$finfo = finfo_open(FILEINFO_MIME);
$info = finfo_buffer($finfo, $data);
return strpos($info, ';') !== false ? substr($info, 0, strpos($info, ';')) : $info;
- } else {
- $tmpFile = \OC::$server->getTempManager()->getTemporaryFile();
- $fh = fopen($tmpFile, 'wb');
- fwrite($fh, $data, 8024);
- fclose($fh);
- $mime = $this->detect($tmpFile);
- unset($tmpFile);
- return $mime;
}
+
+ $tmpFile = \OC::$server->getTempManager()->getTemporaryFile();
+ $fh = fopen($tmpFile, 'wb');
+ fwrite($fh, $data, 8024);
+ fclose($fh);
+ $mime = $this->detect($tmpFile);
+ unset($tmpFile);
+ return $mime;
}
/**
@@ -305,12 +310,10 @@ class Detection implements IMimeTypeDetector {
* @param string $mimeType
* @return string
*/
- public function getSecureMimeType($mimeType) {
+ public function getSecureMimeType($mimeType): string {
$this->loadMappings();
- return isset($this->secureMimeTypes[$mimeType])
- ? $this->secureMimeTypes[$mimeType]
- : 'application/octet-stream';
+ return $this->secureMimeTypes[$mimeType] ?? 'application/octet-stream';
}
/**
@@ -318,7 +321,7 @@ class Detection implements IMimeTypeDetector {
* @param string $mimetype the MIME type
* @return string the url
*/
- public function mimeTypeIcon($mimetype) {
+ public function mimeTypeIcon($mimetype): string {
$this->loadAliases();
while (isset($this->mimeTypeAlias[$mimetype])) {
@@ -329,8 +332,7 @@ class Detection implements IMimeTypeDetector {
}
// Replace slash and backslash with a minus
- $icon = str_replace('/', '-', $mimetype);
- $icon = str_replace('\\', '-', $icon);
+ $icon = str_replace(['/', '\\'], '-', $mimetype);
// Is it a dir?
if ($mimetype === 'dir') {