mirror of
https://github.com/projectsend/projectsend.git
synced 2026-10-03 21:03:17 +00:00
70dc725858
An integration could put a file into a folder by id but could not see, make or arrange the folders themselves, so mirroring a directory tree into ProjectSend was impossible over the API. The hosted AI connector already creates, lists and shares folders. GET /folders polls like every list (updated_since, cursor) and filters on parent_id, top_level and search. Each folder carries its ancestors and a display path, trimmed for a client-scoped token to the folders it may see (BreadcrumbBuilder::visible's rule), worked out for a whole page in two queries by FolderTrails. POST /folders returns an existing folder of the same name in the same place with a 200 rather than making a second one, so a retried request is safe. PATCH renames and moves. DELETE refuses a non-empty folder with 409 unless content_action=cascade_delete is sent, and then asks UndeletableFiles exactly as the web does. Sharing goes through FolderSharing. Every write uses the web's policy, scope and FolderService, and asks Folder::uploadableBy for every parent it writes, creation included. Public state stays web-only: the resource reports `public`, nothing here changes it. A file's `folder` now carries `parent_id` as well.
79 lines
2.3 KiB
PHP
79 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Files\Folders;
|
|
|
|
use App\Models\User;
|
|
use App\Modules\Files\Access\StaffLibraryScope;
|
|
use App\Modules\Files\Models\Folder;
|
|
|
|
/**
|
|
* The ancestors of a whole page of folders, in two queries however long the
|
|
* page is, trimmed to what the viewer may see.
|
|
*
|
|
* BreadcrumbBuilder answers this for one folder on a screen. A list
|
|
* endpoint needs it for every row, and a query per row is the cost a
|
|
* listing must not have.
|
|
*
|
|
* Trimmed the way BreadcrumbBuilder::visible() trims the client portal's
|
|
* trail: the list starts at the first ancestor the viewer can reach, since
|
|
* a client-scoped staff member holding a client's folder deep in somebody
|
|
* else's tree has no business reading the names of the folders above it.
|
|
* An unscoped staff member reaches every folder, so for them nothing is
|
|
* ever trimmed.
|
|
*/
|
|
class FolderTrails
|
|
{
|
|
public function __construct(
|
|
private readonly StaffLibraryScope $scope,
|
|
) {}
|
|
|
|
/**
|
|
* @param iterable<Folder> $folders
|
|
* @return array<int, list<array{id: int, name: string}>> folder id => its visible ancestors, root first, itself excluded
|
|
*/
|
|
public function ancestors(iterable $folders, User $viewer): array
|
|
{
|
|
$chains = [];
|
|
$allIds = [];
|
|
|
|
foreach ($folders as $folder) {
|
|
$ids = $folder->ancestorIds();
|
|
$chains[$folder->id] = $ids;
|
|
array_push($allIds, ...$ids);
|
|
}
|
|
|
|
$allIds = array_values(array_unique($allIds));
|
|
|
|
if ($allIds === []) {
|
|
return array_map(fn (): array => [], $chains);
|
|
}
|
|
|
|
$names = Folder::query()->whereIn('id', $allIds)->pluck('name', 'id')->all();
|
|
|
|
$visible = $viewer->isClientScoped()
|
|
? array_flip($this->scope->folders($viewer)->whereIn('folders.id', $allIds)->pluck('folders.id')->all())
|
|
: array_flip($allIds);
|
|
|
|
$out = [];
|
|
|
|
foreach ($chains as $folderId => $ids) {
|
|
$trail = [];
|
|
$reached = false;
|
|
|
|
foreach ($ids as $id) {
|
|
$reached = $reached || isset($visible[$id]);
|
|
|
|
if ($reached && isset($names[$id])) {
|
|
$trail[] = ['id' => $id, 'name' => (string) $names[$id]];
|
|
}
|
|
}
|
|
|
|
$out[$folderId] = $trail;
|
|
}
|
|
|
|
return $out;
|
|
}
|
|
}
|