Skip to content

Commit

Permalink
Merge pull request #2245 from nextcloud/backport/2244/stable28
Browse files Browse the repository at this point in the history
[stable28] Request background job to generate metadata on non-local files
  • Loading branch information
artonge authored Jan 9, 2024
2 parents 218a0cf + 1e494c6 commit 5d8b9d7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
10 changes: 9 additions & 1 deletion lib/Listener/ExifMetadataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

Expand All @@ -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;
}
Expand Down
10 changes: 9 additions & 1 deletion lib/Listener/OriginalDateTimeMetadataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand Down
10 changes: 9 additions & 1 deletion lib/Listener/SizeMetadataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

Expand All @@ -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;
}
Expand Down

0 comments on commit 5d8b9d7

Please sign in to comment.