You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileName.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018 Daniel Kesselberg <mail@danielkesselberg.de>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\WorkflowEngine\Check;
  23. use OC\Files\Storage\Local;
  24. use OCA\WorkflowEngine\Entity\File;
  25. use OCP\Files\Mount\IMountManager;
  26. use OCP\IL10N;
  27. use OCP\IRequest;
  28. use OCP\WorkflowEngine\IFileCheck;
  29. class FileName extends AbstractStringCheck implements IFileCheck {
  30. use TFileCheck;
  31. /** @var IRequest */
  32. protected $request;
  33. /** @var IMountManager */
  34. private $mountManager;
  35. /**
  36. * @param IL10N $l
  37. * @param IRequest $request
  38. */
  39. public function __construct(IL10N $l, IRequest $request, IMountManager $mountManager) {
  40. parent::__construct($l);
  41. $this->request = $request;
  42. $this->mountManager = $mountManager;
  43. }
  44. /**
  45. * @return string
  46. */
  47. protected function getActualValue(): string {
  48. $fileName = $this->path === null ? '' : basename($this->path);
  49. if ($fileName === '' && (!$this->storage->isLocal() || $this->storage->instanceOfStorage(Local::class))) {
  50. // Return the mountpoint name of external storages that are not mounted as user home
  51. $mountPoints = $this->mountManager->findByStorageId($this->storage->getId());
  52. if (empty($mountPoints) || $mountPoints[0]->getMountType() !== 'external') {
  53. return $fileName;
  54. }
  55. $mountPointPath = rtrim($mountPoints[0]->getMountPoint(), '/');
  56. $mountPointPieces = explode('/', $mountPointPath);
  57. $mountPointName = array_pop($mountPointPieces);
  58. if (!empty($mountPointName) && $mountPointName !== 'files' && count($mountPointPieces) !== 2) {
  59. return $mountPointName;
  60. }
  61. }
  62. return $fileName;
  63. }
  64. /**
  65. * @param string $operator
  66. * @param string $checkValue
  67. * @param string $actualValue
  68. * @return bool
  69. */
  70. protected function executeStringCheck($operator, $checkValue, $actualValue): bool {
  71. if ($operator === 'is' || $operator === '!is') {
  72. $checkValue = mb_strtolower($checkValue);
  73. $actualValue = mb_strtolower($actualValue);
  74. }
  75. return parent::executeStringCheck($operator, $checkValue, $actualValue);
  76. }
  77. public function supportedEntities(): array {
  78. return [ File::class ];
  79. }
  80. public function isAvailableForScope(int $scope): bool {
  81. return true;
  82. }
  83. }