diff options
author | Roeland Douma <rullzer@users.noreply.github.com> | 2015-09-06 16:56:35 +0200 |
---|---|---|
committer | Roeland Douma <rullzer@users.noreply.github.com> | 2015-09-06 16:56:35 +0200 |
commit | 24f5f50b20c4f49bcd602a3c322f2ee2deb0f95b (patch) | |
tree | b5ba1876566dfa1cc39341e8f58e419bc137d722 /core | |
parent | 3642fb701a7fc0caba779de9f5d53bf12c27f5aa (diff) | |
parent | c6314fc699c7316973fa79f75b7d585e620323e9 (diff) | |
download | nextcloud-server-24f5f50b20c4f49bcd602a3c322f2ee2deb0f95b.tar.gz nextcloud-server-24f5f50b20c4f49bcd602a3c322f2ee2deb0f95b.zip |
Merge pull request #18742 from owncloud/mimetype-updatedb
Introduce mimetype DB update occ command
Diffstat (limited to 'core')
-rw-r--r-- | core/command/maintenance/mimetype/updatedb.php | 96 | ||||
-rw-r--r-- | core/command/maintenance/mimetype/updatejs.php (renamed from core/command/maintenance/mimetypesjs.php) | 43 | ||||
-rw-r--r-- | core/register_command.php | 3 |
3 files changed, 123 insertions, 19 deletions
diff --git a/core/command/maintenance/mimetype/updatedb.php b/core/command/maintenance/mimetype/updatedb.php new file mode 100644 index 00000000000..37c401c0338 --- /dev/null +++ b/core/command/maintenance/mimetype/updatedb.php @@ -0,0 +1,96 @@ +<?php +/** + * @author Robin McCorkell <rmccorkell@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + */ + +namespace OC\Core\Command\Maintenance\Mimetype; + +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Input\InputOption; + +use OCP\Files\IMimeTypeDetector; +use OCP\Files\IMimeTypeLoader; + +class UpdateDB extends Command { + + const DEFAULT_MIMETYPE = 'application/octet-stream'; + + /** @var IMimeTypeDetector */ + protected $mimetypeDetector; + + /** @var IMimeTypeLoader */ + protected $mimetypeLoader; + + public function __construct( + IMimeTypeDetector $mimetypeDetector, + IMimeTypeLoader $mimetypeLoader + ) { + parent::__construct(); + $this->mimetypeDetector = $mimetypeDetector; + $this->mimetypeLoader = $mimetypeLoader; + } + + protected function configure() { + $this + ->setName('maintenance:mimetype:update-db') + ->setDescription('Update database mimetypes and update filecache') + ->addOption( + 'repair-filecache', + null, + InputOption::VALUE_NONE, + 'Repair filecache for all mimetypes, not just new ones' + ) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $mappings = $this->mimetypeDetector->getAllMappings(); + + $totalFilecacheUpdates = 0; + $totalNewMimetypes = 0; + + foreach ($mappings as $ext => $mimetypes) { + if ($ext[0] === '_') { + // comment + continue; + } + $mimetype = $mimetypes[0]; + $existing = $this->mimetypeLoader->exists($mimetype); + // this will add the mimetype if it didn't exist + $mimetypeId = $this->mimetypeLoader->getId($mimetype); + + if (!$existing) { + $output->writeln('Added mimetype "'.$mimetype.'" to database'); + $totalNewMimetypes++; + } + + if (!$existing || $input->getOption('repair-filecache')) { + $touchedFilecacheRows = $this->mimetypeLoader->updateFilecache($ext, $mimetypeId); + if ($touchedFilecacheRows > 0) { + $output->writeln('Updated '.$touchedFilecacheRows.' filecache rows for mimetype "'.$mimetype.'"'); + } + $totalFilecacheUpdates += $touchedFilecacheRows; + } + } + + $output->writeln('Added '.$totalNewMimetypes.' new mimetypes'); + $output->writeln('Updated '.$totalFilecacheUpdates.' filecache rows'); + } +} diff --git a/core/command/maintenance/mimetypesjs.php b/core/command/maintenance/mimetype/updatejs.php index 8b01f0acf79..5de75d53a3f 100644 --- a/core/command/maintenance/mimetypesjs.php +++ b/core/command/maintenance/mimetype/updatejs.php @@ -18,27 +18,35 @@ * */ -namespace OC\Core\Command\Maintenance; +namespace OC\Core\Command\Maintenance\Mimetype; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -class MimeTypesJS extends Command { +use OCP\Files\IMimeTypeDetector; + +class UpdateJS extends Command { + + /** @var IMimeTypeDetector */ + protected $mimetypeDetector; + + public function __construct( + IMimeTypeDetector $mimetypeDetector + ) { + parent::__construct(); + $this->mimetypeDetector = $mimetypeDetector; + } + protected function configure() { $this - ->setName('maintenance:mimetypesjs') + ->setName('maintenance:mimetype:update-js') ->setDescription('Update mimetypelist.js'); } protected function execute(InputInterface $input, OutputInterface $output) { // Fetch all the aliases - $aliases = json_decode(file_get_contents(\OC::$SERVERROOT . '/config/mimetypealiases.dist.json'), true); - - if (file_exists(\OC::$SERVERROOT . '/config/mimetypealiases.json')) { - $custom = get_object_vars(json_decode(file_get_contents(\OC::$SERVERROOT . '/config/mimetypealiases.json'))); - $aliases = array_merge($aliases, $custom); - } + $aliases = $this->mimetypeDetector->getAllAliases(); // Remove comments $keys = array_filter(array_keys($aliases), function($k) { @@ -49,23 +57,22 @@ class MimeTypesJS extends Command { } // Fetch all files - $dir = new \DirectoryIterator(dirname(__DIR__) . '/../img/filetypes'); + $dir = new \DirectoryIterator(\OC::$SERVERROOT.'/core/img/filetypes'); $files = []; foreach($dir as $fileInfo) { - if ($fileInfo->isFile()) { - $file = preg_replace('/.[^.]*$/', '', $fileInfo->getFilename()); - $files[] = $file; - } + if ($fileInfo->isFile()) { + $file = preg_replace('/.[^.]*$/', '', $fileInfo->getFilename()); + $files[] = $file; + } } //Remove duplicates $files = array_values(array_unique($files)); - // Fetch all themes! $themes = []; - $dirs = new \DirectoryIterator(dirname(__DIR__) . '/../../themes/'); + $dirs = new \DirectoryIterator(\OC::$SERVERROOT.'/themes/'); foreach($dirs as $dir) { //Valid theme dir if ($dir->isFile() || $dir->isDot()) { @@ -84,7 +91,7 @@ class MimeTypesJS extends Command { $themeIt = new \DirectoryIterator($themeDir); foreach ($themeIt as $fileInfo) { if ($fileInfo->isFile()) { - $file = preg_replace('/.[^.]*$/', '', $fileInfo->getFilename()); + $file = preg_replace('/.[^.]*$/', '', $fileInfo->getFilename()); $themes[$theme][] = $file; } } @@ -110,7 +117,7 @@ OC.MimeTypeList={ '; //Output the JS - file_put_contents(dirname(__DIR__) . '/../js/mimetypelist.js', $js); + file_put_contents(\OC::$SERVERROOT.'/core/js/mimetypelist.js', $js); $output->writeln('<info>mimetypelist.js is updated'); } diff --git a/core/register_command.php b/core/register_command.php index 72c7b28e9ae..d3c04ad0671 100644 --- a/core/register_command.php +++ b/core/register_command.php @@ -79,7 +79,8 @@ if (\OC::$server->getConfig()->getSystemValue('installed', false)) { ); $application->add(new OC\Core\Command\Encryption\ShowKeyStorageRoot($util)); - $application->add(new OC\Core\Command\Maintenance\MimeTypesJS()); + $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\Mode(\OC::$server->getConfig())); $application->add(new OC\Core\Command\Maintenance\Repair(new \OC\Repair(\OC\Repair::getRepairSteps()), \OC::$server->getConfig())); $application->add(new OC\Core\Command\Maintenance\SingleUser(\OC::$server->getConfig())); |