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-03-24 05:01:49 +00:00
|
|
|
/**
|
|
|
|
* Populate the root with the current user's folders.
|
|
|
|
*/
|
|
|
|
public function populate()
|
|
|
|
{
|
|
|
|
\OC::$server->get(FsManager::class)->populateRoot($this);
|
|
|
|
}
|
|
|
|
|
2022-11-16 08:16:01 +00:00
|
|
|
/**
|
|
|
|
* Add a folder to the root.
|
|
|
|
*
|
|
|
|
* @throws \Exception if node is not valid readable folder
|
|
|
|
*/
|
2023-03-24 22:53:26 +00:00
|
|
|
public function addFolder(FileInfo $info)
|
2022-11-16 07:45:01 +00:00
|
|
|
{
|
2023-03-24 22:53:26 +00:00
|
|
|
$folderPath = $info->getPath();
|
2022-11-16 08:16:01 +00:00
|
|
|
|
2023-03-24 22:53:26 +00:00
|
|
|
if (FileInfo::MIMETYPE_FOLDER !== $info->getMimetype()) {
|
2022-11-16 08:16:01 +00:00
|
|
|
throw new \Exception("Not a folder: {$folderPath}");
|
|
|
|
}
|
|
|
|
|
2023-03-24 22:53:26 +00:00
|
|
|
if (!$info->isReadable()) {
|
2022-11-16 08:16:01 +00:00
|
|
|
throw new \Exception("Folder not readable: {$folderPath}");
|
|
|
|
}
|
|
|
|
|
2022-11-16 07:45:01 +00:00
|
|
|
// Add top level folder
|
2023-03-24 22:53:26 +00:00
|
|
|
$id = $info->getId();
|
|
|
|
$this->folders[$id] = $info;
|
2022-11-16 07:45:01 +00:00
|
|
|
$this->folderPaths[$id] = $folderPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add mountpoints recursively
|
|
|
|
public function addMountPoints()
|
|
|
|
{
|
|
|
|
$mp = [];
|
|
|
|
foreach ($this->folderPaths as $id => $folderPath) {
|
|
|
|
$mounts = \OC\Files\Filesystem::getMountManager()->findIn($folderPath);
|
|
|
|
foreach ($mounts as &$mount) {
|
|
|
|
$id = $mount->getStorageRootId();
|
|
|
|
$path = $mount->getMountPoint();
|
|
|
|
$mp[$id] = $path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->folderPaths += $mp;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFolderPath(int $id)
|
|
|
|
{
|
|
|
|
return $this->folderPaths[$id];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getIds()
|
|
|
|
{
|
|
|
|
return array_keys($this->folderPaths);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getOneId()
|
|
|
|
{
|
|
|
|
return array_key_first($this->folders);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFolder(int $id)
|
|
|
|
{
|
|
|
|
return $this->folders[$id];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isEmpty()
|
|
|
|
{
|
|
|
|
return empty($this->folderPaths);
|
|
|
|
}
|
|
|
|
}
|