FileName.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. /**
  17. * @param IL10N $l
  18. * @param IRequest $request
  19. */
  20. public function __construct(
  21. IL10N $l,
  22. protected IRequest $request,
  23. private IMountManager $mountManager,
  24. ) {
  25. parent::__construct($l);
  26. }
  27. /**
  28. * @return string
  29. */
  30. protected function getActualValue(): string {
  31. $fileName = $this->path === null ? '' : basename($this->path);
  32. if ($fileName === '' && (!$this->storage->isLocal() || $this->storage->instanceOfStorage(Local::class))) {
  33. // Return the mountpoint name of external storage that are not mounted as user home
  34. $mountPoints = $this->mountManager->findByStorageId($this->storage->getId());
  35. if (empty($mountPoints) || $mountPoints[0]->getMountType() !== 'external') {
  36. return $fileName;
  37. }
  38. $mountPointPath = rtrim($mountPoints[0]->getMountPoint(), '/');
  39. $mountPointPieces = explode('/', $mountPointPath);
  40. $mountPointName = array_pop($mountPointPieces);
  41. if (!empty($mountPointName) && $mountPointName !== 'files' && count($mountPointPieces) !== 2) {
  42. return $mountPointName;
  43. }
  44. }
  45. return $fileName;
  46. }
  47. /**
  48. * @param string $operator
  49. * @param string $checkValue
  50. * @param string $actualValue
  51. * @return bool
  52. */
  53. protected function executeStringCheck($operator, $checkValue, $actualValue): bool {
  54. if ($operator === 'is' || $operator === '!is') {
  55. $checkValue = mb_strtolower($checkValue);
  56. $actualValue = mb_strtolower($actualValue);
  57. }
  58. return parent::executeStringCheck($operator, $checkValue, $actualValue);
  59. }
  60. public function supportedEntities(): array {
  61. return [ File::class ];
  62. }
  63. public function isAvailableForScope(int $scope): bool {
  64. return true;
  65. }
  66. }