2022-11-07 04:48:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace OCA\Memories\Controller;
|
|
|
|
|
2022-11-07 04:49:33 +00:00
|
|
|
use OCA\Memories\Db\TimelineQuery;
|
2022-11-07 04:48:10 +00:00
|
|
|
use OCP\Files\FileInfo;
|
|
|
|
use OCP\Files\Folder;
|
|
|
|
|
2022-11-07 04:49:33 +00:00
|
|
|
trait FoldersTrait
|
|
|
|
{
|
2022-11-07 04:48:10 +00:00
|
|
|
protected TimelineQuery $timelineQuery;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get subfolders entry for days response.
|
|
|
|
*/
|
2022-11-16 08:16:07 +00:00
|
|
|
public function getSubfoldersEntry(Folder $folder)
|
2022-11-07 04:48:10 +00:00
|
|
|
{
|
|
|
|
// Ugly: get the view of the folder with reflection
|
|
|
|
// This is unfortunately the only way to get the contents of a folder
|
|
|
|
// matching a MIME type without using SEARCH, which is deep
|
|
|
|
$rp = new \ReflectionProperty('\OC\Files\Node\Node', 'view');
|
|
|
|
$rp->setAccessible(true);
|
|
|
|
$view = $rp->getValue($folder);
|
|
|
|
|
|
|
|
// Get the subfolders
|
|
|
|
$folders = $view->getDirectoryContent($folder->getPath(), FileInfo::MIMETYPE_FOLDER, $folder);
|
|
|
|
|
|
|
|
// Sort by name
|
|
|
|
usort($folders, function ($a, $b) {
|
|
|
|
return strnatcmp($a->getName(), $b->getName());
|
|
|
|
});
|
|
|
|
|
|
|
|
// Process to response type
|
|
|
|
return [
|
|
|
|
'dayid' => \OCA\Memories\Util::$TAG_DAYID_FOLDERS,
|
|
|
|
'count' => \count($folders),
|
2022-11-16 02:29:26 +00:00
|
|
|
'detail' => array_map(function ($node) use (&$folder) {
|
2022-11-07 04:48:10 +00:00
|
|
|
return [
|
|
|
|
'fileid' => $node->getId(),
|
|
|
|
'name' => $node->getName(),
|
|
|
|
'isfolder' => 1,
|
|
|
|
'path' => $node->getPath(),
|
|
|
|
'previews' => $this->getFolderPreviews($folder, $node),
|
|
|
|
];
|
|
|
|
}, $folders, []),
|
|
|
|
];
|
|
|
|
}
|
2022-11-07 04:49:33 +00:00
|
|
|
|
|
|
|
private function getFolderPreviews(Folder &$parent, FileInfo &$fileInfo)
|
|
|
|
{
|
|
|
|
$folder = $parent->getById($fileInfo->getId());
|
|
|
|
if (0 === \count($folder)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->timelineQuery->getFolderPreviews($folder[0]);
|
|
|
|
}
|
|
|
|
}
|