Files
service_dav/lib/Backends/FilesCollectionNode.php
T
2026-07-14 18:24:31 -04:00

119 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
namespace KTXM\ServiceDav\Backends;
use KTXF\Documents\Collection\CollectionBaseInterface;
use KTXF\Documents\Service\ServiceBaseInterface;
use KTXF\Documents\Service\ServiceCollectionMutableInterface;
use KTXF\Documents\Service\ServiceEntityMutableInterface;
use KTXF\Resource\Identifier\CollectionIdentifier;
use KTXF\Resource\Identifier\EntityIdentifier;
use Sabre\DAV\Collection;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\INode;
/**
* A folder-level WebDAV collection backed by a document collection.
*/
class FilesCollectionNode extends Collection
{
public function __construct(
private readonly ServiceBaseInterface $service,
private readonly CollectionBaseInterface $collection,
) {}
public function getName(): string
{
return $this->collection->getProperties()->getLabel() ?? (string) $this->collection->identifier();
}
/**
* Identifier of the collection backing this node
*/
private function collectionIdentifier(): CollectionIdentifier
{
return new CollectionIdentifier(
$this->service->provider(),
(string) $this->service->identifier(),
(string) $this->collection->identifier(),
);
}
/** @return INode[] */
public function getChildren(): array
{
$nodes = [];
// ----- collections (folders) -----
$decendents = $this->service->collectionList($this->collection->identifier());
foreach ($decendents as $decendent) {
$nodes[] = new FilesCollectionNode(
$this->service,
$decendent,
);
}
// ----- entities (files) -----
/** @var \KTXF\Documents\Entity\EntityBaseInterface[] $entities */
$entities = $this->service->entityListBulk($this->collection->identifier());
foreach ($entities as $entity) {
if (is_object($entity)) {
$nodes[] = new FilesEntityNode(
$this->service,
$entity,
);
}
}
return $nodes;
}
public function createDirectory($name): void
{
if (!$this->service instanceof ServiceCollectionMutableInterface) {
throw new Forbidden('Service does not support collection creation');
}
$properties = $this->service->collectionFresh()->getProperties();
$properties->setLabel($name);
$this->service->collectionCreate($this->collectionIdentifier(), $properties);
}
public function createFile($name, $data = null): string
{
if (!$this->service instanceof ServiceEntityMutableInterface) {
throw new Forbidden('Service does not support entity creation');
}
$properties = $this->service->entityFresh()->getProperties();
$properties->setLabel($name);
$entity = $this->service->entityCreate($this->collectionIdentifier(), $properties);
if ($data !== null) {
$content = is_resource($data) ? stream_get_contents($data) : (string) $data;
$this->service->entityWrite(
new EntityIdentifier(
$this->service->provider(),
(string) $this->service->identifier(),
(string) $entity->collection(),
(string) $entity->identifier(),
),
$content,
);
}
return '"' . md5($entity->identifier() . time()) . '"';
}
public function delete(): void
{
if (!$this->service instanceof ServiceCollectionMutableInterface) {
throw new Forbidden('Service does not support collection deletion');
}
$this->service->collectionDelete($this->collectionIdentifier(), true);
}
}