diff options
author | Carl Schwan <carl@carlschwan.eu> | 2022-05-09 14:19:17 +0200 |
---|---|---|
committer | Louis Chemineau <louis@chmn.me> | 2022-10-11 10:41:17 +0200 |
commit | 54b6d0708bcf389857fdc7e0afe8f0edcf8ea3f3 (patch) | |
tree | 495809029bbc9dc309394b7574b110745ccea5ce /lib | |
parent | 92d065b6ebaa09c316118d9a155d45f69b00f19d (diff) | |
download | nextcloud-server-54b6d0708bcf389857fdc7e0afe8f0edcf8ea3f3.tar.gz nextcloud-server-54b6d0708bcf389857fdc7e0afe8f0edcf8ea3f3.zip |
Allow scanning for metadata with occ scan:file --generate-metadata
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
Signed-off-by: Louis Chemineau <louis@chmn.me>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/Metadata/FileMetadataMapper.php | 49 | ||||
-rw-r--r-- | lib/private/Metadata/MetadataManager.php | 10 | ||||
-rw-r--r-- | lib/private/Metadata/Provider/ExifProvider.php | 21 |
3 files changed, 35 insertions, 45 deletions
diff --git a/lib/private/Metadata/FileMetadataMapper.php b/lib/private/Metadata/FileMetadataMapper.php index 4d5591baf1f..f8f8df4bf3b 100644 --- a/lib/private/Metadata/FileMetadataMapper.php +++ b/lib/private/Metadata/FileMetadataMapper.php @@ -112,59 +112,36 @@ class FileMetadataMapper extends QBMapper { * @return Entity the saved entity with the set id * @throws Exception * @throws \InvalidArgumentException if entity has no id - * @since 14.0.0 */ public function update(Entity $entity): Entity { - // if entity wasn't changed it makes no sense to run a db query - $properties = $entity->getUpdatedFields(); - if (\count($properties) === 0) { - return $entity; + if (!($entity instanceof FileMetadata)) { + throw new \Exception("Entity should be a FileMetadata entity"); } // entity needs an id $id = $entity->getId(); if ($id === null) { - throw new \InvalidArgumentException( - 'Entity which should be updated has no id'); - } - - if (!($entity instanceof FileMetadata)) { - throw new \Exception("Entity should be a FileMetadata entity"); + throw new \InvalidArgumentException('Entity which should be updated has no id'); } // entity needs an group_name $groupName = $entity->getGroupName(); - if ($id === null) { - throw new \InvalidArgumentException( - 'Entity which should be updated has no group_name'); - } - - // get updated fields to save, fields have to be set using a setter to - // be saved - // do not update the id and group_name field - unset($properties['id']); - unset($properties['group_name']); - - $qb = $this->db->getQueryBuilder(); - $qb->update($this->tableName); - - // build the fields - foreach ($properties as $property => $updated) { - $column = $entity->propertyToColumn($property); - $getter = 'get' . ucfirst($property); - $value = $entity->$getter(); - - $type = $this->getParameterTypeForProperty($entity, $property); - $qb->set($column, $qb->createNamedParameter($value, $type)); + if ($groupName === null) { + throw new \InvalidArgumentException('Entity which should be updated has no group_name'); } $idType = $this->getParameterTypeForProperty($entity, 'id'); $groupNameType = $this->getParameterTypeForProperty($entity, 'groupName'); + $metadataValue = $entity->getMetadata(); + $metadataType = $this->getParameterTypeForProperty($entity, 'metadata'); - $qb->where($qb->expr()->eq('id', $qb->createNamedParameter($id, $idType))) - ->andWhere($qb->expr()->eq('group_name', $qb->createNamedParameter($groupName, $groupNameType))); + $qb = $this->db->getQueryBuilder(); - $qb->executeStatement(); + $qb->update($this->tableName) + ->set('metadata', $qb->createNamedParameter($metadataValue, $metadataType)) + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, $idType))) + ->andWhere($qb->expr()->eq('group_name', $qb->createNamedParameter($groupName, $groupNameType))) + ->executeStatement(); return $entity; } diff --git a/lib/private/Metadata/MetadataManager.php b/lib/private/Metadata/MetadataManager.php index d1cb896febf..77407a2f529 100644 --- a/lib/private/Metadata/MetadataManager.php +++ b/lib/private/Metadata/MetadataManager.php @@ -21,27 +21,19 @@ namespace OC\Metadata; use OC\Metadata\Provider\ExifProvider; use OCP\Files\File; -use OCP\IConfig; -use Psr\Log\LoggerInterface; class MetadataManager implements IMetadataManager { /** @var array<string, IMetadataProvider> */ private array $providers; private array $providerClasses; private FileMetadataMapper $fileMetadataMapper; - private IConfig $config; - private LoggerInterface $logger; public function __construct( - FileMetadataMapper $fileMetadataMapper, - IConfig $config, - LoggerInterface $logger + FileMetadataMapper $fileMetadataMapper ) { $this->providers = []; $this->providerClasses = []; $this->fileMetadataMapper = $fileMetadataMapper; - $this->config = $config; - $this->logger = $logger; // TODO move to another place, where? $this->registerProvider(ExifProvider::class); diff --git a/lib/private/Metadata/Provider/ExifProvider.php b/lib/private/Metadata/Provider/ExifProvider.php index aa0b5464682..02024bd3877 100644 --- a/lib/private/Metadata/Provider/ExifProvider.php +++ b/lib/private/Metadata/Provider/ExifProvider.php @@ -1,5 +1,25 @@ <?php +declare(strict_types=1); +/** + * @copyright Copyright 2022 Carl Schwan <carl@carlschwan.eu> + * @copyright Copyright 2022 Louis Chmn <louis@chmn.me> + * @license AGPL-3.0-or-later + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + namespace OC\Metadata\Provider; use OC\Metadata\FileMetadata; @@ -24,6 +44,7 @@ class ExifProvider implements IMetadataProvider { return extension_loaded('exif'); } + /** @return array{'gps': FileMetadata, 'size': FileMetadata} */ public function execute(File $file): array { $exifData = []; $fileDescriptor = $file->fopen('rb'); |