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;
}
/**
* 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;
}

View File

@ -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);
}
}