FileName.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\WorkflowEngine\Check;
  8. use OC\Files\Storage\Local;
  9. use OCA\WorkflowEngine\Entity\File;
  10. use OCP\Files\Mount\IMountManager;
  11. use OCP\IL10N;
  12. use OCP\IRequest;
  13. use OCP\WorkflowEngine\IFileCheck;
  14. class FileName extends AbstractStringCheck implements IFileCheck {
  15. use TFileCheck;
  16. /** @var IRequest */
  17. protected $request;
  18. /** @var IMountManager */
  19. private $mountManager;
  20. /**
  21. * @param IL10N $l
  22. * @param IRequest $request
  23. */
  24. public function __construct(IL10N $l, IRequest $request, IMountManager $mountManager) {
  25. parent::__construct($l);
  26. $this->request = $request;
  27. $this->mountManager = $mountManager;
  28. }
  29. /**
  30. * @return string
  31. */
  32. protected function getActualValue(): string {
  33. $fileName = $this->path === null ? '' : basename($this->path);
  34. if ($fileName === '' && (!$this->storage->isLocal() || $this->storage->instanceOfStorage(Local::class))) {
  35. // Return the mountpoint name of external storage that are not mounted as user home
  36. $mountPoints = $this->mountManager->findByStorageId($this->storage->getId());
  37. if (empty($mountPoints) || $mountPoints[0]->getMountType() !== 'external') {
  38. return $fileName;
  39. }
  40. $mountPointPath = rtrim($mountPoints[0]->getMountPoint(), '/');
  41. $mountPointPieces = explode('/', $mountPointPath);
  42. $mountPointName = array_pop($mountPointPieces);
  43. if (!empty($mountPointName) && $mountPointName !== 'files' && count($mountPointPieces) !== 2) {
  44. return $mountPointName;
  45. }
  46. }
  47. return $fileName;
  48. }
  49. /**
  50. * @param string $operator
  51. * @param string $checkValue
  52. * @param string $actualValue
  53. * @return bool
  54. */
  55. protected function executeStringCheck($operator, $checkValue, $actualValue): bool {
  56. if ($operator === 'is' || $operator === '!is') {
  57. $checkValue = mb_strtolower($checkValue);
  58. $actualValue = mb_strtolower($actualValue);
  59. }
  60. return parent::executeStringCheck($operator, $checkValue, $actualValue);
  61. }
  62. public function supportedEntities(): array {
  63. return [ File::class ];
  64. }
  65. public function isAvailableForScope(int $scope): bool {
  66. return true;
  67. }
  68. }