Honor .nomedia in postlistener (fix #5)

pull/37/head
Varun Patil 2022-09-10 17:50:08 -07:00
parent 075ff4e006
commit 91f329b7b8
2 changed files with 51 additions and 7 deletions

View File

@ -15,6 +15,21 @@ class TimelineWrite {
$this->connection = $connection; $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 * Process a file to insert Exif data into the database
* @param File $file * @param File $file
@ -27,10 +42,9 @@ class TimelineWrite {
// https://stackoverflow.com/questions/15252213/sql-standard-upsert-call // https://stackoverflow.com/questions/15252213/sql-standard-upsert-call
// Check if we want to process this file // Check if we want to process this file
$mime = $file->getMimeType(); $fileType = $this->getFileType($file);
$is_image = in_array($mime, Application::IMAGE_MIMES); $isvideo = ($fileType === 2);
$isvideo = in_array($mime, Application::VIDEO_MIMES); if (!$fileType) {
if (!$is_image && !$isvideo) {
return 0; return 0;
} }

View File

@ -34,12 +34,12 @@ use OCP\IDBConnection;
use OCP\IUserManager; use OCP\IUserManager;
class PostWriteListener implements IEventListener { class PostWriteListener implements IEventListener {
private TimelineWrite $util; private TimelineWrite $timelineWrite;
public function __construct(IDBConnection $connection, public function __construct(IDBConnection $connection,
IUserManager $userManager) { IUserManager $userManager) {
$this->userManager = $userManager; $this->userManager = $userManager;
$this->util = new TimelineWrite($connection); $this->timelineWrite = new TimelineWrite($connection);
} }
public function handle(Event $event): void { public function handle(Event $event): void {
@ -53,6 +53,36 @@ class PostWriteListener implements IEventListener {
return; 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);
} }
} }