MetadataManager.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * @copyright Copyright 2022 Carl Schwan <carl@carlschwan.eu>
  4. * @license AGPL-3.0-or-later
  5. *
  6. * This code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, version 3,
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License, version 3,
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>
  17. *
  18. */
  19. namespace OC\Metadata;
  20. use OC\Metadata\Provider\ExifProvider;
  21. use OCP\Files\File;
  22. class MetadataManager implements IMetadataManager {
  23. /** @var array<string, IMetadataProvider> */
  24. private array $providers;
  25. private array $providerClasses;
  26. private FileMetadataMapper $fileMetadataMapper;
  27. public function __construct(
  28. FileMetadataMapper $fileMetadataMapper
  29. ) {
  30. $this->providers = [];
  31. $this->providerClasses = [];
  32. $this->fileMetadataMapper = $fileMetadataMapper;
  33. // TODO move to another place, where?
  34. $this->registerProvider(ExifProvider::class);
  35. }
  36. /**
  37. * @param class-string<IMetadataProvider> $className
  38. */
  39. public function registerProvider(string $className):void {
  40. if (in_array($className, $this->providerClasses)) {
  41. return;
  42. }
  43. if (call_user_func([$className, 'isAvailable'])) {
  44. $this->providers[call_user_func([$className, 'getMimetypesSupported'])] = \OC::$server->get($className);
  45. }
  46. }
  47. public function generateMetadata(File $file, bool $checkExisting = false): void {
  48. $existingMetadataGroups = [];
  49. if ($checkExisting) {
  50. $existingMetadata = $this->fileMetadataMapper->findForFile($file->getId());
  51. foreach ($existingMetadata as $metadata) {
  52. $existingMetadataGroups[] = $metadata->getGroupName();
  53. }
  54. }
  55. foreach ($this->providers as $supportedMimetype => $provider) {
  56. if (preg_match($supportedMimetype, $file->getMimeType())) {
  57. if (count(array_diff($provider::groupsProvided(), $existingMetadataGroups)) > 0) {
  58. $metaDataGroup = $provider->execute($file);
  59. foreach ($metaDataGroup as $group => $metadata) {
  60. $this->fileMetadataMapper->insertOrUpdate($metadata);
  61. }
  62. }
  63. }
  64. }
  65. }
  66. public function clearMetadata(int $fileId): void {
  67. $this->fileMetadataMapper->clear($fileId);
  68. }
  69. /**
  70. * @return array<int, FileMetadata>
  71. */
  72. public function fetchMetadataFor(string $group, array $fileIds): array {
  73. return $this->fileMetadataMapper->findForGroupForFiles($fileIds, $group);
  74. }
  75. public function getCapabilities(): array {
  76. $capabilities = [];
  77. foreach ($this->providers as $supportedMimetype => $provider) {
  78. foreach ($provider::groupsProvided() as $group) {
  79. if (isset($capabilities[$group])) {
  80. $capabilities[$group][] = $supportedMimetype;
  81. }
  82. $capabilities[$group] = [$supportedMimetype];
  83. }
  84. }
  85. return $capabilities;
  86. }
  87. }