mirror of
https://github.com/nextcloud/server.git
synced 2026-04-15 22:11:17 -04:00
Use rich objects instead of name, link and icon
Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
parent
403b673b93
commit
3022ef687a
6 changed files with 49 additions and 161 deletions
|
|
@ -25,26 +25,32 @@ namespace OCA\Files\Collaboration\Resources;
|
|||
|
||||
use OCP\Collaboration\Resources\IProvider;
|
||||
use OCP\Collaboration\Resources\IResource;
|
||||
use OCP\Collaboration\Resources\ResourceException;
|
||||
use OCP\Files\IRootFolder;
|
||||
use OCP\Files\Node;
|
||||
use OCP\IPreview;
|
||||
use OCP\IURLGenerator;
|
||||
use OCP\IUser;
|
||||
|
||||
class ResourceProvider implements IProvider {
|
||||
|
||||
const RESOURCE_TYPE = 'files';
|
||||
public const RESOURCE_TYPE = 'file';
|
||||
|
||||
/** @var IRootFolder */
|
||||
protected $rootFolder;
|
||||
|
||||
/** @var IPreview */
|
||||
private $preview;
|
||||
/** @var IURLGenerator */
|
||||
private $urlGenerator;
|
||||
|
||||
/** @var array */
|
||||
protected $nodes = [];
|
||||
|
||||
public function __construct(IRootFolder $rootFolder, IURLGenerator $urlGenerator) {
|
||||
public function __construct(IRootFolder $rootFolder,
|
||||
IPreview $preview,
|
||||
IURLGenerator $urlGenerator) {
|
||||
$this->rootFolder = $rootFolder;
|
||||
$this->preview = $preview;
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
}
|
||||
|
||||
|
|
@ -61,21 +67,34 @@ class ResourceProvider implements IProvider {
|
|||
}
|
||||
|
||||
/**
|
||||
* Get the display name of a resource
|
||||
*
|
||||
* @param IResource $resource
|
||||
* @return string
|
||||
* @return array
|
||||
* @since 16.0.0
|
||||
*/
|
||||
public function getName(IResource $resource): string {
|
||||
public function getResourceRichObject(IResource $resource): array {
|
||||
if (isset($this->nodes[(int) $resource->getId()])) {
|
||||
return $this->nodes[(int) $resource->getId()]->getPath();
|
||||
$node = $this->nodes[(int) $resource->getId()]->getPath();
|
||||
} else {
|
||||
$node = $this->getNode($resource);
|
||||
}
|
||||
$node = $this->getNode($resource);
|
||||
if ($node) {
|
||||
return $node->getName();
|
||||
|
||||
if ($node instanceof Node) {
|
||||
$link = $this->urlGenerator->linkToRouteAbsolute(
|
||||
'files.viewcontroller.showFile',
|
||||
['fileid' => $resource->getId()]
|
||||
);
|
||||
return [
|
||||
'type' => 'file',
|
||||
'id' => $resource->getId(),
|
||||
'name' => $node->getName(),
|
||||
'path' => $node->getInternalPath(),
|
||||
'link' => $link,
|
||||
'mimetype' => $node->getMimetype(),
|
||||
'preview-available' => $this->preview->isAvailable($node),
|
||||
];
|
||||
}
|
||||
return '';
|
||||
|
||||
throw new ResourceException('File not found');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -102,25 +121,6 @@ class ResourceProvider implements IProvider {
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the icon class of a resource
|
||||
*
|
||||
* @param IResource $resource
|
||||
* @return string
|
||||
* @since 16.0.0
|
||||
*/
|
||||
public function getIconLink(IResource $resource): string {
|
||||
$node = $this->getNode($resource);
|
||||
if ($node && $node->getMimetype() === 'httpd/unix-directory') {
|
||||
return $this->urlGenerator->getAbsoluteURL(
|
||||
$this->urlGenerator->imagePath('core', 'places/files')
|
||||
);
|
||||
}
|
||||
return $this->urlGenerator->getAbsoluteURL(
|
||||
$this->urlGenerator->imagePath('core', 'filetypes/file')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the resource type of the provider
|
||||
*
|
||||
|
|
@ -130,15 +130,4 @@ class ResourceProvider implements IProvider {
|
|||
public function getType(): string {
|
||||
return self::RESOURCE_TYPE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the link to a resource
|
||||
*
|
||||
* @param IResource $resource
|
||||
* @return string
|
||||
* @since 16.0.0
|
||||
*/
|
||||
public function getLink(IResource $resource): string {
|
||||
return $this->urlGenerator->linkToRoute('files.viewcontroller.showFile', ['fileid' => $resource->getId()]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -241,12 +241,6 @@ class CollaborationResourcesController extends OCSController {
|
|||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'type' => $resource->getType(),
|
||||
'id' => $resource->getId(),
|
||||
'name' => $resource->getName(),
|
||||
'iconLink' => $resource->getIconLink(),
|
||||
'link' => $resource->getLink(),
|
||||
];
|
||||
return $resource->getRichObject();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -285,41 +285,23 @@ class Manager implements IManager {
|
|||
}
|
||||
|
||||
/**
|
||||
* Get the display name of a resource
|
||||
* Get the rich object data of a resource
|
||||
*
|
||||
* @param IResource $resource
|
||||
* @return string
|
||||
* @return array
|
||||
* @since 16.0.0
|
||||
*/
|
||||
public function getName(IResource $resource): string {
|
||||
public function getResourceRichObject(IResource $resource): array {
|
||||
foreach ($this->getProviders() as $provider) {
|
||||
if ($provider->getType() === $resource->getType()) {
|
||||
try {
|
||||
return $provider->getName($resource);
|
||||
return $provider->getResourceRichObject($resource);
|
||||
} catch (ResourceException $e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param IResource $resource
|
||||
* @return string
|
||||
*/
|
||||
public function getIconLink(IResource $resource): string {
|
||||
foreach ($this->getProviders() as $provider) {
|
||||
if ($provider->getType() === $resource->getType()) {
|
||||
try {
|
||||
return $provider->getIconLink($resource);
|
||||
} catch (ResourceException $e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -541,24 +523,4 @@ class Manager implements IManager {
|
|||
public function getType(): string {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the link to a resource
|
||||
*
|
||||
* @param IResource $resource
|
||||
* @return string
|
||||
* @since 16.0.0
|
||||
*/
|
||||
public function getLink(IResource $resource): string {
|
||||
foreach ($this->getProviders() as $provider) {
|
||||
if ($provider->getType() === $resource->getType()) {
|
||||
try {
|
||||
return $provider->getLink($resource);
|
||||
} catch (ResourceException $e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,14 +49,8 @@ class Resource implements IResource {
|
|||
/** @var bool|null */
|
||||
protected $access;
|
||||
|
||||
/** @var string|null */
|
||||
protected $name;
|
||||
|
||||
/** @var string|null */
|
||||
protected $iconClass;
|
||||
|
||||
/** @var string|null */
|
||||
protected $link;
|
||||
/** @var array|null */
|
||||
protected $data;
|
||||
|
||||
public function __construct(
|
||||
IManager $manager,
|
||||
|
|
@ -91,35 +85,15 @@ class Resource implements IResource {
|
|||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @return array
|
||||
* @since 16.0.0
|
||||
*/
|
||||
public function getName(): string {
|
||||
if ($this->name === null) {
|
||||
$this->name = $this->manager->getName($this);
|
||||
public function getRichObject(): array {
|
||||
if ($this->data === null) {
|
||||
$this->data = $this->manager->getResourceRichObject($this);
|
||||
}
|
||||
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @since 16.0.0
|
||||
*/
|
||||
public function getIconLink(): string {
|
||||
if ($this->iconClass === null) {
|
||||
$this->iconClass = $this->manager->getIconLink($this);
|
||||
}
|
||||
|
||||
return $this->iconClass;
|
||||
}
|
||||
|
||||
public function getLink(): string {
|
||||
if ($this->link === null) {
|
||||
$this->link = $this->manager->getLink($this);
|
||||
}
|
||||
|
||||
return $this->link;
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -38,31 +38,13 @@ interface IProvider {
|
|||
public function getType(): string;
|
||||
|
||||
/**
|
||||
* Get the display name of a resource
|
||||
* Get the rich object data of a resource
|
||||
*
|
||||
* @param IResource $resource
|
||||
* @return string
|
||||
* @return array
|
||||
* @since 16.0.0
|
||||
*/
|
||||
public function getName(IResource $resource): string;
|
||||
|
||||
/**
|
||||
* Get the icon class of a resource
|
||||
*
|
||||
* @param IResource $resource
|
||||
* @return string
|
||||
* @since 16.0.0
|
||||
*/
|
||||
public function getIconLink(IResource $resource): string;
|
||||
|
||||
/**
|
||||
* Get the link to a resource
|
||||
*
|
||||
* @param IResource $resource
|
||||
* @return string
|
||||
* @since 16.0.0
|
||||
*/
|
||||
public function getLink(IResource $resource): string;
|
||||
public function getResourceRichObject(IResource $resource): array;
|
||||
|
||||
/**
|
||||
* Can a user/guest access the collection
|
||||
|
|
|
|||
|
|
@ -42,23 +42,10 @@ interface IResource {
|
|||
public function getId(): string;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @return array
|
||||
* @since 16.0.0
|
||||
*/
|
||||
public function getName(): string;
|
||||
|
||||
/**
|
||||
* Absolute link to an icon to represent the resource
|
||||
* @return string
|
||||
* @since 16.0.0
|
||||
*/
|
||||
public function getIconLink(): string;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @since 16.0.0
|
||||
*/
|
||||
public function getLink(): string;
|
||||
public function getRichObject(): array;
|
||||
|
||||
/**
|
||||
* Can a user/guest access the resource
|
||||
|
|
|
|||
Loading…
Reference in a new issue