@@ -17,473 +17,473 @@ |
||
| 17 | 17 | use OCP\Migration\IRepairStep; |
| 18 | 18 | |
| 19 | 19 | class RepairMimeTypes implements IRepairStep { |
| 20 | - private bool $dryRun = false; |
|
| 21 | - private int $changeCount = 0; |
|
| 22 | - |
|
| 23 | - /** @var int */ |
|
| 24 | - protected int $folderMimeTypeId; |
|
| 25 | - |
|
| 26 | - public function __construct( |
|
| 27 | - protected IConfig $config, |
|
| 28 | - protected IAppConfig $appConfig, |
|
| 29 | - protected IDBConnection $connection, |
|
| 30 | - ) { |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - public function getName(): string { |
|
| 34 | - return 'Repair mime types'; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @throws Exception |
|
| 39 | - */ |
|
| 40 | - private function updateMimetypes($updatedMimetypes): IResult|int|null { |
|
| 41 | - if ($this->dryRun) { |
|
| 42 | - $this->changeCount += count($updatedMimetypes); |
|
| 43 | - return null; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - $query = $this->connection->getQueryBuilder(); |
|
| 47 | - $query->select('id') |
|
| 48 | - ->from('mimetypes') |
|
| 49 | - ->where($query->expr()->eq('mimetype', $query->createParameter('mimetype'), IQueryBuilder::PARAM_INT)); |
|
| 50 | - $insert = $this->connection->getQueryBuilder(); |
|
| 51 | - $insert->insert('mimetypes') |
|
| 52 | - ->setValue('mimetype', $insert->createParameter('mimetype')); |
|
| 53 | - |
|
| 54 | - if (empty($this->folderMimeTypeId)) { |
|
| 55 | - $query->setParameter('mimetype', 'httpd/unix-directory'); |
|
| 56 | - $result = $query->execute(); |
|
| 57 | - $this->folderMimeTypeId = (int)$result->fetchOne(); |
|
| 58 | - $result->closeCursor(); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - $update = $this->connection->getQueryBuilder(); |
|
| 62 | - $update->update('filecache') |
|
| 63 | - ->runAcrossAllShards() |
|
| 64 | - ->set('mimetype', $update->createParameter('mimetype')) |
|
| 65 | - ->where($update->expr()->neq('mimetype', $update->createParameter('mimetype'), IQueryBuilder::PARAM_INT)) |
|
| 66 | - ->andWhere($update->expr()->neq('mimetype', $update->createParameter('folder'), IQueryBuilder::PARAM_INT)) |
|
| 67 | - ->andWhere($update->expr()->iLike('name', $update->createParameter('name'))) |
|
| 68 | - ->setParameter('folder', $this->folderMimeTypeId); |
|
| 69 | - |
|
| 70 | - $count = 0; |
|
| 71 | - foreach ($updatedMimetypes as $extension => $mimetype) { |
|
| 72 | - // get target mimetype id |
|
| 73 | - $query->setParameter('mimetype', $mimetype); |
|
| 74 | - $result = $query->execute(); |
|
| 75 | - $mimetypeId = (int)$result->fetchOne(); |
|
| 76 | - $result->closeCursor(); |
|
| 77 | - |
|
| 78 | - if (!$mimetypeId) { |
|
| 79 | - // insert mimetype |
|
| 80 | - $insert->setParameter('mimetype', $mimetype); |
|
| 81 | - $insert->execute(); |
|
| 82 | - $mimetypeId = $insert->getLastInsertId(); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - // change mimetype for files with x extension |
|
| 86 | - $update->setParameter('mimetype', $mimetypeId) |
|
| 87 | - ->setParameter('name', '%' . $this->connection->escapeLikeParameter('.' . $extension)); |
|
| 88 | - $count += $update->execute(); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - return $count; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @throws Exception |
|
| 96 | - * @since 12.0.0.14 |
|
| 97 | - */ |
|
| 98 | - private function introduceImageTypes(): IResult|int|null { |
|
| 99 | - $updatedMimetypes = [ |
|
| 100 | - 'jp2' => 'image/jp2', |
|
| 101 | - 'webp' => 'image/webp', |
|
| 102 | - ]; |
|
| 103 | - |
|
| 104 | - return $this->updateMimetypes($updatedMimetypes); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @throws Exception |
|
| 109 | - * @since 12.0.0.13 |
|
| 110 | - */ |
|
| 111 | - private function introduceWindowsProgramTypes(): IResult|int|null { |
|
| 112 | - $updatedMimetypes = [ |
|
| 113 | - 'htaccess' => 'text/plain', |
|
| 114 | - 'bat' => 'application/x-msdos-program', |
|
| 115 | - 'cmd' => 'application/cmd', |
|
| 116 | - ]; |
|
| 117 | - |
|
| 118 | - return $this->updateMimetypes($updatedMimetypes); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * @throws Exception |
|
| 123 | - * @since 13.0.0.0 |
|
| 124 | - */ |
|
| 125 | - private function introduceLocationTypes(): IResult|int|null { |
|
| 126 | - $updatedMimetypes = [ |
|
| 127 | - 'gpx' => 'application/gpx+xml', |
|
| 128 | - 'kml' => 'application/vnd.google-earth.kml+xml', |
|
| 129 | - 'kmz' => 'application/vnd.google-earth.kmz', |
|
| 130 | - 'tcx' => 'application/vnd.garmin.tcx+xml', |
|
| 131 | - ]; |
|
| 132 | - |
|
| 133 | - return $this->updateMimetypes($updatedMimetypes); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * @throws Exception |
|
| 138 | - * @since 13.0.0.3 |
|
| 139 | - */ |
|
| 140 | - private function introduceInternetShortcutTypes(): IResult|int|null { |
|
| 141 | - $updatedMimetypes = [ |
|
| 142 | - 'url' => 'application/internet-shortcut', |
|
| 143 | - 'webloc' => 'application/internet-shortcut' |
|
| 144 | - ]; |
|
| 145 | - |
|
| 146 | - return $this->updateMimetypes($updatedMimetypes); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * @throws Exception |
|
| 151 | - * @since 13.0.0.6 |
|
| 152 | - */ |
|
| 153 | - private function introduceStreamingTypes(): IResult|int|null { |
|
| 154 | - $updatedMimetypes = [ |
|
| 155 | - 'm3u' => 'audio/mpegurl', |
|
| 156 | - 'm3u8' => 'audio/mpegurl', |
|
| 157 | - 'pls' => 'audio/x-scpls' |
|
| 158 | - ]; |
|
| 159 | - |
|
| 160 | - return $this->updateMimetypes($updatedMimetypes); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * @throws Exception |
|
| 165 | - * @since 14.0.0.8 |
|
| 166 | - */ |
|
| 167 | - private function introduceVisioTypes(): IResult|int|null { |
|
| 168 | - $updatedMimetypes = [ |
|
| 169 | - 'vsdm' => 'application/vnd.visio', |
|
| 170 | - 'vsdx' => 'application/vnd.visio', |
|
| 171 | - 'vssm' => 'application/vnd.visio', |
|
| 172 | - 'vssx' => 'application/vnd.visio', |
|
| 173 | - 'vstm' => 'application/vnd.visio', |
|
| 174 | - 'vstx' => 'application/vnd.visio', |
|
| 175 | - ]; |
|
| 176 | - |
|
| 177 | - return $this->updateMimetypes($updatedMimetypes); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * @throws Exception |
|
| 182 | - * @since 14.0.0.10 |
|
| 183 | - */ |
|
| 184 | - private function introduceComicbookTypes(): IResult|int|null { |
|
| 185 | - $updatedMimetypes = [ |
|
| 186 | - 'cb7' => 'application/comicbook+7z', |
|
| 187 | - 'cba' => 'application/comicbook+ace', |
|
| 188 | - 'cbr' => 'application/comicbook+rar', |
|
| 189 | - 'cbt' => 'application/comicbook+tar', |
|
| 190 | - 'cbtc' => 'application/comicbook+truecrypt', |
|
| 191 | - 'cbz' => 'application/comicbook+zip', |
|
| 192 | - ]; |
|
| 193 | - |
|
| 194 | - return $this->updateMimetypes($updatedMimetypes); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * @throws Exception |
|
| 199 | - * @since 20.0.0.5 |
|
| 200 | - */ |
|
| 201 | - private function introduceOpenDocumentTemplates(): IResult|int|null { |
|
| 202 | - $updatedMimetypes = [ |
|
| 203 | - 'ott' => 'application/vnd.oasis.opendocument.text-template', |
|
| 204 | - 'ots' => 'application/vnd.oasis.opendocument.spreadsheet-template', |
|
| 205 | - 'otp' => 'application/vnd.oasis.opendocument.presentation-template', |
|
| 206 | - 'otg' => 'application/vnd.oasis.opendocument.graphics-template', |
|
| 207 | - ]; |
|
| 208 | - |
|
| 209 | - return $this->updateMimetypes($updatedMimetypes); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * @throws Exception |
|
| 214 | - * @since 21.0.0.7 |
|
| 215 | - */ |
|
| 216 | - private function introduceOrgModeType(): IResult|int|null { |
|
| 217 | - $updatedMimetypes = [ |
|
| 218 | - 'org' => 'text/org' |
|
| 219 | - ]; |
|
| 220 | - |
|
| 221 | - return $this->updateMimetypes($updatedMimetypes); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * @throws Exception |
|
| 226 | - * @since 23.0.0.2 |
|
| 227 | - */ |
|
| 228 | - private function introduceFlatOpenDocumentType(): IResult|int|null { |
|
| 229 | - $updatedMimetypes = [ |
|
| 230 | - 'fodt' => 'application/vnd.oasis.opendocument.text-flat-xml', |
|
| 231 | - 'fods' => 'application/vnd.oasis.opendocument.spreadsheet-flat-xml', |
|
| 232 | - 'fodg' => 'application/vnd.oasis.opendocument.graphics-flat-xml', |
|
| 233 | - 'fodp' => 'application/vnd.oasis.opendocument.presentation-flat-xml', |
|
| 234 | - ]; |
|
| 235 | - |
|
| 236 | - return $this->updateMimetypes($updatedMimetypes); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * @throws Exception |
|
| 241 | - * @since 25.0.0.2 |
|
| 242 | - */ |
|
| 243 | - private function introduceOnlyofficeFormType(): IResult|int|null { |
|
| 244 | - $updatedMimetypes = [ |
|
| 245 | - 'oform' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document.oform', |
|
| 246 | - 'docxf' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document.docxf', |
|
| 247 | - ]; |
|
| 248 | - |
|
| 249 | - return $this->updateMimetypes($updatedMimetypes); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * @throws Exception |
|
| 254 | - * @since 26.0.0.1 |
|
| 255 | - */ |
|
| 256 | - private function introduceAsciidocType(): IResult|int|null { |
|
| 257 | - $updatedMimetypes = [ |
|
| 258 | - 'adoc' => 'text/asciidoc', |
|
| 259 | - 'asciidoc' => 'text/asciidoc', |
|
| 260 | - ]; |
|
| 261 | - |
|
| 262 | - return $this->updateMimetypes($updatedMimetypes); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * @throws Exception |
|
| 267 | - * @since 28.0.0.5 |
|
| 268 | - */ |
|
| 269 | - private function introduceEnhancedMetafileFormatType(): IResult|int|null { |
|
| 270 | - $updatedMimetypes = [ |
|
| 271 | - 'emf' => 'image/emf', |
|
| 272 | - ]; |
|
| 273 | - |
|
| 274 | - return $this->updateMimetypes($updatedMimetypes); |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * @throws Exception |
|
| 279 | - * @since 29.0.0.2 |
|
| 280 | - */ |
|
| 281 | - private function introduceEmlAndMsgFormatType(): IResult|int|null { |
|
| 282 | - $updatedMimetypes = [ |
|
| 283 | - 'eml' => 'message/rfc822', |
|
| 284 | - 'msg' => 'application/vnd.ms-outlook', |
|
| 285 | - ]; |
|
| 286 | - |
|
| 287 | - return $this->updateMimetypes($updatedMimetypes); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * @throws Exception |
|
| 292 | - * @since 29.0.0.6 |
|
| 293 | - */ |
|
| 294 | - private function introduceAacAudioType(): IResult|int|null { |
|
| 295 | - $updatedMimetypes = [ |
|
| 296 | - 'aac' => 'audio/aac', |
|
| 297 | - ]; |
|
| 298 | - |
|
| 299 | - return $this->updateMimetypes($updatedMimetypes); |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * @throws Exception |
|
| 304 | - * @since 29.0.10 |
|
| 305 | - */ |
|
| 306 | - private function introduceReStructuredTextFormatType(): IResult|int|null { |
|
| 307 | - $updatedMimetypes = [ |
|
| 308 | - 'rst' => 'text/x-rst', |
|
| 309 | - ]; |
|
| 310 | - |
|
| 311 | - return $this->updateMimetypes($updatedMimetypes); |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * @throws Exception |
|
| 316 | - * @since 30.0.0 |
|
| 317 | - */ |
|
| 318 | - private function introduceExcalidrawType(): IResult|int|null { |
|
| 319 | - $updatedMimetypes = [ |
|
| 320 | - 'excalidraw' => 'application/vnd.excalidraw+json', |
|
| 321 | - ]; |
|
| 322 | - |
|
| 323 | - return $this->updateMimetypes($updatedMimetypes); |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - |
|
| 327 | - /** |
|
| 328 | - * @throws Exception |
|
| 329 | - * @since 31.0.0 |
|
| 330 | - */ |
|
| 331 | - private function introduceZstType(): IResult|int|null { |
|
| 332 | - $updatedMimetypes = [ |
|
| 333 | - 'zst' => 'application/zstd', |
|
| 334 | - 'nfo' => 'text/x-nfo', |
|
| 335 | - ]; |
|
| 336 | - |
|
| 337 | - return $this->updateMimetypes($updatedMimetypes); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * @throws Exception |
|
| 342 | - * @since 32.0.0 |
|
| 343 | - */ |
|
| 344 | - private function introduceMusicxmlType(): IResult|int|null { |
|
| 345 | - $updatedMimetypes = [ |
|
| 346 | - 'mxl' => 'application/vnd.recordare.musicxml', |
|
| 347 | - 'musicxml' => 'application/vnd.recordare.musicxml+xml', |
|
| 348 | - ]; |
|
| 349 | - |
|
| 350 | - return $this->updateMimetypes($updatedMimetypes); |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * @throws Exception |
|
| 355 | - * @since 32.0.0 |
|
| 356 | - */ |
|
| 357 | - private function introduceTextType(): IResult|int|null { |
|
| 358 | - $updatedMimetypes = [ |
|
| 359 | - 'text' => 'text/plain', |
|
| 360 | - ]; |
|
| 361 | - |
|
| 362 | - return $this->updateMimetypes($updatedMimetypes); |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * Check if there are any migrations available |
|
| 369 | - * |
|
| 370 | - * @throws Exception |
|
| 371 | - */ |
|
| 372 | - public function migrationsAvailable(): bool { |
|
| 373 | - $this->dryRun = true; |
|
| 374 | - $this->run(new NullOutput()); |
|
| 375 | - $this->dryRun = false; |
|
| 376 | - return $this->changeCount > 0; |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - /** |
|
| 380 | - * Get the current mimetype version |
|
| 381 | - */ |
|
| 382 | - private function getMimeTypeVersion(): string { |
|
| 383 | - $serverVersion = $this->config->getSystemValueString('version', '0.0.0'); |
|
| 384 | - // 29.0.0.10 is the last version with a mimetype migration before it was moved to a separate version number |
|
| 385 | - if (version_compare($serverVersion, '29.0.0.10', '>')) { |
|
| 386 | - return $this->appConfig->getValueString('files', 'mimetype_version', '29.0.0.10'); |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - return $serverVersion; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - /** |
|
| 393 | - * Fix mime types |
|
| 394 | - * |
|
| 395 | - * @throws Exception |
|
| 396 | - */ |
|
| 397 | - public function run(IOutput $output): void { |
|
| 398 | - $serverVersion = $this->config->getSystemValueString('version', '0.0.0'); |
|
| 399 | - $mimeTypeVersion = $this->getMimeTypeVersion(); |
|
| 400 | - |
|
| 401 | - // NOTE TO DEVELOPERS: when adding new mime types, please make sure to |
|
| 402 | - // add a version comparison to avoid doing it every time |
|
| 403 | - // PLEASE ALSO KEEP THE LIST SORTED BY VERSION NUMBER |
|
| 404 | - |
|
| 405 | - if (version_compare($mimeTypeVersion, '12.0.0.14', '<') && $this->introduceImageTypes()) { |
|
| 406 | - $output->info('Fixed image mime types'); |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - if (version_compare($mimeTypeVersion, '12.0.0.13', '<') && $this->introduceWindowsProgramTypes()) { |
|
| 410 | - $output->info('Fixed windows program mime types'); |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - if (version_compare($mimeTypeVersion, '13.0.0.0', '<') && $this->introduceLocationTypes()) { |
|
| 414 | - $output->info('Fixed geospatial mime types'); |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - if (version_compare($mimeTypeVersion, '13.0.0.3', '<') && $this->introduceInternetShortcutTypes()) { |
|
| 418 | - $output->info('Fixed internet-shortcut mime types'); |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - if (version_compare($mimeTypeVersion, '13.0.0.6', '<') && $this->introduceStreamingTypes()) { |
|
| 422 | - $output->info('Fixed streaming mime types'); |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - if (version_compare($mimeTypeVersion, '14.0.0.8', '<') && $this->introduceVisioTypes()) { |
|
| 426 | - $output->info('Fixed visio mime types'); |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - if (version_compare($mimeTypeVersion, '14.0.0.10', '<') && $this->introduceComicbookTypes()) { |
|
| 430 | - $output->info('Fixed comicbook mime types'); |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - if (version_compare($mimeTypeVersion, '20.0.0.5', '<') && $this->introduceOpenDocumentTemplates()) { |
|
| 434 | - $output->info('Fixed OpenDocument template mime types'); |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - if (version_compare($mimeTypeVersion, '21.0.0.7', '<') && $this->introduceOrgModeType()) { |
|
| 438 | - $output->info('Fixed orgmode mime types'); |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - if (version_compare($mimeTypeVersion, '23.0.0.2', '<') && $this->introduceFlatOpenDocumentType()) { |
|
| 442 | - $output->info('Fixed Flat OpenDocument mime types'); |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - if (version_compare($mimeTypeVersion, '25.0.0.2', '<') && $this->introduceOnlyofficeFormType()) { |
|
| 446 | - $output->info('Fixed ONLYOFFICE Forms OpenXML mime types'); |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - if (version_compare($mimeTypeVersion, '26.0.0.1', '<') && $this->introduceAsciidocType()) { |
|
| 450 | - $output->info('Fixed AsciiDoc mime types'); |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - if (version_compare($mimeTypeVersion, '28.0.0.5', '<') && $this->introduceEnhancedMetafileFormatType()) { |
|
| 454 | - $output->info('Fixed Enhanced Metafile Format mime types'); |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - if (version_compare($mimeTypeVersion, '29.0.0.2', '<') && $this->introduceEmlAndMsgFormatType()) { |
|
| 458 | - $output->info('Fixed eml and msg mime type'); |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - if (version_compare($mimeTypeVersion, '29.0.0.6', '<') && $this->introduceAacAudioType()) { |
|
| 462 | - $output->info('Fixed aac mime type'); |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - if (version_compare($mimeTypeVersion, '29.0.0.10', '<') && $this->introduceReStructuredTextFormatType()) { |
|
| 466 | - $output->info('Fixed ReStructured Text mime type'); |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - if (version_compare($mimeTypeVersion, '30.0.0.0', '<') && $this->introduceExcalidrawType()) { |
|
| 470 | - $output->info('Fixed Excalidraw mime type'); |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - if (version_compare($mimeTypeVersion, '31.0.0.0', '<') && $this->introduceZstType()) { |
|
| 474 | - $output->info('Fixed zst mime type'); |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - if (version_compare($mimeTypeVersion, '32.0.0.0', '<') && $this->introduceMusicxmlType()) { |
|
| 478 | - $output->info('Fixed musicxml mime type'); |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - if (version_compare($mimeTypeVersion, '32.0.0.0', '<') && $this->introduceTextType()) { |
|
| 482 | - $output->info('Fixed text mime type'); |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - if (!$this->dryRun) { |
|
| 486 | - $this->appConfig->setValueString('files', 'mimetype_version', $serverVersion); |
|
| 487 | - } |
|
| 488 | - } |
|
| 20 | + private bool $dryRun = false; |
|
| 21 | + private int $changeCount = 0; |
|
| 22 | + |
|
| 23 | + /** @var int */ |
|
| 24 | + protected int $folderMimeTypeId; |
|
| 25 | + |
|
| 26 | + public function __construct( |
|
| 27 | + protected IConfig $config, |
|
| 28 | + protected IAppConfig $appConfig, |
|
| 29 | + protected IDBConnection $connection, |
|
| 30 | + ) { |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + public function getName(): string { |
|
| 34 | + return 'Repair mime types'; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @throws Exception |
|
| 39 | + */ |
|
| 40 | + private function updateMimetypes($updatedMimetypes): IResult|int|null { |
|
| 41 | + if ($this->dryRun) { |
|
| 42 | + $this->changeCount += count($updatedMimetypes); |
|
| 43 | + return null; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + $query = $this->connection->getQueryBuilder(); |
|
| 47 | + $query->select('id') |
|
| 48 | + ->from('mimetypes') |
|
| 49 | + ->where($query->expr()->eq('mimetype', $query->createParameter('mimetype'), IQueryBuilder::PARAM_INT)); |
|
| 50 | + $insert = $this->connection->getQueryBuilder(); |
|
| 51 | + $insert->insert('mimetypes') |
|
| 52 | + ->setValue('mimetype', $insert->createParameter('mimetype')); |
|
| 53 | + |
|
| 54 | + if (empty($this->folderMimeTypeId)) { |
|
| 55 | + $query->setParameter('mimetype', 'httpd/unix-directory'); |
|
| 56 | + $result = $query->execute(); |
|
| 57 | + $this->folderMimeTypeId = (int)$result->fetchOne(); |
|
| 58 | + $result->closeCursor(); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + $update = $this->connection->getQueryBuilder(); |
|
| 62 | + $update->update('filecache') |
|
| 63 | + ->runAcrossAllShards() |
|
| 64 | + ->set('mimetype', $update->createParameter('mimetype')) |
|
| 65 | + ->where($update->expr()->neq('mimetype', $update->createParameter('mimetype'), IQueryBuilder::PARAM_INT)) |
|
| 66 | + ->andWhere($update->expr()->neq('mimetype', $update->createParameter('folder'), IQueryBuilder::PARAM_INT)) |
|
| 67 | + ->andWhere($update->expr()->iLike('name', $update->createParameter('name'))) |
|
| 68 | + ->setParameter('folder', $this->folderMimeTypeId); |
|
| 69 | + |
|
| 70 | + $count = 0; |
|
| 71 | + foreach ($updatedMimetypes as $extension => $mimetype) { |
|
| 72 | + // get target mimetype id |
|
| 73 | + $query->setParameter('mimetype', $mimetype); |
|
| 74 | + $result = $query->execute(); |
|
| 75 | + $mimetypeId = (int)$result->fetchOne(); |
|
| 76 | + $result->closeCursor(); |
|
| 77 | + |
|
| 78 | + if (!$mimetypeId) { |
|
| 79 | + // insert mimetype |
|
| 80 | + $insert->setParameter('mimetype', $mimetype); |
|
| 81 | + $insert->execute(); |
|
| 82 | + $mimetypeId = $insert->getLastInsertId(); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + // change mimetype for files with x extension |
|
| 86 | + $update->setParameter('mimetype', $mimetypeId) |
|
| 87 | + ->setParameter('name', '%' . $this->connection->escapeLikeParameter('.' . $extension)); |
|
| 88 | + $count += $update->execute(); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + return $count; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @throws Exception |
|
| 96 | + * @since 12.0.0.14 |
|
| 97 | + */ |
|
| 98 | + private function introduceImageTypes(): IResult|int|null { |
|
| 99 | + $updatedMimetypes = [ |
|
| 100 | + 'jp2' => 'image/jp2', |
|
| 101 | + 'webp' => 'image/webp', |
|
| 102 | + ]; |
|
| 103 | + |
|
| 104 | + return $this->updateMimetypes($updatedMimetypes); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @throws Exception |
|
| 109 | + * @since 12.0.0.13 |
|
| 110 | + */ |
|
| 111 | + private function introduceWindowsProgramTypes(): IResult|int|null { |
|
| 112 | + $updatedMimetypes = [ |
|
| 113 | + 'htaccess' => 'text/plain', |
|
| 114 | + 'bat' => 'application/x-msdos-program', |
|
| 115 | + 'cmd' => 'application/cmd', |
|
| 116 | + ]; |
|
| 117 | + |
|
| 118 | + return $this->updateMimetypes($updatedMimetypes); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * @throws Exception |
|
| 123 | + * @since 13.0.0.0 |
|
| 124 | + */ |
|
| 125 | + private function introduceLocationTypes(): IResult|int|null { |
|
| 126 | + $updatedMimetypes = [ |
|
| 127 | + 'gpx' => 'application/gpx+xml', |
|
| 128 | + 'kml' => 'application/vnd.google-earth.kml+xml', |
|
| 129 | + 'kmz' => 'application/vnd.google-earth.kmz', |
|
| 130 | + 'tcx' => 'application/vnd.garmin.tcx+xml', |
|
| 131 | + ]; |
|
| 132 | + |
|
| 133 | + return $this->updateMimetypes($updatedMimetypes); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * @throws Exception |
|
| 138 | + * @since 13.0.0.3 |
|
| 139 | + */ |
|
| 140 | + private function introduceInternetShortcutTypes(): IResult|int|null { |
|
| 141 | + $updatedMimetypes = [ |
|
| 142 | + 'url' => 'application/internet-shortcut', |
|
| 143 | + 'webloc' => 'application/internet-shortcut' |
|
| 144 | + ]; |
|
| 145 | + |
|
| 146 | + return $this->updateMimetypes($updatedMimetypes); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * @throws Exception |
|
| 151 | + * @since 13.0.0.6 |
|
| 152 | + */ |
|
| 153 | + private function introduceStreamingTypes(): IResult|int|null { |
|
| 154 | + $updatedMimetypes = [ |
|
| 155 | + 'm3u' => 'audio/mpegurl', |
|
| 156 | + 'm3u8' => 'audio/mpegurl', |
|
| 157 | + 'pls' => 'audio/x-scpls' |
|
| 158 | + ]; |
|
| 159 | + |
|
| 160 | + return $this->updateMimetypes($updatedMimetypes); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * @throws Exception |
|
| 165 | + * @since 14.0.0.8 |
|
| 166 | + */ |
|
| 167 | + private function introduceVisioTypes(): IResult|int|null { |
|
| 168 | + $updatedMimetypes = [ |
|
| 169 | + 'vsdm' => 'application/vnd.visio', |
|
| 170 | + 'vsdx' => 'application/vnd.visio', |
|
| 171 | + 'vssm' => 'application/vnd.visio', |
|
| 172 | + 'vssx' => 'application/vnd.visio', |
|
| 173 | + 'vstm' => 'application/vnd.visio', |
|
| 174 | + 'vstx' => 'application/vnd.visio', |
|
| 175 | + ]; |
|
| 176 | + |
|
| 177 | + return $this->updateMimetypes($updatedMimetypes); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * @throws Exception |
|
| 182 | + * @since 14.0.0.10 |
|
| 183 | + */ |
|
| 184 | + private function introduceComicbookTypes(): IResult|int|null { |
|
| 185 | + $updatedMimetypes = [ |
|
| 186 | + 'cb7' => 'application/comicbook+7z', |
|
| 187 | + 'cba' => 'application/comicbook+ace', |
|
| 188 | + 'cbr' => 'application/comicbook+rar', |
|
| 189 | + 'cbt' => 'application/comicbook+tar', |
|
| 190 | + 'cbtc' => 'application/comicbook+truecrypt', |
|
| 191 | + 'cbz' => 'application/comicbook+zip', |
|
| 192 | + ]; |
|
| 193 | + |
|
| 194 | + return $this->updateMimetypes($updatedMimetypes); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * @throws Exception |
|
| 199 | + * @since 20.0.0.5 |
|
| 200 | + */ |
|
| 201 | + private function introduceOpenDocumentTemplates(): IResult|int|null { |
|
| 202 | + $updatedMimetypes = [ |
|
| 203 | + 'ott' => 'application/vnd.oasis.opendocument.text-template', |
|
| 204 | + 'ots' => 'application/vnd.oasis.opendocument.spreadsheet-template', |
|
| 205 | + 'otp' => 'application/vnd.oasis.opendocument.presentation-template', |
|
| 206 | + 'otg' => 'application/vnd.oasis.opendocument.graphics-template', |
|
| 207 | + ]; |
|
| 208 | + |
|
| 209 | + return $this->updateMimetypes($updatedMimetypes); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * @throws Exception |
|
| 214 | + * @since 21.0.0.7 |
|
| 215 | + */ |
|
| 216 | + private function introduceOrgModeType(): IResult|int|null { |
|
| 217 | + $updatedMimetypes = [ |
|
| 218 | + 'org' => 'text/org' |
|
| 219 | + ]; |
|
| 220 | + |
|
| 221 | + return $this->updateMimetypes($updatedMimetypes); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * @throws Exception |
|
| 226 | + * @since 23.0.0.2 |
|
| 227 | + */ |
|
| 228 | + private function introduceFlatOpenDocumentType(): IResult|int|null { |
|
| 229 | + $updatedMimetypes = [ |
|
| 230 | + 'fodt' => 'application/vnd.oasis.opendocument.text-flat-xml', |
|
| 231 | + 'fods' => 'application/vnd.oasis.opendocument.spreadsheet-flat-xml', |
|
| 232 | + 'fodg' => 'application/vnd.oasis.opendocument.graphics-flat-xml', |
|
| 233 | + 'fodp' => 'application/vnd.oasis.opendocument.presentation-flat-xml', |
|
| 234 | + ]; |
|
| 235 | + |
|
| 236 | + return $this->updateMimetypes($updatedMimetypes); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * @throws Exception |
|
| 241 | + * @since 25.0.0.2 |
|
| 242 | + */ |
|
| 243 | + private function introduceOnlyofficeFormType(): IResult|int|null { |
|
| 244 | + $updatedMimetypes = [ |
|
| 245 | + 'oform' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document.oform', |
|
| 246 | + 'docxf' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document.docxf', |
|
| 247 | + ]; |
|
| 248 | + |
|
| 249 | + return $this->updateMimetypes($updatedMimetypes); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * @throws Exception |
|
| 254 | + * @since 26.0.0.1 |
|
| 255 | + */ |
|
| 256 | + private function introduceAsciidocType(): IResult|int|null { |
|
| 257 | + $updatedMimetypes = [ |
|
| 258 | + 'adoc' => 'text/asciidoc', |
|
| 259 | + 'asciidoc' => 'text/asciidoc', |
|
| 260 | + ]; |
|
| 261 | + |
|
| 262 | + return $this->updateMimetypes($updatedMimetypes); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * @throws Exception |
|
| 267 | + * @since 28.0.0.5 |
|
| 268 | + */ |
|
| 269 | + private function introduceEnhancedMetafileFormatType(): IResult|int|null { |
|
| 270 | + $updatedMimetypes = [ |
|
| 271 | + 'emf' => 'image/emf', |
|
| 272 | + ]; |
|
| 273 | + |
|
| 274 | + return $this->updateMimetypes($updatedMimetypes); |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * @throws Exception |
|
| 279 | + * @since 29.0.0.2 |
|
| 280 | + */ |
|
| 281 | + private function introduceEmlAndMsgFormatType(): IResult|int|null { |
|
| 282 | + $updatedMimetypes = [ |
|
| 283 | + 'eml' => 'message/rfc822', |
|
| 284 | + 'msg' => 'application/vnd.ms-outlook', |
|
| 285 | + ]; |
|
| 286 | + |
|
| 287 | + return $this->updateMimetypes($updatedMimetypes); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * @throws Exception |
|
| 292 | + * @since 29.0.0.6 |
|
| 293 | + */ |
|
| 294 | + private function introduceAacAudioType(): IResult|int|null { |
|
| 295 | + $updatedMimetypes = [ |
|
| 296 | + 'aac' => 'audio/aac', |
|
| 297 | + ]; |
|
| 298 | + |
|
| 299 | + return $this->updateMimetypes($updatedMimetypes); |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * @throws Exception |
|
| 304 | + * @since 29.0.10 |
|
| 305 | + */ |
|
| 306 | + private function introduceReStructuredTextFormatType(): IResult|int|null { |
|
| 307 | + $updatedMimetypes = [ |
|
| 308 | + 'rst' => 'text/x-rst', |
|
| 309 | + ]; |
|
| 310 | + |
|
| 311 | + return $this->updateMimetypes($updatedMimetypes); |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * @throws Exception |
|
| 316 | + * @since 30.0.0 |
|
| 317 | + */ |
|
| 318 | + private function introduceExcalidrawType(): IResult|int|null { |
|
| 319 | + $updatedMimetypes = [ |
|
| 320 | + 'excalidraw' => 'application/vnd.excalidraw+json', |
|
| 321 | + ]; |
|
| 322 | + |
|
| 323 | + return $this->updateMimetypes($updatedMimetypes); |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + |
|
| 327 | + /** |
|
| 328 | + * @throws Exception |
|
| 329 | + * @since 31.0.0 |
|
| 330 | + */ |
|
| 331 | + private function introduceZstType(): IResult|int|null { |
|
| 332 | + $updatedMimetypes = [ |
|
| 333 | + 'zst' => 'application/zstd', |
|
| 334 | + 'nfo' => 'text/x-nfo', |
|
| 335 | + ]; |
|
| 336 | + |
|
| 337 | + return $this->updateMimetypes($updatedMimetypes); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * @throws Exception |
|
| 342 | + * @since 32.0.0 |
|
| 343 | + */ |
|
| 344 | + private function introduceMusicxmlType(): IResult|int|null { |
|
| 345 | + $updatedMimetypes = [ |
|
| 346 | + 'mxl' => 'application/vnd.recordare.musicxml', |
|
| 347 | + 'musicxml' => 'application/vnd.recordare.musicxml+xml', |
|
| 348 | + ]; |
|
| 349 | + |
|
| 350 | + return $this->updateMimetypes($updatedMimetypes); |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * @throws Exception |
|
| 355 | + * @since 32.0.0 |
|
| 356 | + */ |
|
| 357 | + private function introduceTextType(): IResult|int|null { |
|
| 358 | + $updatedMimetypes = [ |
|
| 359 | + 'text' => 'text/plain', |
|
| 360 | + ]; |
|
| 361 | + |
|
| 362 | + return $this->updateMimetypes($updatedMimetypes); |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * Check if there are any migrations available |
|
| 369 | + * |
|
| 370 | + * @throws Exception |
|
| 371 | + */ |
|
| 372 | + public function migrationsAvailable(): bool { |
|
| 373 | + $this->dryRun = true; |
|
| 374 | + $this->run(new NullOutput()); |
|
| 375 | + $this->dryRun = false; |
|
| 376 | + return $this->changeCount > 0; |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + /** |
|
| 380 | + * Get the current mimetype version |
|
| 381 | + */ |
|
| 382 | + private function getMimeTypeVersion(): string { |
|
| 383 | + $serverVersion = $this->config->getSystemValueString('version', '0.0.0'); |
|
| 384 | + // 29.0.0.10 is the last version with a mimetype migration before it was moved to a separate version number |
|
| 385 | + if (version_compare($serverVersion, '29.0.0.10', '>')) { |
|
| 386 | + return $this->appConfig->getValueString('files', 'mimetype_version', '29.0.0.10'); |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + return $serverVersion; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + /** |
|
| 393 | + * Fix mime types |
|
| 394 | + * |
|
| 395 | + * @throws Exception |
|
| 396 | + */ |
|
| 397 | + public function run(IOutput $output): void { |
|
| 398 | + $serverVersion = $this->config->getSystemValueString('version', '0.0.0'); |
|
| 399 | + $mimeTypeVersion = $this->getMimeTypeVersion(); |
|
| 400 | + |
|
| 401 | + // NOTE TO DEVELOPERS: when adding new mime types, please make sure to |
|
| 402 | + // add a version comparison to avoid doing it every time |
|
| 403 | + // PLEASE ALSO KEEP THE LIST SORTED BY VERSION NUMBER |
|
| 404 | + |
|
| 405 | + if (version_compare($mimeTypeVersion, '12.0.0.14', '<') && $this->introduceImageTypes()) { |
|
| 406 | + $output->info('Fixed image mime types'); |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + if (version_compare($mimeTypeVersion, '12.0.0.13', '<') && $this->introduceWindowsProgramTypes()) { |
|
| 410 | + $output->info('Fixed windows program mime types'); |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + if (version_compare($mimeTypeVersion, '13.0.0.0', '<') && $this->introduceLocationTypes()) { |
|
| 414 | + $output->info('Fixed geospatial mime types'); |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + if (version_compare($mimeTypeVersion, '13.0.0.3', '<') && $this->introduceInternetShortcutTypes()) { |
|
| 418 | + $output->info('Fixed internet-shortcut mime types'); |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + if (version_compare($mimeTypeVersion, '13.0.0.6', '<') && $this->introduceStreamingTypes()) { |
|
| 422 | + $output->info('Fixed streaming mime types'); |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + if (version_compare($mimeTypeVersion, '14.0.0.8', '<') && $this->introduceVisioTypes()) { |
|
| 426 | + $output->info('Fixed visio mime types'); |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + if (version_compare($mimeTypeVersion, '14.0.0.10', '<') && $this->introduceComicbookTypes()) { |
|
| 430 | + $output->info('Fixed comicbook mime types'); |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + if (version_compare($mimeTypeVersion, '20.0.0.5', '<') && $this->introduceOpenDocumentTemplates()) { |
|
| 434 | + $output->info('Fixed OpenDocument template mime types'); |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + if (version_compare($mimeTypeVersion, '21.0.0.7', '<') && $this->introduceOrgModeType()) { |
|
| 438 | + $output->info('Fixed orgmode mime types'); |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + if (version_compare($mimeTypeVersion, '23.0.0.2', '<') && $this->introduceFlatOpenDocumentType()) { |
|
| 442 | + $output->info('Fixed Flat OpenDocument mime types'); |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + if (version_compare($mimeTypeVersion, '25.0.0.2', '<') && $this->introduceOnlyofficeFormType()) { |
|
| 446 | + $output->info('Fixed ONLYOFFICE Forms OpenXML mime types'); |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + if (version_compare($mimeTypeVersion, '26.0.0.1', '<') && $this->introduceAsciidocType()) { |
|
| 450 | + $output->info('Fixed AsciiDoc mime types'); |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + if (version_compare($mimeTypeVersion, '28.0.0.5', '<') && $this->introduceEnhancedMetafileFormatType()) { |
|
| 454 | + $output->info('Fixed Enhanced Metafile Format mime types'); |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + if (version_compare($mimeTypeVersion, '29.0.0.2', '<') && $this->introduceEmlAndMsgFormatType()) { |
|
| 458 | + $output->info('Fixed eml and msg mime type'); |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + if (version_compare($mimeTypeVersion, '29.0.0.6', '<') && $this->introduceAacAudioType()) { |
|
| 462 | + $output->info('Fixed aac mime type'); |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + if (version_compare($mimeTypeVersion, '29.0.0.10', '<') && $this->introduceReStructuredTextFormatType()) { |
|
| 466 | + $output->info('Fixed ReStructured Text mime type'); |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + if (version_compare($mimeTypeVersion, '30.0.0.0', '<') && $this->introduceExcalidrawType()) { |
|
| 470 | + $output->info('Fixed Excalidraw mime type'); |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + if (version_compare($mimeTypeVersion, '31.0.0.0', '<') && $this->introduceZstType()) { |
|
| 474 | + $output->info('Fixed zst mime type'); |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + if (version_compare($mimeTypeVersion, '32.0.0.0', '<') && $this->introduceMusicxmlType()) { |
|
| 478 | + $output->info('Fixed musicxml mime type'); |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + if (version_compare($mimeTypeVersion, '32.0.0.0', '<') && $this->introduceTextType()) { |
|
| 482 | + $output->info('Fixed text mime type'); |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + if (!$this->dryRun) { |
|
| 486 | + $this->appConfig->setValueString('files', 'mimetype_version', $serverVersion); |
|
| 487 | + } |
|
| 488 | + } |
|
| 489 | 489 | } |