Skip to content

Commit

Permalink
Fix scanning app data with metadata
Browse files Browse the repository at this point in the history
Previously we were listening to change in the appdata folder but an
appdata scan didn't setup the file system, so the view was unavailable.

**Test plan:**

1. rm -rf data/appdata_...../preview data/<user>/Media
2. occ files:scan-app-data
3. occ files:scan <user>

No errors and the files and metadata are correctly removed from the
database too.

Signed-off-by: Carl Schwan <[email protected]>
  • Loading branch information
CarlSchwan committed Apr 22, 2022
1 parent 12ed5c9 commit 046414a
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/private/Metadata/FileEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\FileInfo;
use Psr\Log\LoggerInterface;

class FileEventListener implements IEventListener {
private IMetadataManager $manager;

public function __construct(IMetadataManager $manager) {
public function __construct(IMetadataManager $manager, LoggerInterface $logger) {
$this->manager = $manager;
$this->logger = $logger;
}

private function shouldExtractMetadata(Node $node): bool {
Expand All @@ -52,13 +54,31 @@ private function shouldExtractMetadata(Node $node): bool {
}

$path = $node->getPath();
return $this->isCorrectPath($path);
}

private function isCorrectPath(string $path): bool {
// TODO make this more dynamic, we have the same issue in other places
return !str_starts_with($path, 'appdata_') && !str_starts_with($path, 'files_versions/') && !str_starts_with($path, 'files_trashbin/');
}

public function handle(Event $event): void {
if ($event instanceof NodeRemovedFromCache) {
if (!$this->isCorrectPath($event->getPath())) {
// Don't listen to paths for which we don't extract metadata
return;
}
$view = Filesystem::getView();
if (!$view) {
// Should not happen since a scan in the user folder should setup
// the file system.
$e = new \Exception(); // don't trigger, just get backtrace
$this->log->error('Detecting deletion of a file with possible metadata but file system setup is not setup', [
'exception' => $e,
'app' => 'metadata'
]);
return;
}
$info = $view->getFileInfo($event->getPath());
if ($info && $info->getType() === FileInfo::TYPE_FILE) {
$this->manager->clearMetadata($info->getId());
Expand Down

0 comments on commit 046414a

Please sign in to comment.