2023-03-24 22:53:26 +00:00
|
|
|
<?php
|
|
|
|
|
2023-10-15 02:20:21 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2023-03-24 22:53:26 +00:00
|
|
|
namespace OCA\Memories\Controller;
|
|
|
|
|
2023-10-13 23:18:37 +00:00
|
|
|
use OCA\Memories\Db\TimelineRoot;
|
2023-03-24 22:53:26 +00:00
|
|
|
use OCA\Memories\Exceptions;
|
|
|
|
use OCA\Memories\Util;
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\Files\FileInfo;
|
|
|
|
use OCP\Files\Folder;
|
|
|
|
|
|
|
|
class FoldersController extends GenericApiController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
|
|
|
public function sub(string $folder): Http\Response
|
|
|
|
{
|
|
|
|
return Util::guardEx(function () use ($folder) {
|
|
|
|
try {
|
|
|
|
$node = Util::getUserFolder()->get($folder);
|
|
|
|
} catch (\OCP\Files\NotFoundException $e) {
|
|
|
|
throw Exceptions::NotFound('Folder not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$node instanceof Folder) {
|
|
|
|
throw Exceptions::NotFound('Path is not a folder');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ugly: get the view of the folder with reflection
|
|
|
|
// This is unfortunately the only way to get the contents of a folder
|
2023-10-15 02:34:30 +00:00
|
|
|
// matching a MIME type without using SEARCH, which is deep.
|
|
|
|
//
|
|
|
|
// To top it off, all this is completely useless at the moment
|
|
|
|
// because the MIME search is done PHP-side
|
|
|
|
$rp = new \ReflectionProperty(\OC\Files\Node\Node::class, 'view');
|
2023-03-24 22:53:26 +00:00
|
|
|
$rp->setAccessible(true);
|
2023-10-15 02:34:30 +00:00
|
|
|
|
|
|
|
/** @var \OC\Files\View */
|
2023-03-24 22:53:26 +00:00
|
|
|
$view = $rp->getValue($node);
|
|
|
|
|
|
|
|
// Get the subfolders
|
|
|
|
$folders = $view->getDirectoryContent($node->getPath(), FileInfo::MIMETYPE_FOLDER, $node);
|
|
|
|
|
|
|
|
// Sort by name
|
2023-08-30 18:10:08 +00:00
|
|
|
usort($folders, static fn ($a, $b) => strnatcmp($a->getName(), $b->getName()));
|
2023-03-24 22:53:26 +00:00
|
|
|
|
2023-10-13 23:18:37 +00:00
|
|
|
// Construct root for the base folder. This way we can reuse the
|
|
|
|
// root by filtering out the subfolders we don't want.
|
|
|
|
$root = new TimelineRoot();
|
|
|
|
$this->fs->populateRoot($root);
|
|
|
|
|
2023-03-24 22:53:26 +00:00
|
|
|
// Process to response type
|
2023-10-13 23:18:37 +00:00
|
|
|
$list = array_map(function ($node) use ($root) {
|
|
|
|
$root->addFolder($node);
|
|
|
|
$root->baseChange($node->getPath());
|
|
|
|
|
|
|
|
return [
|
|
|
|
'fileid' => $node->getId(),
|
|
|
|
'name' => $node->getName(),
|
|
|
|
'path' => $node->getPath(),
|
2023-10-14 06:51:12 +00:00
|
|
|
'previews' => $this->tq->getRootPreviews($root),
|
2023-10-13 23:18:37 +00:00
|
|
|
];
|
|
|
|
}, $folders);
|
2023-03-24 22:53:26 +00:00
|
|
|
|
|
|
|
return new Http\JSONResponse($list, Http::STATUS_OK);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|