diff options
Diffstat (limited to 'lib/private/files')
-rw-r--r-- | lib/private/files/storage/common.php | 2 | ||||
-rw-r--r-- | lib/private/files/storage/wrapper/encryption.php | 14 | ||||
-rw-r--r-- | lib/private/files/type/detection.php | 127 | ||||
-rw-r--r-- | lib/private/files/utils/scanner.php | 3 | ||||
-rw-r--r-- | lib/private/files/view.php | 4 |
5 files changed, 137 insertions, 13 deletions
diff --git a/lib/private/files/storage/common.php b/lib/private/files/storage/common.php index d006f1fb486..a5ed5fd3996 100644 --- a/lib/private/files/storage/common.php +++ b/lib/private/files/storage/common.php @@ -338,7 +338,7 @@ abstract class Common implements Storage { } if (!isset($this->watcher)) { $this->watcher = new Watcher($storage); - $globalPolicy = \OC::$server->getConfig()->getSystemValue('filesystem_check_changes', Watcher::CHECK_ONCE); + $globalPolicy = \OC::$server->getConfig()->getSystemValue('filesystem_check_changes', Watcher::CHECK_NEVER); $this->watcher->setPolicy((int)$this->getMountOption('filesystem_check_changes', $globalPolicy)); } return $this->watcher; diff --git a/lib/private/files/storage/wrapper/encryption.php b/lib/private/files/storage/wrapper/encryption.php index 61290791faa..4ba9b21ddb4 100644 --- a/lib/private/files/storage/wrapper/encryption.php +++ b/lib/private/files/storage/wrapper/encryption.php @@ -127,12 +127,11 @@ class Encryption extends Wrapper { $info = $this->getCache()->get($path); if (isset($this->unencryptedSize[$fullPath])) { $size = $this->unencryptedSize[$fullPath]; + // update file cache + $info['encrypted'] = true; + $info['size'] = $size; + $this->getCache()->put($path, $info); - if (isset($info['fileid'])) { - $info['encrypted'] = true; - $info['size'] = $size; - $this->getCache()->put($path, $info); - } return $size; } @@ -419,10 +418,11 @@ class Encryption extends Wrapper { } if ($shouldEncrypt === true && $encryptionModule !== null) { + $headerSize = $this->getHeaderSize($path); $source = $this->storage->fopen($path, $mode); $handle = \OC\Files\Stream\Encryption::wrap($source, $path, $fullPath, $header, $this->uid, $encryptionModule, $this->storage, $this, $this->util, $this->fileHelper, $mode, - $size, $unencryptedSize, $this->getHeaderSize($path)); + $size, $unencryptedSize, $headerSize); return $handle; } @@ -521,7 +521,7 @@ class Encryption extends Wrapper { if ($preserveMtime) { $this->touch($targetInternalPath, $sourceStorage->filemtime($sourceInternalPath)); } - $isEncrypted = $this->mount->getOption('encrypt', true) ? 1 : 0; + $isEncrypted = $this->encryptionManager->isEnabled() && $this->mount->getOption('encrypt', true) ? 1 : 0; // in case of a rename we need to manipulate the source cache because // this information will be kept for the new target diff --git a/lib/private/files/type/detection.php b/lib/private/files/type/detection.php index 3287375bc79..ba286637df3 100644 --- a/lib/private/files/type/detection.php +++ b/lib/private/files/type/detection.php @@ -4,6 +4,7 @@ * @author Jens-Christian Fischer <jens-christian.fischer@switch.ch> * @author Morris Jobke <hey@morrisjobke.de> * @author Robin Appelman <icewind@owncloud.com> + * @author Roeland Jago Douma <roeland@famdouma.nl> * @author Thomas Tanghus <thomas@tanghus.net> * * @copyright Copyright (c) 2015, ownCloud, Inc. @@ -25,6 +26,9 @@ namespace OC\Files\Type; +use OCP\Files\IMimeTypeDetector; +use OCP\IURLGenerator; + /** * Class Detection * @@ -32,9 +36,28 @@ namespace OC\Files\Type; * * @package OC\Files\Type */ -class Detection { - protected $mimetypes = array(); - protected $secureMimeTypes = array(); +class Detection implements IMimeTypeDetector { + protected $mimetypes = []; + protected $secureMimeTypes = []; + + protected $mimetypeIcons = []; + /** @var string[] */ + protected $mimeTypeAlias = []; + + /** @var IURLGenerator */ + private $urlGenerator; + + /** @var string */ + private $configDir; + + /** + * @param IURLGenerator $urlGenerator + * @param string $configDir + */ + public function __construct(IURLGenerator $urlGenerator, $configDir) { + $this->urlGenerator = $urlGenerator; + $this->configDir = $configDir; + } /** * Add an extension -> mimetype mapping @@ -71,12 +94,52 @@ class Detection { } /** + * Add the mimetype aliases if they are not yet present + */ + private function loadAliases() { + if (!empty($this->mimeTypeAlias)) { + return; + } + + $file = file_get_contents($this->configDir . '/mimetypealiases.dist.json'); + $this->mimeTypeAlias = get_object_vars(json_decode($file)); + + if (file_exists($this->configDir . '/mimetypealiases.json')) { + $custom = get_object_vars(json_decode(file_get_contents($this->configDir . '/mimetypealiases.json'))); + $this->mimeTypeAlias = array_merge($this->mimeTypeAlias, $custom); + } + } + + /** + * Add mimetype mappings if they are not yet present + */ + private function loadMappings() { + if (!empty($this->mimetypes)) { + return; + } + + $dist = file_get_contents($this->configDir . '/mimetypemapping.dist.json'); + $mimetypemapping = get_object_vars(json_decode($dist)); + + //Check if need to load custom mappings + if (file_exists($this->configDir . '/mimetypemapping.json')) { + $custom = file_get_contents($this->configDir . '/mimetypemapping.json'); + $custom_mapping = get_object_vars(json_decode($custom)); + $mimetypemapping = array_merge($mimetypemapping, $custom_mapping); + } + + $this->registerTypeArray($mimetypemapping); + } + + /** * detect mimetype only based on filename, content of file is not used * * @param string $path * @return string */ public function detectPath($path) { + $this->loadMappings(); + if (strpos($path, '.')) { //try to guess the type by the file extension $extension = strtolower(strrchr(basename($path), ".")); @@ -96,6 +159,8 @@ class Detection { * @return string */ public function detect($path) { + $this->loadMappings(); + if (@is_dir($path)) { // directories are easy return "httpd/unix-directory"; @@ -166,8 +231,64 @@ class Detection { * @return string */ public function getSecureMimeType($mimeType) { + $this->loadMappings(); + return isset($this->secureMimeTypes[$mimeType]) ? $this->secureMimeTypes[$mimeType] : 'application/octet-stream'; } + + /** + * Get path to the icon of a file type + * @param string $mimeType the MIME type + * @return string the url + */ + public function mimeTypeIcon($mimetype) { + $this->loadAliases(); + + if (isset($this->mimeTypeAlias[$mimetype])) { + $mimetype = $this->mimeTypeAlias[$mimetype]; + } + if (isset($this->mimetypeIcons[$mimetype])) { + return $this->mimetypeIcons[$mimetype]; + } + + // Replace slash and backslash with a minus + $icon = str_replace('/', '-', $mimetype); + $icon = str_replace('\\', '-', $icon); + + // Is it a dir? + if ($mimetype === 'dir') { + $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/folder.png'); + return $this->mimetypeIcons[$mimetype]; + } + if ($mimetype === 'dir-shared') { + $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/folder-shared.png'); + return $this->mimetypeIcons[$mimetype]; + } + if ($mimetype === 'dir-external') { + $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/folder-external.png'); + return $this->mimetypeIcons[$mimetype]; + } + + // Icon exists? + try { + $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/' . $icon . '.png'); + return $this->mimetypeIcons[$mimetype]; + } catch (\RuntimeException $e) { + // Specified image not found + } + + // Try only the first part of the filetype + $mimePart = substr($icon, 0, strpos($icon, '-')); + try { + $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/' . $mimePart . '.png'); + return $this->mimetypeIcons[$mimetype]; + } catch (\RuntimeException $e) { + // Image for the first part of the mimetype not found + } + + $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/file.png'); + return $this->mimetypeIcons[$mimetype]; + } } diff --git a/lib/private/files/utils/scanner.php b/lib/private/files/utils/scanner.php index 3d68eb530a2..c70f4beb31d 100644 --- a/lib/private/files/utils/scanner.php +++ b/lib/private/files/utils/scanner.php @@ -131,6 +131,9 @@ class Scanner extends PublicEmitter { * @throws \OC\ForbiddenException */ public function scan($dir = '') { + if (!Filesystem::isValidPath($dir)) { + throw new \InvalidArgumentException('Invalid path to scan'); + } $mounts = $this->getMounts($dir); foreach ($mounts as $mount) { if (is_null($mount->getStorage())) { diff --git a/lib/private/files/view.php b/lib/private/files/view.php index cb3c05d2bca..1a6be73d5bb 100644 --- a/lib/private/files/view.php +++ b/lib/private/files/view.php @@ -1656,11 +1656,11 @@ class View { } // verify database - e.g. mysql only 3-byte chars - if (preg_match('%^(?: + if (preg_match('%(?: \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 -)*$%xs', $fileName)) { +)%xs', $fileName)) { throw new InvalidPathException($l10n->t('4-byte characters are not supported in file names')); } |