MetadataManager.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. public function fetchMetadataFor(string $group, array $fileIds): array {
  70. return $this->fileMetadataMapper->findForGroupForFiles($fileIds, $group);
  71. }
  72. public function getCapabilities(): array {
  73. $capabilities = [];
  74. foreach ($this->providers as $supportedMimetype => $provider) {
  75. foreach ($provider::groupsProvided() as $group) {
  76. if (isset($capabilities[$group])) {
  77. $capabilities[$group][] = $supportedMimetype;
  78. }
  79. $capabilities[$group] = [$supportedMimetype];
  80. }
  81. }
  82. return $capabilities;
  83. }
  84. }