2014-06-25 21:34:39 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Olivier Paroz <github@oparoz.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Rello <Rello@users.noreply.github.com>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Stefan Weil <sw@weilnetz.de>
|
|
|
|
* @author Thomas Ebert <thomas.ebert@usability.de>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-06-25 12:43:55 +03:00
|
|
|
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
2014-06-25 21:34:39 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2014-06-25 21:34:39 +04:00
|
|
|
namespace OC\Repair;
|
|
|
|
|
2020-11-06 12:26:41 +03:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IDBConnection;
|
2016-04-22 16:35:39 +03:00
|
|
|
use OCP\Migration\IOutput;
|
|
|
|
use OCP\Migration\IRepairStep;
|
2014-06-25 21:34:39 +04:00
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
class RepairMimeTypes implements IRepairStep {
|
2020-11-06 12:26:41 +03:00
|
|
|
/** @var IConfig */
|
2015-08-25 15:09:38 +03:00
|
|
|
protected $config;
|
2020-11-06 12:26:41 +03:00
|
|
|
/** @var IDBConnection */
|
|
|
|
protected $connection;
|
2015-08-25 15:09:38 +03:00
|
|
|
|
2020-11-06 12:26:41 +03:00
|
|
|
/** @var int */
|
2015-10-09 19:16:36 +03:00
|
|
|
protected $folderMimeTypeId;
|
|
|
|
|
2020-11-06 12:26:41 +03:00
|
|
|
public function __construct(IConfig $config,
|
|
|
|
IDBConnection $connection) {
|
2015-08-25 15:09:38 +03:00
|
|
|
$this->config = $config;
|
2020-11-06 12:26:41 +03:00
|
|
|
$this->connection = $connection;
|
2015-08-25 15:09:38 +03:00
|
|
|
}
|
2014-06-25 21:34:39 +04:00
|
|
|
|
|
|
|
public function getName() {
|
|
|
|
return 'Repair mime types';
|
|
|
|
}
|
2015-03-21 00:11:42 +03:00
|
|
|
|
2014-11-26 21:58:59 +03:00
|
|
|
private function updateMimetypes($updatedMimetypes) {
|
2020-11-06 12:26:41 +03:00
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select('id')
|
|
|
|
->from('mimetypes')
|
|
|
|
->where($query->expr()->eq('mimetype', $query->createParameter('mimetype'), IQueryBuilder::PARAM_INT));
|
|
|
|
$insert = $this->connection->getQueryBuilder();
|
|
|
|
$insert->insert('mimetypes')
|
|
|
|
->setValue('mimetype', $insert->createParameter('mimetype'));
|
|
|
|
|
2015-10-09 19:16:36 +03:00
|
|
|
if (empty($this->folderMimeTypeId)) {
|
2020-11-06 12:26:41 +03:00
|
|
|
$query->setParameter('mimetype', 'httpd/unix-directory');
|
|
|
|
$result = $query->execute();
|
|
|
|
$this->folderMimeTypeId = (int)$result->fetchColumn();
|
|
|
|
$result->closeCursor();
|
2015-10-09 19:16:36 +03:00
|
|
|
}
|
2015-03-21 00:11:42 +03:00
|
|
|
|
2020-11-06 12:26:41 +03:00
|
|
|
$update = $this->connection->getQueryBuilder();
|
|
|
|
$update->update('filecache')
|
|
|
|
->set('mimetype', $update->createParameter('mimetype'))
|
|
|
|
->where($update->expr()->neq('mimetype', $update->createParameter('mimetype'), IQueryBuilder::PARAM_INT))
|
|
|
|
->andWhere($update->expr()->neq('mimetype', $update->createParameter('folder'), IQueryBuilder::PARAM_INT))
|
|
|
|
->andWhere($update->expr()->iLike('name', $update->createParameter('name')))
|
|
|
|
->setParameter('folder', $this->folderMimeTypeId);
|
|
|
|
|
2017-10-31 16:04:14 +03:00
|
|
|
$count = 0;
|
2015-03-21 00:11:42 +03:00
|
|
|
foreach ($updatedMimetypes as $extension => $mimetype) {
|
2020-11-06 12:26:41 +03:00
|
|
|
// get target mimetype id
|
|
|
|
$query->setParameter('mimetype', $mimetype);
|
|
|
|
$result = $query->execute();
|
|
|
|
$mimetypeId = (int)$result->fetchColumn();
|
|
|
|
$result->closeCursor();
|
2014-06-25 21:34:39 +04:00
|
|
|
|
2020-11-06 12:26:41 +03:00
|
|
|
if (!$mimetypeId) {
|
2014-06-25 21:34:39 +04:00
|
|
|
// insert mimetype
|
2020-11-06 12:26:41 +03:00
|
|
|
$insert->setParameter('mimetype', $mimetype);
|
|
|
|
$insert->execute();
|
|
|
|
$mimetypeId = $insert->getLastInsertId();
|
2014-06-25 21:34:39 +04:00
|
|
|
}
|
2017-08-31 17:20:21 +03:00
|
|
|
|
2014-06-25 21:34:39 +04:00
|
|
|
// change mimetype for files with x extension
|
2020-11-06 12:26:41 +03:00
|
|
|
$update->setParameter('mimetype', $mimetypeId)
|
|
|
|
->setParameter('name', '%' . $this->connection->escapeLikeParameter('.' . $extension));
|
|
|
|
$count += $update->execute();
|
2014-06-25 21:34:39 +04:00
|
|
|
}
|
2017-10-31 16:04:14 +03:00
|
|
|
|
|
|
|
return $count;
|
2014-06-25 21:34:39 +04:00
|
|
|
}
|
|
|
|
|
2017-04-25 19:56:23 +03:00
|
|
|
private function introduceImageTypes() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$updatedMimetypes = [
|
2017-04-25 19:56:23 +03:00
|
|
|
'jp2' => 'image/jp2',
|
|
|
|
'webp' => 'image/webp',
|
2020-03-26 11:30:18 +03:00
|
|
|
];
|
2017-04-25 19:56:23 +03:00
|
|
|
|
2017-10-31 16:04:14 +03:00
|
|
|
return $this->updateMimetypes($updatedMimetypes);
|
2017-04-25 19:56:23 +03:00
|
|
|
}
|
|
|
|
|
2017-02-10 16:25:25 +03:00
|
|
|
private function introduceWindowsProgramTypes() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$updatedMimetypes = [
|
2017-02-10 16:25:25 +03:00
|
|
|
'htaccess' => 'text/plain',
|
|
|
|
'bat' => 'application/x-msdos-program',
|
|
|
|
'cmd' => 'application/cmd',
|
2020-03-26 11:30:18 +03:00
|
|
|
];
|
2017-02-10 16:25:25 +03:00
|
|
|
|
2017-10-31 16:04:14 +03:00
|
|
|
return $this->updateMimetypes($updatedMimetypes);
|
2017-02-10 16:25:25 +03:00
|
|
|
}
|
|
|
|
|
2017-06-14 11:48:56 +03:00
|
|
|
private function introduceLocationTypes() {
|
|
|
|
$updatedMimetypes = [
|
2017-06-14 12:02:05 +03:00
|
|
|
'gpx' => 'application/gpx+xml',
|
|
|
|
'kml' => 'application/vnd.google-earth.kml+xml',
|
|
|
|
'kmz' => 'application/vnd.google-earth.kmz',
|
|
|
|
'tcx' => 'application/vnd.garmin.tcx+xml',
|
2017-06-14 11:48:56 +03:00
|
|
|
];
|
|
|
|
|
2017-10-31 16:04:14 +03:00
|
|
|
return $this->updateMimetypes($updatedMimetypes);
|
2017-06-14 11:48:56 +03:00
|
|
|
}
|
|
|
|
|
2017-08-31 17:20:21 +03:00
|
|
|
private function introduceInternetShortcutTypes() {
|
|
|
|
$updatedMimetypes = [
|
|
|
|
'url' => 'application/internet-shortcut',
|
|
|
|
'webloc' => 'application/internet-shortcut'
|
|
|
|
];
|
|
|
|
|
2017-10-31 16:04:14 +03:00
|
|
|
return $this->updateMimetypes($updatedMimetypes);
|
2017-08-31 17:20:21 +03:00
|
|
|
}
|
|
|
|
|
2017-08-31 18:48:49 +03:00
|
|
|
private function introduceStreamingTypes() {
|
|
|
|
$updatedMimetypes = [
|
|
|
|
'm3u' => 'audio/mpegurl',
|
|
|
|
'm3u8' => 'audio/mpegurl',
|
|
|
|
'pls' => 'audio/x-scpls'
|
|
|
|
];
|
|
|
|
|
2017-10-31 16:04:14 +03:00
|
|
|
return $this->updateMimetypes($updatedMimetypes);
|
2017-08-31 17:20:21 +03:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:16:21 +03:00
|
|
|
private function introduceVisioTypes() {
|
|
|
|
$updatedMimetypes = [
|
|
|
|
'vsdm' => 'application/vnd.visio',
|
|
|
|
'vsdx' => 'application/vnd.visio',
|
|
|
|
'vssm' => 'application/vnd.visio',
|
|
|
|
'vssx' => 'application/vnd.visio',
|
|
|
|
'vstm' => 'application/vnd.visio',
|
|
|
|
'vstx' => 'application/vnd.visio',
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->updateMimetypes($updatedMimetypes);
|
|
|
|
}
|
|
|
|
|
2018-07-10 22:27:35 +03:00
|
|
|
private function introduceComicbookTypes() {
|
|
|
|
$updatedMimetypes = [
|
|
|
|
'cb7' => 'application/comicbook+7z',
|
|
|
|
'cba' => 'application/comicbook+ace',
|
|
|
|
'cbr' => 'application/comicbook+rar',
|
|
|
|
'cbt' => 'application/comicbook+tar',
|
|
|
|
'cbtc' => 'application/comicbook+truecrypt',
|
|
|
|
'cbz' => 'application/comicbook+zip',
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->updateMimetypes($updatedMimetypes);
|
|
|
|
}
|
|
|
|
|
2020-09-04 09:42:32 +03:00
|
|
|
private function introduceOpenDocumentTemplates() {
|
|
|
|
$updatedMimetypes = [
|
|
|
|
'ott' => 'application/vnd.oasis.opendocument.text-template',
|
|
|
|
'ots' => 'application/vnd.oasis.opendocument.spreadsheet-template',
|
|
|
|
'otp' => 'application/vnd.oasis.opendocument.presentation-template',
|
|
|
|
'otg' => 'application/vnd.oasis.opendocument.graphics-template',
|
|
|
|
];
|
|
|
|
|
|
|
|
return $this->updateMimetypes($updatedMimetypes);
|
|
|
|
}
|
|
|
|
|
2014-06-25 21:34:39 +04:00
|
|
|
/**
|
|
|
|
* Fix mime types
|
|
|
|
*/
|
2016-04-22 16:35:39 +03:00
|
|
|
public function run(IOutput $out) {
|
2015-08-25 15:09:38 +03:00
|
|
|
$ocVersionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
|
2015-03-21 00:11:42 +03:00
|
|
|
|
2015-08-25 15:09:38 +03:00
|
|
|
// NOTE TO DEVELOPERS: when adding new mime types, please make sure to
|
|
|
|
// add a version comparison to avoid doing it every time
|
2015-03-21 00:11:42 +03:00
|
|
|
|
2017-04-25 19:56:23 +03:00
|
|
|
if (version_compare($ocVersionFromBeforeUpdate, '12.0.0.14', '<') && $this->introduceImageTypes()) {
|
|
|
|
$out->info('Fixed image mime types');
|
|
|
|
}
|
|
|
|
|
2017-02-10 16:25:25 +03:00
|
|
|
if (version_compare($ocVersionFromBeforeUpdate, '12.0.0.13', '<') && $this->introduceWindowsProgramTypes()) {
|
|
|
|
$out->info('Fixed windows program mime types');
|
|
|
|
}
|
2017-06-14 11:48:56 +03:00
|
|
|
|
|
|
|
if (version_compare($ocVersionFromBeforeUpdate, '13.0.0.0', '<') && $this->introduceLocationTypes()) {
|
|
|
|
$out->info('Fixed geospatial mime types');
|
|
|
|
}
|
2017-08-31 17:20:21 +03:00
|
|
|
|
|
|
|
if (version_compare($ocVersionFromBeforeUpdate, '13.0.0.3', '<') && $this->introduceInternetShortcutTypes()) {
|
|
|
|
$out->info('Fixed internet-shortcut mime types');
|
|
|
|
}
|
2017-08-31 18:48:49 +03:00
|
|
|
|
|
|
|
if (version_compare($ocVersionFromBeforeUpdate, '13.0.0.6', '<') && $this->introduceStreamingTypes()) {
|
|
|
|
$out->info('Fixed streaming mime types');
|
|
|
|
}
|
2018-07-06 16:16:21 +03:00
|
|
|
|
|
|
|
if (version_compare($ocVersionFromBeforeUpdate, '14.0.0.8', '<') && $this->introduceVisioTypes()) {
|
|
|
|
$out->info('Fixed visio mime types');
|
|
|
|
}
|
2018-07-10 22:27:35 +03:00
|
|
|
|
|
|
|
if (version_compare($ocVersionFromBeforeUpdate, '14.0.0.10', '<') && $this->introduceComicbookTypes()) {
|
|
|
|
$out->info('Fixed comicbook mime types');
|
|
|
|
}
|
2020-09-04 09:42:32 +03:00
|
|
|
|
|
|
|
if (version_compare($ocVersionFromBeforeUpdate, '20.0.0.5', '<') && $this->introduceOpenDocumentTemplates()) {
|
|
|
|
$out->info('Fixed OpenDocument template mime types');
|
|
|
|
}
|
2014-06-25 21:34:39 +04:00
|
|
|
}
|
2015-03-16 18:25:04 +03:00
|
|
|
}
|