From 1e494c67b4cb8d46f8a07302dcbd79a232031fbd Mon Sep 17 00:00:00 2001 From: Louis Chemineau Date: Tue, 9 Jan 2024 18:02:31 +0100 Subject: [PATCH] Request background job to generate metadata on non-local files Signed-off-by: Louis Chemineau --- lib/AppInfo/Application.php | 3 +++ lib/Listener/ExifMetadataProvider.php | 10 +++++++++- lib/Listener/OriginalDateTimeMetadataProvider.php | 10 +++++++++- lib/Listener/SizeMetadataProvider.php | 10 +++++++++- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 4bcae701c..36d4c8bc2 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -91,8 +91,11 @@ public function register(IRegistrationContext $context): void { // Metadata $context->registerEventListener(MetadataLiveEvent::class, ExifMetadataProvider::class); + $context->registerEventListener(MetadataBackgroundEvent::class, ExifMetadataProvider::class); $context->registerEventListener(MetadataLiveEvent::class, SizeMetadataProvider::class); + $context->registerEventListener(MetadataBackgroundEvent::class, SizeMetadataProvider::class); $context->registerEventListener(MetadataLiveEvent::class, OriginalDateTimeMetadataProvider::class); + $context->registerEventListener(MetadataBackgroundEvent::class, OriginalDateTimeMetadataProvider::class); $context->registerEventListener(MetadataLiveEvent::class, PlaceMetadataProvider::class); $context->registerEventListener(MetadataBackgroundEvent::class, PlaceMetadataProvider::class); diff --git a/lib/Listener/ExifMetadataProvider.php b/lib/Listener/ExifMetadataProvider.php index 2acfbdfc6..a10b8e1a6 100644 --- a/lib/Listener/ExifMetadataProvider.php +++ b/lib/Listener/ExifMetadataProvider.php @@ -26,6 +26,7 @@ use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; use OCP\Files\File; +use OCP\FilesMetadata\Event\MetadataBackgroundEvent; use OCP\FilesMetadata\Event\MetadataLiveEvent; use Psr\Log\LoggerInterface; @@ -42,7 +43,7 @@ public function __construct( } public function handle(Event $event): void { - if (!($event instanceof MetadataLiveEvent)) { + if (!($event instanceof MetadataLiveEvent) && !($event instanceof MetadataBackgroundEvent)) { return; } @@ -52,6 +53,13 @@ public function handle(Event $event): void { return; } + // We need the file content to extract the EXIF data. + // This can be slow for remote storage, so we do it in a background job. + if (!$node->getStorage()->isLocal() && $event instanceof MetadataLiveEvent) { + $event->requestBackgroundJob(); + return; + } + if (!in_array($node->getMimeType(), Application::IMAGE_MIMES)) { return; } diff --git a/lib/Listener/OriginalDateTimeMetadataProvider.php b/lib/Listener/OriginalDateTimeMetadataProvider.php index fdc8f9485..5582364e5 100644 --- a/lib/Listener/OriginalDateTimeMetadataProvider.php +++ b/lib/Listener/OriginalDateTimeMetadataProvider.php @@ -27,6 +27,7 @@ use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; use OCP\Files\File; +use OCP\FilesMetadata\Event\MetadataBackgroundEvent; use OCP\FilesMetadata\Event\MetadataLiveEvent; use Psr\Log\LoggerInterface; @@ -68,7 +69,7 @@ private function dateToTimestamp(string $format, string $date, File $node): int| } public function handle(Event $event): void { - if (!($event instanceof MetadataLiveEvent)) { + if (!($event instanceof MetadataLiveEvent) && !($event instanceof MetadataBackgroundEvent)) { return; } @@ -78,6 +79,13 @@ public function handle(Event $event): void { return; } + // We need the file content to extract the EXIF data. + // This can be slow for remote storage, so we do it in a background job. + if (!$node->getStorage()->isLocal() && $event instanceof MetadataLiveEvent) { + $event->requestBackgroundJob(); + return; + } + if (!in_array($node->getMimeType(), Application::IMAGE_MIMES) && !in_array($node->getMimeType(), Application::VIDEO_MIMES)) { return; } diff --git a/lib/Listener/SizeMetadataProvider.php b/lib/Listener/SizeMetadataProvider.php index 0a78b598a..ce01cbf87 100644 --- a/lib/Listener/SizeMetadataProvider.php +++ b/lib/Listener/SizeMetadataProvider.php @@ -26,6 +26,7 @@ use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; use OCP\Files\File; +use OCP\FilesMetadata\Event\MetadataBackgroundEvent; use OCP\FilesMetadata\Event\MetadataLiveEvent; use Psr\Log\LoggerInterface; @@ -39,7 +40,7 @@ public function __construct( } public function handle(Event $event): void { - if (!($event instanceof MetadataLiveEvent)) { + if (!($event instanceof MetadataLiveEvent) && !($event instanceof MetadataBackgroundEvent)) { return; } @@ -49,6 +50,13 @@ public function handle(Event $event): void { return; } + // We need the file content to extract the size. + // This can be slow for remote storage, so we do it in a background job. + if (!$node->getStorage()->isLocal() && $event instanceof MetadataLiveEvent) { + $event->requestBackgroundJob(); + return; + } + if (!in_array($node->getMimeType(), Application::IMAGE_MIMES)) { return; }