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
|
2023-10-19 19:12:26 +00:00
|
|
|
*
|
|
|
|
* @PublicPage
|
2023-03-24 22:53:26 +00:00
|
|
|
*/
|
|
|
|
public function sub(string $folder): Http\Response
|
|
|
|
{
|
|
|
|
return Util::guardEx(function () use ($folder) {
|
2023-10-20 01:51:54 +00:00
|
|
|
$folder = Util::sanitizePath($folder);
|
|
|
|
if (null === $folder) {
|
|
|
|
throw Exceptions::BadRequest('Invalid parameter folder');
|
|
|
|
}
|
|
|
|
|
2023-10-20 04:29:33 +00:00
|
|
|
// Get the root folder (share root or user root)
|
|
|
|
$root = $this->fs->getShareNode() ?? Util::getUserFolder();
|
|
|
|
if (!$root instanceof Folder) {
|
|
|
|
throw Exceptions::BadRequest('Root is not a folder');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the inner folder
|
2023-03-24 22:53:26 +00:00
|
|
|
try {
|
2023-10-20 04:29:33 +00:00
|
|
|
$node = $root->get($folder);
|
2023-10-15 19:46:35 +00:00
|
|
|
} catch (\OCP\Files\NotFoundException) {
|
2023-10-20 01:51:54 +00:00
|
|
|
throw Exceptions::NotFound("Folder not found: {$folder}");
|
2023-03-24 22:53:26 +00:00
|
|
|
}
|
|
|
|
|
2023-10-20 04:29:33 +00:00
|
|
|
// Make sure we have a folder
|
2023-03-24 22:53:26 +00:00
|
|
|
if (!$node instanceof Folder) {
|
2023-10-20 04:29:33 +00:00
|
|
|
throw Exceptions::BadRequest('Path is not a folder');
|
2023-03-24 22:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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(),
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|