Skip to content

Commit dbfd000

Browse files
committed
add setup check for needed mimetype migrations
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent d9b37ac commit dbfd000

8 files changed

Lines changed: 137 additions & 27 deletions

File tree

apps/settings/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
'OCA\\Settings\\SetupChecks\\LegacySSEKeyFormat' => $baseDir . '/../lib/SetupChecks/LegacySSEKeyFormat.php',
103103
'OCA\\Settings\\SetupChecks\\MaintenanceWindowStart' => $baseDir . '/../lib/SetupChecks/MaintenanceWindowStart.php',
104104
'OCA\\Settings\\SetupChecks\\MemcacheConfigured' => $baseDir . '/../lib/SetupChecks/MemcacheConfigured.php',
105+
'OCA\\Settings\\SetupChecks\\MimeTypeMigrationAvailable' => $baseDir . '/../lib/SetupChecks/MimeTypeMigrationAvailable.php',
105106
'OCA\\Settings\\SetupChecks\\MysqlUnicodeSupport' => $baseDir . '/../lib/SetupChecks/MysqlUnicodeSupport.php',
106107
'OCA\\Settings\\SetupChecks\\OcxProviders' => $baseDir . '/../lib/SetupChecks/OcxProviders.php',
107108
'OCA\\Settings\\SetupChecks\\OverwriteCliUrl' => $baseDir . '/../lib/SetupChecks/OverwriteCliUrl.php',

apps/settings/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ class ComposerStaticInitSettings
117117
'OCA\\Settings\\SetupChecks\\LegacySSEKeyFormat' => __DIR__ . '/..' . '/../lib/SetupChecks/LegacySSEKeyFormat.php',
118118
'OCA\\Settings\\SetupChecks\\MaintenanceWindowStart' => __DIR__ . '/..' . '/../lib/SetupChecks/MaintenanceWindowStart.php',
119119
'OCA\\Settings\\SetupChecks\\MemcacheConfigured' => __DIR__ . '/..' . '/../lib/SetupChecks/MemcacheConfigured.php',
120+
'OCA\\Settings\\SetupChecks\\MimeTypeMigrationAvailable' => __DIR__ . '/..' . '/../lib/SetupChecks/MimeTypeMigrationAvailable.php',
120121
'OCA\\Settings\\SetupChecks\\MysqlUnicodeSupport' => __DIR__ . '/..' . '/../lib/SetupChecks/MysqlUnicodeSupport.php',
121122
'OCA\\Settings\\SetupChecks\\OcxProviders' => __DIR__ . '/..' . '/../lib/SetupChecks/OcxProviders.php',
122123
'OCA\\Settings\\SetupChecks\\OverwriteCliUrl' => __DIR__ . '/..' . '/../lib/SetupChecks/OverwriteCliUrl.php',

