From 3479b1cd1a807ff064f7f279c8477922d208fa49 Mon Sep 17 00:00:00 2001 From: Andrew Summers <18727110+summersab@users.noreply.github.com> Date: Tue, 29 Aug 2023 18:46:24 -0500 Subject: [PATCH] Refactor `OC\Server::getMimeTypeDetector` Signed-off-by: Andrew Summers <18727110+summersab@users.noreply.github.com> --- core/register_command.php | 8 +++++--- lib/private/Files/ObjectStore/ObjectStoreStorage.php | 5 +++-- lib/private/Files/Storage/Common.php | 3 ++- lib/private/Files/Storage/DAV.php | 2 +- lib/private/legacy/OC_Files.php | 5 +++-- lib/private/legacy/template/functions.php | 5 ++++- lib/public/Files.php | 4 +++- 7 files changed, 21 insertions(+), 11 deletions(-) diff --git a/core/register_command.php b/core/register_command.php index d9e5dfcd775eb..c16f99420d60e 100644 --- a/core/register_command.php +++ b/core/register_command.php @@ -48,6 +48,8 @@ * along with this program. If not, see * */ + +use OCP\Files\IMimeTypeDetector; use Psr\Log\LoggerInterface; $application->add(new \Stecman\Component\Symfony\Console\BashCompletion\CompletionCommand()); @@ -163,11 +165,11 @@ ); $application->add(new OC\Core\Command\Maintenance\DataFingerprint(\OC::$server->getConfig(), new \OC\AppFramework\Utility\TimeFactory())); - $application->add(new OC\Core\Command\Maintenance\Mimetype\UpdateDB(\OC::$server->getMimeTypeDetector(), \OC::$server->getMimeTypeLoader())); - $application->add(new OC\Core\Command\Maintenance\Mimetype\UpdateJS(\OC::$server->getMimeTypeDetector())); + $application->add(new OC\Core\Command\Maintenance\Mimetype\UpdateDB(\OC::$server->get(IMimeTypeDetector::class), \OC::$server->getMimeTypeLoader())); + $application->add(new OC\Core\Command\Maintenance\Mimetype\UpdateJS(\OC::$server->get(IMimeTypeDetector::class))); $application->add(new OC\Core\Command\Maintenance\Mode(\OC::$server->getConfig())); $application->add(new OC\Core\Command\Maintenance\UpdateHtaccess()); - $application->add(new OC\Core\Command\Maintenance\UpdateTheme(\OC::$server->getMimeTypeDetector(), \OC::$server->getMemCacheFactory())); + $application->add(new OC\Core\Command\Maintenance\UpdateTheme(\OC::$server->get(IMimeTypeDetector::class), \OC::$server->getMemCacheFactory())); $application->add(new OC\Core\Command\Upgrade(\OC::$server->getConfig(), \OC::$server->get(LoggerInterface::class), \OC::$server->query(\OC\Installer::class))); $application->add(new OC\Core\Command\Maintenance\Repair( diff --git a/lib/private/Files/ObjectStore/ObjectStoreStorage.php b/lib/private/Files/ObjectStore/ObjectStoreStorage.php index d918bd9872971..803cbf4aac103 100644 --- a/lib/private/Files/ObjectStore/ObjectStoreStorage.php +++ b/lib/private/Files/ObjectStore/ObjectStoreStorage.php @@ -42,6 +42,7 @@ use OCP\Files\Cache\ICacheEntry; use OCP\Files\FileInfo; use OCP\Files\GenericFileException; +use OCP\Files\IMimeTypeDetector; use OCP\Files\NotFoundException; use OCP\Files\ObjectStore\IObjectStore; use OCP\Files\ObjectStore\IObjectStoreMultiPartUpload; @@ -436,7 +437,7 @@ public function touch($path, $mtime = null) { //create a empty file, need to have at least on char to make it // work with all object storage implementations $this->file_put_contents($path, ' '); - $mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path); + $mimeType = \OC::$server->get(IMimeTypeDetector::class)->detectPath($path); $stat = [ 'etag' => $this->getETag($path), 'mimetype' => $mimeType, @@ -501,7 +502,7 @@ public function writeStream(string $path, $stream, int $size = null): int { $stat['mtime'] = $mTime; $stat['storage_mtime'] = $mTime; - $mimetypeDetector = \OC::$server->getMimeTypeDetector(); + $mimetypeDetector = \OC::$server->get(IMimeTypeDetector::class); $mimetype = $mimetypeDetector->detectPath($path); $stat['mimetype'] = $mimetype; diff --git a/lib/private/Files/Storage/Common.php b/lib/private/Files/Storage/Common.php index 5ab411434d0b5..c1bc6e54ecb72 100644 --- a/lib/private/Files/Storage/Common.php +++ b/lib/private/Files/Storage/Common.php @@ -54,6 +54,7 @@ use OCP\Files\FileNameTooLongException; use OCP\Files\ForbiddenException; use OCP\Files\GenericFileException; +use OCP\Files\IMimeTypeDetector; use OCP\Files\InvalidCharacterInPathException; use OCP\Files\InvalidDirectoryException; use OCP\Files\InvalidPathException; @@ -254,7 +255,7 @@ public function getMimeType($path) { if ($this->is_dir($path)) { return 'httpd/unix-directory'; } elseif ($this->file_exists($path)) { - return \OC::$server->getMimeTypeDetector()->detectPath($path); + return \OC::$server->get(IMimeTypeDetector::class)->detectPath($path); } else { return false; } diff --git a/lib/private/Files/Storage/DAV.php b/lib/private/Files/Storage/DAV.php index 70f22a1703400..6846cad515fbb 100644 --- a/lib/private/Files/Storage/DAV.php +++ b/lib/private/Files/Storage/DAV.php @@ -154,7 +154,7 @@ public function __construct($params) { $this->eventLogger = \OC::$server->get(IEventLogger::class); // This timeout value will be used for the download and upload of files $this->timeout = \OC::$server->get(IConfig::class)->getSystemValueInt('davstorage.request_timeout', 30); - $this->mimeTypeDetector = \OC::$server->getMimeTypeDetector(); + $this->mimeTypeDetector = \OC::$server->get(IMimeTypeDetector::class); } protected function init() { diff --git a/lib/private/legacy/OC_Files.php b/lib/private/legacy/OC_Files.php index 7bc1fab94b6b4..d05e7792eed43 100644 --- a/lib/private/legacy/OC_Files.php +++ b/lib/private/legacy/OC_Files.php @@ -44,6 +44,7 @@ use OC\Files\View; use OC\Streamer; use OCP\Lock\ILockingProvider; +use OCP\Files\IMimeTypeDetector; use OCP\Files\Events\BeforeZipCreatedEvent; use OCP\Files\Events\BeforeDirectFileDownloadEvent; use OCP\EventDispatcher\IEventDispatcher; @@ -80,7 +81,7 @@ private static function sendHeaders($filename, $name, array $rangeArray): void { header('Expires: 0'); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); $fileSize = \OC\Files\Filesystem::filesize($filename); - $type = \OC::$server->getMimeTypeDetector()->getSecureMimeType(\OC\Files\Filesystem::getMimeType($filename)); + $type = \OC::$server->get(IMimeTypeDetector::class)->getSecureMimeType(\OC\Files\Filesystem::getMimeType($filename)); if ($fileSize > -1) { if (!empty($rangeArray)) { http_response_code(206); @@ -361,7 +362,7 @@ private static function getSingleFile($view, $dir, $name, $params) { // we have to check it before body contents $view->readfilePart($filename, $rangeArray[0]['size'], $rangeArray[0]['size']); - $type = \OC::$server->getMimeTypeDetector()->getSecureMimeType(\OC\Files\Filesystem::getMimeType($filename)); + $type = \OC::$server->get(IMimeTypeDetector::class)->getSecureMimeType(\OC\Files\Filesystem::getMimeType($filename)); foreach ($rangeArray as $range) { echo "\r\n--".self::getBoundary()."\r\n". diff --git a/lib/private/legacy/template/functions.php b/lib/private/legacy/template/functions.php index bcc0906dcdfb3..3649bcaf92455 100644 --- a/lib/private/legacy/template/functions.php +++ b/lib/private/legacy/template/functions.php @@ -34,6 +34,9 @@ * along with this program. If not, see * */ + +use OCP\Files\IMimeTypeDetector; + function p($string) { print(\OCP\Util::sanitizeHTML($string)); } @@ -255,7 +258,7 @@ function image_path($app, $image) { * @return string link to the image */ function mimetype_icon($mimetype) { - return \OC::$server->getMimeTypeDetector()->mimeTypeIcon($mimetype); + return \OC::$server->get(IMimeTypeDetector::class)->mimeTypeIcon($mimetype); } /** diff --git a/lib/public/Files.php b/lib/public/Files.php index e6e63b10b0bbd..774b9a4902007 100644 --- a/lib/public/Files.php +++ b/lib/public/Files.php @@ -35,6 +35,8 @@ namespace OCP; +use OCP\Files\IMimeTypeDetector; + /** * This class provides access to the internal filesystem abstraction layer. Use * this class exclusively if you want to access files @@ -61,7 +63,7 @@ public static function rmdirr($dir) { * @deprecated 14.0.0 */ public static function getMimeType($path) { - return \OC::$server->getMimeTypeDetector()->detect($path); + return \OC::$server->get(IMimeTypeDetector::class)->detect($path); } /**