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.

FileSize.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\WorkflowEngine\Check;
  26. use OCA\WorkflowEngine\Entity\File;
  27. use OCP\IL10N;
  28. use OCP\IRequest;
  29. use OCP\Util;
  30. use OCP\WorkflowEngine\ICheck;
  31. class FileSize implements ICheck {
  32. /** @var int */
  33. protected $size;
  34. /** @var IL10N */
  35. protected $l;
  36. /** @var IRequest */
  37. protected $request;
  38. /**
  39. * @param IL10N $l
  40. * @param IRequest $request
  41. */
  42. public function __construct(IL10N $l, IRequest $request) {
  43. $this->l = $l;
  44. $this->request = $request;
  45. }
  46. /**
  47. * @param string $operator
  48. * @param string $value
  49. * @return bool
  50. */
  51. public function executeCheck($operator, $value) {
  52. $size = $this->getFileSizeFromHeader();
  53. $value = Util::computerFileSize($value);
  54. if ($size !== false) {
  55. switch ($operator) {
  56. case 'less':
  57. return $size < $value;
  58. case '!less':
  59. return $size >= $value;
  60. case 'greater':
  61. return $size > $value;
  62. case '!greater':
  63. return $size <= $value;
  64. }
  65. }
  66. return false;
  67. }
  68. /**
  69. * @param string $operator
  70. * @param string $value
  71. * @throws \UnexpectedValueException
  72. */
  73. public function validateCheck($operator, $value) {
  74. if (!in_array($operator, ['less', '!less', 'greater', '!greater'])) {
  75. throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
  76. }
  77. if (!preg_match('/^[0-9]+[ ]?[kmgt]?b$/i', $value)) {
  78. throw new \UnexpectedValueException($this->l->t('The given file size is invalid'), 2);
  79. }
  80. }
  81. /**
  82. * @return string
  83. */
  84. protected function getFileSizeFromHeader() {
  85. if ($this->size !== null) {
  86. return $this->size;
  87. }
  88. $size = $this->request->getHeader('OC-Total-Length');
  89. if ($size === '') {
  90. if (in_array($this->request->getMethod(), ['POST', 'PUT'])) {
  91. $size = $this->request->getHeader('Content-Length');
  92. }
  93. }
  94. if ($size === '') {
  95. $size = false;
  96. }
  97. $this->size = $size;
  98. return $this->size;
  99. }
  100. public function supportedEntities(): array {
  101. return [ File::class ];
  102. }
  103. public function isAvailableForScope(int $scope): bool {
  104. return true;
  105. }
  106. }