From 91f329b7b8846a1a74c47b6221d14e6ec890101e Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Sat, 10 Sep 2022 17:50:08 -0700 Subject: [PATCH] Honor .nomedia in postlistener (fix #5) --- lib/Db/TimelineWrite.php | 22 ++++++++++++++---- lib/Listeners/PostWriteListener.php | 36 ++++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/lib/Db/TimelineWrite.php b/lib/Db/TimelineWrite.php index c8fbf9f1..3aa27b3b 100644 --- a/lib/Db/TimelineWrite.php +++ b/lib/Db/TimelineWrite.php @@ -15,6 +15,21 @@ class TimelineWrite { $this->connection = $connection; } + /** + * Check if a file has a valid mimetype for processing + * @param File $file + * @return int 0 for invalid, 1 for image, 2 for video + */ + public function getFileType(File $file): int { + $mime = $file->getMimeType(); + if (in_array($mime, Application::IMAGE_MIMES)) { + return 1; + } elseif (in_array($mime, Application::VIDEO_MIMES)) { + return 2; + } + return 0; + } + /** * Process a file to insert Exif data into the database * @param File $file @@ -27,10 +42,9 @@ class TimelineWrite { // https://stackoverflow.com/questions/15252213/sql-standard-upsert-call // Check if we want to process this file - $mime = $file->getMimeType(); - $is_image = in_array($mime, Application::IMAGE_MIMES); - $isvideo = in_array($mime, Application::VIDEO_MIMES); - if (!$is_image && !$isvideo) { + $fileType = $this->getFileType($file); + $isvideo = ($fileType === 2); + if (!$fileType) { return 0; } diff --git a/lib/Listeners/PostWriteListener.php b/lib/Listeners/PostWriteListener.php index e6abfbc1..1e7c2280 100644 --- a/lib/Listeners/PostWriteListener.php +++ b/lib/Listeners/PostWriteListener.php @@ -34,12 +34,12 @@ use OCP\IDBConnection; use OCP\IUserManager; class PostWriteListener implements IEventListener { - private TimelineWrite $util; + private TimelineWrite $timelineWrite; public function __construct(IDBConnection $connection, IUserManager $userManager) { $this->userManager = $userManager; - $this->util = new TimelineWrite($connection); + $this->timelineWrite = new TimelineWrite($connection); } public function handle(Event $event): void { @@ -53,6 +53,36 @@ class PostWriteListener implements IEventListener { return; } - $this->util->processFile($node); + // Check the mime type first + if (!$this->timelineWrite->getFileType($node)) { + return; + } + + // Check if a directory at a higher level contains a .nomedia file + // Do this by getting all the parent folders first, then checking them + // in reverse order from root to leaf. The rationale is that the + // .nomedia file is most likely to be in higher level directories. + $parents = []; + try { + $parent = $node->getParent(); + while ($parent) { + $parents[] = $parent; + $parent = $parent->getParent(); + } + } + catch (\OCP\Files\NotFoundException $e) { + // This happens when the parent is in the root directory + // and getParent() is called on it. + } + + // Traverse the array in reverse order looking for .nomedia + $parents = array_reverse($parents); + foreach ($parents as &$parent) { + if ($parent->nodeExists('.nomedia')) { + return; + } + } + + $this->timelineWrite->processFile($node); } } \ No newline at end of file