memories/lib/Db/TimelineRoot.php

127 lines
3.3 KiB
PHP
Raw Permalink Normal View History

2022-11-16 07:45:01 +00:00
<?php
declare(strict_types=1);
namespace OCA\Memories\Db;
use OCP\Files\FileInfo;
2022-11-16 07:45:01 +00:00
class TimelineRoot
{
/** @var array<int, \OCP\Files\FileInfo> */
protected array $folders = [];
/** @var array<int, string> */
protected array $folderPaths = [];
2022-11-16 07:45:01 +00:00
/**
* Add a folder to the root.
*
* @throws \Exception if node is not valid readable folder
*/
public function addFolder(FileInfo $info): void
2022-11-16 07:45:01 +00:00
{
$path = $info->getPath();
if (FileInfo::MIMETYPE_FOLDER !== $info->getMimetype()) {
throw new \Exception("Not a folder: {$path}");
}
if (!$info->isReadable()) {
throw new \Exception("Folder not readable: {$path}");
}
2022-11-16 07:45:01 +00:00
// Add top level folder
$this->setFolder($info->getId() ?? 0, $info, $path);
2022-11-16 07:45:01 +00:00
}
/**
* Add mountpoints recursively.
*/
public function addMountPoints(): void
2022-11-16 07:45:01 +00:00
{
$manager = \OC\Files\Filesystem::getMountManager();
2022-11-16 07:45:01 +00:00
foreach ($this->folderPaths as $id => $folderPath) {
$mounts = $manager->findIn($folderPath);
foreach ($mounts as $mount) {
$id = $mount->getStorageRootId();
$path = $mount->getMountPoint();
// Ignore hidden mounts or any mounts in hidden folders
// (any edge cases/exceptions here?)
if (str_contains($path, '/.')) {
continue;
}
$this->setFolder($id, null, $path);
}
}
}
/**
* Exclude all folders that are in the given paths.
*
* @param string[] $paths The paths to exclude
*/
public function excludePaths(array $paths): void
{
foreach ($paths as $path) {
foreach ($this->folderPaths as $id => $folderPath) {
// dirname strips the trailing slash, so we can directly add a
// trailing slash to folderPath and path to prevent false matches.
// https://github.com/pulsejet/memories/issues/668
if (str_starts_with($folderPath.'/', $path.'/')) {
unset($this->folderPaths[$id], $this->folders[$id]);
}
2022-11-16 07:45:01 +00:00
}
}
}
/**
* Change the base folder to a different one.
* This excludes all folders not prefixed with the new base path.
*
* @param string $path The new base path
*/
public function baseChange(string $path): void
{
foreach ($this->folderPaths as $id => $folderPath) {
if (!str_starts_with($folderPath.'/', $path.'/')) {
unset($this->folderPaths[$id], $this->folders[$id]);
}
}
}
/** @return int[] */
public function getIds(): array
2022-11-16 07:45:01 +00:00
{
return array_keys($this->folderPaths);
}
public function getOneId(): ?int
2022-11-16 07:45:01 +00:00
{
return array_key_first($this->folders);
}
public function getFolder(int $id): ?FileInfo
2022-11-16 07:45:01 +00:00
{
return $this->folders[$id];
}
public function isEmpty(): bool
2022-11-16 07:45:01 +00:00
{
return empty($this->folderPaths);
}
private function setFolder(int $id, ?FileInfo $fileInfo, ?string $path): void
{
if (null !== $path) {
$this->folderPaths[$id] = $path;
}
if (null !== $fileInfo) {
$this->folders[$id] = $fileInfo;
}
}
2022-11-16 07:45:01 +00:00
}