generated from Nodarx/template
Initial service DAV module
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user