2022-11-16 07:45:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\Memories\Db;
|
|
|
|
|
2023-03-24 22:53:26 +00:00
|
|
|
use OCP\Files\FileInfo;
|
2022-11-16 07:45:01 +00:00
|
|
|
|
|
|
|
class TimelineRoot
|
|
|
|
{
|
|
|
|
protected array $folders;
|
|
|
|
protected array $folderPaths;
|
|
|
|
|
|
|
|
/** Initialize */
|
|
|
|
public function __construct()
|
|
|
|
{
|
2023-04-14 22:26:20 +00:00
|
|
|
$this->folders = [];
|
|
|
|
$this->folderPaths = [];
|
2022-11-16 07:45:01 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 08:16:01 +00:00
|
|
|
/**
|
|
|
|
* Add a folder to the root.
|
|
|
|
*
|
|
|
|
* @throws \Exception if node is not valid readable folder
|
|
|
|
*/
|
2023-10-14 08:25:50 +00:00
|
|
|
public function addFolder(FileInfo $info): void
|
2022-11-16 07:45:01 +00:00
|
|
|
{
|
2023-04-14 22:26:20 +00:00
|
|
|
$path = $info->getPath();
|
2022-11-16 08:16:01 +00:00
|
|
|
|
2023-03-24 22:53:26 +00:00
|
|
|
if (FileInfo::MIMETYPE_FOLDER !== $info->getMimetype()) {
|
2023-04-14 22:26:20 +00:00
|
|
|
throw new \Exception("Not a folder: {$path}");
|
2022-11-16 08:16:01 +00:00
|
|
|
}
|
|
|
|
|
2023-03-24 22:53:26 +00:00
|
|
|
if (!$info->isReadable()) {
|
2023-04-14 22:26:20 +00:00
|
|
|
throw new \Exception("Folder not readable: {$path}");
|
2022-11-16 08:16:01 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 07:45:01 +00:00
|
|
|
// Add top level folder
|
2023-04-14 22:26:20 +00:00
|
|
|
$this->setFolder($info->getId(), $info, $path);
|
2022-11-16 07:45:01 +00:00
|
|
|
}
|
|
|
|
|
2023-10-13 23:18:37 +00:00
|
|
|
/**
|
|
|
|
* Add mountpoints recursively.
|
|
|
|
*/
|
2023-10-14 08:25:50 +00:00
|
|
|
public function addMountPoints(): void
|
2022-11-16 07:45:01 +00:00
|
|
|
{
|
2023-10-13 22:10:59 +00:00
|
|
|
$manager = \OC\Files\Filesystem::getMountManager();
|
2022-11-16 07:45:01 +00:00
|
|
|
foreach ($this->folderPaths as $id => $folderPath) {
|
2023-10-13 22:10:59 +00:00
|
|
|
$mounts = $manager->findIn($folderPath);
|
2023-04-14 22:26:20 +00:00
|
|
|
foreach ($mounts as $mount) {
|
2023-10-14 00:43:01 +00:00
|
|
|
$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);
|
2023-04-14 22:26:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 23:18:37 +00:00
|
|
|
/**
|
|
|
|
* Exclude all folders that are in the given paths.
|
|
|
|
*
|
|
|
|
* @param string[] $paths The paths to exclude
|
|
|
|
*/
|
2023-10-14 08:25:50 +00:00
|
|
|
public function excludePaths(array $paths): void
|
2023-04-14 22:26:20 +00:00
|
|
|
{
|
|
|
|
foreach ($paths as $path) {
|
|
|
|
foreach ($this->folderPaths as $id => $folderPath) {
|
2023-05-30 17:36:20 +00:00
|
|
|
// 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.'/')) {
|
2023-04-14 22:26:20 +00:00
|
|
|
unset($this->folderPaths[$id], $this->folders[$id]);
|
|
|
|
}
|
2022-11-16 07:45:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 23:18:37 +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
|
|
|
|
*/
|
2023-10-14 08:25:50 +00:00
|
|
|
public function baseChange(string $path): void
|
2023-10-13 23:18:37 +00:00
|
|
|
{
|
|
|
|
foreach ($this->folderPaths as $id => $folderPath) {
|
|
|
|
if (!str_starts_with($folderPath.'/', $path.'/')) {
|
|
|
|
unset($this->folderPaths[$id], $this->folders[$id]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 07:45:01 +00:00
|
|
|
public function getFolderPath(int $id)
|
|
|
|
{
|
|
|
|
return $this->folderPaths[$id];
|
|
|
|
}
|
|
|
|
|
2023-10-14 08:25:50 +00:00
|
|
|
/**
|
|
|
|
* @return int[]
|
|
|
|
*/
|
|
|
|
public function getIds(): array
|
2022-11-16 07:45:01 +00:00
|
|
|
{
|
|
|
|
return array_keys($this->folderPaths);
|
|
|
|
}
|
|
|
|
|
2023-10-14 08:25:50 +00:00
|
|
|
/**
|
|
|
|
* @return null|int
|
|
|
|
*/
|
2022-11-16 07:45:01 +00:00
|
|
|
public function getOneId()
|
|
|
|
{
|
|
|
|
return array_key_first($this->folders);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFolder(int $id)
|
|
|
|
{
|
|
|
|
return $this->folders[$id];
|
|
|
|
}
|
|
|
|
|
2023-10-14 08:25:50 +00:00
|
|
|
public function isEmpty(): bool
|
2022-11-16 07:45:01 +00:00
|
|
|
{
|
|
|
|
return empty($this->folderPaths);
|
|
|
|
}
|
2023-04-14 22:26:20 +00:00
|
|
|
|
2023-10-14 08:25:50 +00:00
|
|
|
private function setFolder(int $id, ?FileInfo $fileInfo, ?string $path): void
|
2023-04-14 22:26:20 +00:00
|
|
|
{
|
|
|
|
if (null !== $path) {
|
|
|
|
$this->folderPaths[$id] = $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null !== $fileInfo) {
|
|
|
|
$this->folders[$id] = $fileInfo;
|
|
|
|
}
|
|
|
|
}
|
2022-11-16 07:45:01 +00:00
|
|
|
}
|