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

91 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace KTXM\ServiceDav\Backends;
use KTXF\Documents\Entity\EntityBaseInterface;
use KTXF\Documents\Service\ServiceBaseInterface;
use KTXF\Documents\Service\ServiceEntityMutableInterface;
use KTXF\Resource\Identifier\EntityIdentifier;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\File;
/**
* A file node backed by a document entity.
*/
class FilesEntityNode extends File
{
public function __construct(
private readonly ServiceBaseInterface $service,
private readonly EntityBaseInterface $entity,
) {}
public function getName(): string
{
return $this->entity->getProperties()->getLabel() ?? (string) $this->entity->identifier();
}
/**
* Identifier of the entity backing this node
*/
private function entityIdentifier(): EntityIdentifier
{
return new EntityIdentifier(
$this->service->provider(),
(string) $this->service->identifier(),
(string) $this->entity->collection(),
(string) $this->entity->identifier(),
);
}
/** @return resource|string */
public function get()
{
return $this->service->entityRead($this->entityIdentifier());
}
public function put($data): ?string
{
if (!$this->service instanceof ServiceEntityMutableInterface) {
throw new Forbidden('Entity is immutable');
}
$this->service->entityWrite(
$this->entityIdentifier(),
is_resource($data) ? stream_get_contents($data) : (string) $data,
);
return '"' . md5($this->entity->identifier() . time()) . '"';
}
public function delete(): void
{
if (!$this->service instanceof ServiceEntityMutableInterface) {
throw new Forbidden('Entity is immutable');
}
$this->service->entityDelete($this->entityIdentifier());
}
public function getContentType(): ?string
{
return $this->entity->getProperties()->getMime();
}
public function getETag(): ?string
{
return '"' . $this->entity->signature() . '"';
}
public function getSize(): ?int
{
return $this->entity->getProperties()->size();
}
public function getLastModified(): ?int
{
return $this->entity->modified()?->getTimestamp() ?? null;
}
}