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.

MetadataManager.php 3.0KB

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. use OCP\IConfig;
  23. use Psr\Log\LoggerInterface;
  24. class MetadataManager implements IMetadataManager {
  25. /** @var array<string, IMetadataProvider> */
  26. private array $providers;
  27. private array $providerClasses;
  28. private FileMetadataMapper $fileMetadataMapper;
  29. private IConfig $config;
  30. private LoggerInterface $logger;
  31. public function __construct(
  32. FileMetadataMapper $fileMetadataMapper,
  33. IConfig $config,
  34. LoggerInterface $logger
  35. ) {
  36. $this->providers = [];
  37. $this->providerClasses = [];
  38. $this->fileMetadataMapper = $fileMetadataMapper;
  39. $this->config = $config;
  40. $this->logger = $logger;
  41. // TODO move to another place, where?
  42. $this->registerProvider(ExifProvider::class);
  43. }
  44. /**
  45. * @param class-string<IMetadataProvider> $className
  46. */
  47. public function registerProvider(string $className):void {
  48. if (in_array($className, $this->providerClasses)) {
  49. return;
  50. }
  51. if (call_user_func([$className, 'isAvailable'])) {
  52. $this->providers[call_user_func([$className, 'getMimetypesSupported'])] = \OC::$server->get($className);
  53. }
  54. }
  55. public function generateMetadata(File $file, bool $checkExisting = false): void {
  56. $existingMetadataGroups = [];
  57. if ($checkExisting) {
  58. $existingMetadata = $this->fileMetadataMapper->findForFile($file->getId());
  59. foreach ($existingMetadata as $metadata) {
  60. $existingMetadataGroups[] = $metadata->getGroupName();
  61. }
  62. }
  63. foreach ($this->providers as $supportedMimetype => $provider) {
  64. if (preg_match($supportedMimetype, $file->getMimeType())) {
  65. if (count(array_diff($provider::groupsProvided(), $existingMetadataGroups)) > 0) {
  66. $metaDataGroup = $provider->execute($file);
  67. foreach ($metaDataGroup as $group => $metadata) {
  68. $this->fileMetadataMapper->insertOrUpdate($metadata);
  69. }
  70. }
  71. }
  72. }
  73. }
  74. public function clearMetadata(int $fileId): void {
  75. $this->fileMetadataMapper->clear($fileId);
  76. }
  77. public function fetchMetadataFor(string $group, array $fileIds): array {
  78. return $this->fileMetadataMapper->findForGroupForFiles($fileIds, $group);
  79. }
  80. public function getCapabilities(): array {
  81. $capabilities = [];
  82. foreach ($this->providers as $supportedMimetype => $provider) {
  83. $capabilities[$supportedMimetype] = $provider::groupsProvided();
  84. }
  85. return $capabilities;
  86. }
  87. }