FileSize.php 2.7 KB

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