|
| 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 | + |
| 22 | +namespace OCA\WorkflowEngine\Check; |
| 23 | + |
| 24 | + |
| 25 | +use OCP\Files\Storage\IStorage; |
| 26 | +use OCP\IRequest; |
| 27 | +use OCP\Util; |
| 28 | +use OCP\WorkflowEngine\ICheck; |
| 29 | + |
| 30 | +class FileSize implements ICheck { |
| 31 | + |
| 32 | + /** @var int */ |
| 33 | + protected $size; |
| 34 | + |
| 35 | + /** @var IRequest */ |
| 36 | + protected $request; |
| 37 | + |
| 38 | + /** |
| 39 | + * @param IRequest $request |
| 40 | + */ |
| 41 | + public function __construct(IRequest $request) { |
| 42 | + $this->request = $request; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @param IStorage $storage |
| 47 | + * @param string $path |
| 48 | + */ |
| 49 | + public function setFileInfo(IStorage $storage, $path) { |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @param string $operator |
| 54 | + * @param string $value |
| 55 | + * @return bool |
| 56 | + */ |
| 57 | + public function executeCheck($operator, $value) { |
| 58 | + $size = $this->getFileSizeFromHeader(); |
| 59 | + |
| 60 | + $value = Util::computerFileSize($value); |
| 61 | + if ($size !== false) { |
| 62 | + switch ($operator) { |
| 63 | + case 'less': |
| 64 | + return $size < $value; |
| 65 | + case '!less': |
| 66 | + return $size >= $value; |
| 67 | + case 'greater': |
| 68 | + return $size > $value; |
| 69 | + case '!greater': |
| 70 | + return $size <= $value; |
| 71 | + } |
| 72 | + } |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @param string $operator |
| 78 | + * @param string $value |
| 79 | + * @throws \UnexpectedValueException |
| 80 | + */ |
| 81 | + public function validateCheck($operator, $value) { |
| 82 | + if (!in_array($operator, ['less', '!less', 'greater', '!greater'])) { |
| 83 | + throw new \UnexpectedValueException('Invalid operator', 1); |
| 84 | + } |
| 85 | + |
| 86 | + if (!preg_match('/^[0-9]+[ ]?[kmgt]?b$/i', $value)) { |
| 87 | + throw new \UnexpectedValueException('Invalid file size', 2); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @return string |
| 93 | + */ |
| 94 | + protected function getFileSizeFromHeader() { |
| 95 | + if ($this->size !== null) { |
| 96 | + return $this->size; |
| 97 | + } |
| 98 | + |
| 99 | + $size = $this->request->getHeader('OC-Total-Length'); |
| 100 | + if ($size === null) { |
| 101 | + if (in_array($this->request->getMethod(), ['POST', 'PUT'])) { |
| 102 | + $size = $this->request->getHeader('Content-Length'); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if ($size === null) { |
| 107 | + $size = false; |
| 108 | + } |
| 109 | + |
| 110 | + $this->size = $size; |
| 111 | + return $this->size; |
| 112 | + } |
| 113 | +} |
0 commit comments