apps/settings/lib/AppInfo/Application.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
use OCA\Settings\SetupChecks\LegacySSEKeyFormat;
4646
use OCA\Settings\SetupChecks\MaintenanceWindowStart;
4747
use OCA\Settings\SetupChecks\MemcacheConfigured;
48+
use OCA\Settings\SetupChecks\MimeTypeMigrationAvailable;
4849
use OCA\Settings\SetupChecks\MysqlUnicodeSupport;
4950
use OCA\Settings\SetupChecks\OcxProviders;
5051
use OCA\Settings\SetupChecks\OverwriteCliUrl;
@@ -176,6 +177,7 @@ public function register(IRegistrationContext $context): void {
176177
$context->registerSetupCheck(LegacySSEKeyFormat::class);
177178
$context->registerSetupCheck(MaintenanceWindowStart::class);
178179
$context->registerSetupCheck(MemcacheConfigured::class);
180+
$context->registerSetupCheck(MimeTypeMigrationAvailable::class);
179181
$context->registerSetupCheck(MysqlUnicodeSupport::class);
180182
$context->registerSetupCheck(OcxProviders::class);
181183
$context->registerSetupCheck(OverwriteCliUrl::class);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
namespace OCA\Settings\SetupChecks;
10+
11+
use OC\Repair\RepairMimeTypes;
12+
use OCP\IL10N;
13+
use OCP\L10N\IFactory;
14+
use OCP\SetupCheck\ISetupCheck;
15+
use OCP\SetupCheck\SetupResult;
16+
17+
class MimeTypeMigrationAvailable implements ISetupCheck {
18+
private IL10N $l10n;
19+
20+
public function __construct(
21+
IFactory $l10nFactory,
22+
private RepairMimeTypes $repairMimeTypes,
23+
) {
24+
$this->l10n = $l10nFactory->get('core');
25+
}
26+
27+
public function getCategory(): string {
28+
return 'system';
29+
}
30+
31+
public function getName(): string {
32+
return $this->l10n->t('Mimetype migrations available');
33+
}
34+
35+
public function run(): SetupResult {
36+
if ($this->repairMimeTypes->migrationsAvailable()) {
37+
return SetupResult::warning(
38+
$this->l10n->t('One or more mimetype migrations are available. Occasionally new mimetypes are added to better handle certain file types. Migrating the mimetypes take a long time on larger instances so this is not done automatically during upgrades. Use the command `occ maintenance:repair --include-expensive` to perform the migrations.'),
39+
);
40+
} else {
41+
return SetupResult::success('None');
42+
}
43+
}
44+
}

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,7 @@
16071607
'OC\\MemoryInfo' => $baseDir . '/lib/private/MemoryInfo.php',
16081608
'OC\\Migration\\BackgroundRepair' => $baseDir . '/lib/private/Migration/BackgroundRepair.php',
16091609
'OC\\Migration\\ConsoleOutput' => $baseDir . '/lib/private/Migration/ConsoleOutput.php',
1610+
'OC\\Migration\\NullOutput' => $baseDir . '/lib/private/Migration/NullOutput.php',
16101611
'OC\\Migration\\SimpleOutput' => $baseDir . '/lib/private/Migration/SimpleOutput.php',
16111612
'OC\\NaturalSort' => $baseDir . '/lib/private/NaturalSort.php',
16121613
'OC\\NaturalSort_DefaultCollator' => $baseDir . '/lib/private/NaturalSort_DefaultCollator.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
16401640
'OC\\MemoryInfo' => __DIR__ . '/../../..' . '/lib/private/MemoryInfo.php',
16411641
'OC\\Migration\\BackgroundRepair' => __DIR__ . '/../../..' . '/lib/private/Migration/BackgroundRepair.php',
16421642
'OC\\Migration\\ConsoleOutput' => __DIR__ . '/../../..' . '/lib/private/Migration/ConsoleOutput.php',
1643+
'OC\\Migration\\NullOutput' => __DIR__ . '/../../..' . '/lib/private/Migration/NullOutput.php',
16431644
'OC\\Migration\\SimpleOutput' => __DIR__ . '/../../..' . '/lib/private/Migration/SimpleOutput.php',
16441645
'OC\\NaturalSort' => __DIR__ . '/../../..' . '/lib/private/NaturalSort.php',
16451646
'OC\\NaturalSort_DefaultCollator' => __DIR__ . '/../../..' . '/lib/private/NaturalSort_DefaultCollator.php',
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
4+
* SPDX-License-Identifier: AGPL-3.0-only
5+
*/
6+
namespace OC\Migration;
7+
8+
use OCP\Migration\IOutput;
9+
use Psr\Log\LoggerInterface;
10+
11+
/**
12+
* Class NullOutput
13+
*
14+
* A simple IOutput that discards all output
15+
*
16+
* @package OC\Migration
17+
*/
18+
class NullOutput implements IOutput {
19+
public function debug(string $message): void {
20+
}
21+
22+
public function info($message): void {
23+
}
24+
25+
public function warning($message): void {
26+
}
27+
28+
public function startProgress($max = 0): void {
29+
}
30+
31+
public function advance($step = 1, $description = ''): void {
32+
}
33+
34+
public function finishProgress(): void {
35+
}
36+
}

lib/private/Repair/RepairMimeTypes.php

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
11
<?php
22

33
/**
4-
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
5-
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
4+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
65
* SPDX-License-Identifier: AGPL-3.0-only
76
*/
87
namespace OC\Repair;
98

9+
use OC\Migration\NullOutput;
1010
use OCP\DB\QueryBuilder\IQueryBuilder;
1111
use OCP\IConfig;
1212
use OCP\IDBConnection;
1313
use OCP\Migration\IOutput;
1414
use OCP\Migration\IRepairStep;
1515

1616
class RepairMimeTypes implements IRepairStep {
17-
/** @var IConfig */
18-
protected $config;
19-
/** @var IDBConnection */
20-
protected $connection;
17+
private bool $dryRun = false;
18+
private int $changeCount = 0;
2119

2220
/** @var int */
2321
protected $folderMimeTypeId;
2422

25-
public function __construct(IConfig $config,
26-
IDBConnection $connection) {
27-
$this->config = $config;
28-
$this->connection = $connection;
23+
public function __construct(
24+
protected IConfig $config,
25+
protected IDBConnection $connection
26+
) {
2927
}
3028

3129
public function getName() {
3230
return 'Repair mime types';
3331
}
3432

3533
private function updateMimetypes($updatedMimetypes) {
34+
if ($this->dryRun) {
35+
$this->changeCount += count($updatedMimetypes);
36+
return;
37+
}
3638
$query = $this->connection->getQueryBuilder();
3739
$query->select('id')
3840
->from('mimetypes')
@@ -236,77 +238,99 @@ private function introduceReStructuredTextFormatType() {
236238
return $this->updateMimetypes($updatedMimetypes);
237239
}
238240

241+
public function migrationsAvailable(): bool {
242+
$this->dryRun = true;
243+
$this->run(new NullOutput());
244+
$this->dryRun = false;
245+
return $this->changeCount > 0;
246+
}
247+
248+
private function getMimeTypeVersion(): string {
249+
$serverVersion = $this->config->getSystemValue('version', '0.0.0');
250+
// 29.0.0.10 is the last version with a mimetype migration before it was moved to a separate version number
251+
if (version_compare($serverVersion, '29.0.0.10', '>')) {
252+
return $this->config->getAppValue('files', 'mimetype_version', '29.0.0.10');
253+
} else {
254+
return $serverVersion;
255+
}
256+
}
257+
239258
/**
240259
* Fix mime types
241260
*/
242261
public function run(IOutput $out) {
243-
$ocVersionFromBeforeUpdate = $this->config->getSystemValueString('version', '0.0.0');
262+
$serverVersion = $this->config->getSystemValue('version', '0.0.0');
263+
$mimeTypeVersion = $this->getMimeTypeVersion();
244264

245265
// NOTE TO DEVELOPERS: when adding new mime types, please make sure to
246266
// add a version comparison to avoid doing it every time
247267

248-
if (version_compare($ocVersionFromBeforeUpdate, '12.0.0.14', '<') && $this->introduceImageTypes()) {
268+
if (version_compare($mimeTypeVersion, '12.0.0.14', '<') && $this->introduceImageTypes()) {
249269
$out->info('Fixed image mime types');
250270
}
251271

252-
if (version_compare($ocVersionFromBeforeUpdate, '12.0.0.13', '<') && $this->introduceWindowsProgramTypes()) {
272+
if (version_compare($mimeTypeVersion, '12.0.0.13', '<') && $this->introduceWindowsProgramTypes()) {
253273
$out->info('Fixed windows program mime types');
254274
}
255275

256-
if (version_compare($ocVersionFromBeforeUpdate, '13.0.0.0', '<') && $this->introduceLocationTypes()) {
276+
if (version_compare($mimeTypeVersion, '13.0.0.0', '<') && $this->introduceLocationTypes()) {
257277
$out->info('Fixed geospatial mime types');
258278
}
259279

260-
if (version_compare($ocVersionFromBeforeUpdate, '13.0.0.3', '<') && $this->introduceInternetShortcutTypes()) {
280+
if (version_compare($mimeTypeVersion, '13.0.0.3', '<') && $this->introduceInternetShortcutTypes()) {
261281
$out->info('Fixed internet-shortcut mime types');
262282
}
263283

264-
if (version_compare($ocVersionFromBeforeUpdate, '13.0.0.6', '<') && $this->introduceStreamingTypes()) {
284+
if (version_compare($mimeTypeVersion, '13.0.0.6', '<') && $this->introduceStreamingTypes()) {
265285
$out->info('Fixed streaming mime types');
266286
}
267287

268-
if (version_compare($ocVersionFromBeforeUpdate, '14.0.0.8', '<') && $this->introduceVisioTypes()) {
288+
if (version_compare($mimeTypeVersion, '14.0.0.8', '<') && $this->introduceVisioTypes()) {
269289
$out->info('Fixed visio mime types');
270290
}
271291

272-
if (version_compare($ocVersionFromBeforeUpdate, '14.0.0.10', '<') && $this->introduceComicbookTypes()) {
292+
if (version_compare($mimeTypeVersion, '14.0.0.10', '<') && $this->introduceComicbookTypes()) {
273293
$out->info('Fixed comicbook mime types');
274294
}
275295

276-
if (version_compare($ocVersionFromBeforeUpdate, '20.0.0.5', '<') && $this->introduceOpenDocumentTemplates()) {
296+
if (version_compare($mimeTypeVersion, '20.0.0.5', '<') && $this->introduceOpenDocumentTemplates()) {
277297
$out->info('Fixed OpenDocument template mime types');
278298
}
279299

280-
if (version_compare($ocVersionFromBeforeUpdate, '21.0.0.7', '<') && $this->introduceOrgModeType()) {
300+
if (version_compare($mimeTypeVersion, '21.0.0.7', '<') && $this->introduceOrgModeType()) {
281301
$out->info('Fixed orgmode mime types');
282302
}
283303

284-
if (version_compare($ocVersionFromBeforeUpdate, '23.0.0.2', '<') && $this->introduceFlatOpenDocumentType()) {
304+
if (version_compare($mimeTypeVersion, '23.0.0.2', '<') && $this->introduceFlatOpenDocumentType()) {
285305
$out->info('Fixed Flat OpenDocument mime types');
286306
}
287307

288-
if (version_compare($ocVersionFromBeforeUpdate, '25.0.0.2', '<') && $this->introduceOnlyofficeFormType()) {
308+
if (version_compare($mimeTypeVersion, '25.0.0.2', '<') && $this->introduceOnlyofficeFormType()) {
289309
$out->info('Fixed ONLYOFFICE Forms OpenXML mime types');
290310
}
291311

292-
if (version_compare($ocVersionFromBeforeUpdate, '26.0.0.1', '<') && $this->introduceAsciidocType()) {
312+
if (version_compare($mimeTypeVersion, '26.0.0.1', '<') && $this->introduceAsciidocType()) {
293313
$out->info('Fixed AsciiDoc mime types');
294314
}
295315

296-
if (version_compare($ocVersionFromBeforeUpdate, '28.0.0.5', '<') && $this->introduceEnhancedMetafileFormatType()) {
316+
if (version_compare($mimeTypeVersion, '28.0.0.5', '<') && $this->introduceEnhancedMetafileFormatType()) {
297317
$out->info('Fixed Enhanced Metafile Format mime types');
298318
}
299319

300-
if (version_compare($ocVersionFromBeforeUpdate, '29.0.0.2', '<') && $this->introduceEmlAndMsgFormatType()) {
320+
if (version_compare($mimeTypeVersion, '29.0.0.2', '<') && $this->introduceEmlAndMsgFormatType()) {
301321
$out->info('Fixed eml and msg mime type');
302322
}
303323

304-
if (version_compare($ocVersionFromBeforeUpdate, '29.0.0.6', '<') && $this->introduceAacAudioType()) {
324+
if (version_compare($mimeTypeVersion, '29.0.0.6', '<') && $this->introduceAacAudioType()) {
305325
$out->info('Fixed aac mime type');
306326
}
307327

308-
if (version_compare($ocVersionFromBeforeUpdate, '29.0.0.10', '<') && $this->introduceReStructuredTextFormatType()) {
328+
if (version_compare($mimeTypeVersion, '29.0.0.10', '<') && $this->introduceReStructuredTextFormatType()) {
309329
$out->info('Fixed ReStructured Text mime type');
310330
}
331+
332+
if (!$this->dryRun) {
333+
$this->config->setAppValue('files', 'mimetype_version', $serverVersion);
334+
}
311335
}
312336
}

0 commit comments

Comments
 (0)