You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GenerateMimetypeFileBuilder.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019 Xheni Myrtaj <xheni@protonmail.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Xheni Myrtaj <myrtajxheni@gmail.com>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Core\Command\Maintenance\Mimetype;
  27. class GenerateMimetypeFileBuilder {
  28. /**
  29. * Generate mime type list file
  30. *
  31. * @param array<string,string> $aliases
  32. * @return string
  33. */
  34. public function generateFile(array $aliases): string {
  35. // Remove comments
  36. $aliases = array_filter($aliases, static function ($key) {
  37. return !($key === '' || $key[0] === '_');
  38. }, ARRAY_FILTER_USE_KEY);
  39. // Fetch all files
  40. $dir = new \DirectoryIterator(\OC::$SERVERROOT.'/core/img/filetypes');
  41. $files = [];
  42. foreach ($dir as $fileInfo) {
  43. if ($fileInfo->isFile()) {
  44. $file = preg_replace('/.[^.]*$/', '', $fileInfo->getFilename());
  45. $files[] = $file;
  46. }
  47. }
  48. //Remove duplicates
  49. $files = array_values(array_unique($files));
  50. sort($files);
  51. // Fetch all themes!
  52. $themes = [];
  53. $dirs = new \DirectoryIterator(\OC::$SERVERROOT.'/themes/');
  54. foreach ($dirs as $dir) {
  55. //Valid theme dir
  56. if ($dir->isFile() || $dir->isDot()) {
  57. continue;
  58. }
  59. $theme = $dir->getFilename();
  60. $themeDir = $dir->getPath() . '/' . $theme . '/core/img/filetypes/';
  61. // Check if this theme has its own filetype icons
  62. if (!file_exists($themeDir)) {
  63. continue;
  64. }
  65. $themes[$theme] = [];
  66. // Fetch all the theme icons!
  67. $themeIt = new \DirectoryIterator($themeDir);
  68. foreach ($themeIt as $fileInfo) {
  69. if ($fileInfo->isFile()) {
  70. $file = preg_replace('/.[^.]*$/', '', $fileInfo->getFilename());
  71. $themes[$theme][] = $file;
  72. }
  73. }
  74. //Remove Duplicates
  75. $themes[$theme] = array_values(array_unique($themes[$theme]));
  76. sort($themes[$theme]);
  77. }
  78. //Generate the JS
  79. return '/**
  80. * This file is automatically generated
  81. * DO NOT EDIT MANUALLY!
  82. *
  83. * You can update the list of MimeType Aliases in config/mimetypealiases.json
  84. * The list of files is fetched from core/img/filetypes
  85. * To regenerate this file run ./occ maintenance:mimetype:update-js
  86. */
  87. OC.MimeTypeList={
  88. aliases: ' . json_encode($aliases, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . ',
  89. files: ' . json_encode($files, JSON_PRETTY_PRINT) . ',
  90. themes: ' . json_encode($themes, JSON_PRETTY_PRINT) . '
  91. };
  92. ';
  93. }
  94. }