1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016 Joas Schilling <[email protected]> |
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\IL10N; |
27
|
|
|
use OCP\IRequest; |
28
|
|
|
use OCP\Util; |
29
|
|
|
use OCP\WorkflowEngine\ICheck; |
30
|
|
|
|
31
|
|
|
class FileSize implements ICheck { |
32
|
|
|
|
33
|
|
|
/** @var int */ |
34
|
|
|
protected $size; |
35
|
|
|
|
36
|
|
|
/** @var IL10N */ |
37
|
|
|
protected $l; |
38
|
|
|
|
39
|
|
|
/** @var IRequest */ |
40
|
|
|
protected $request; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param IL10N $l |
44
|
|
|
* @param IRequest $request |
45
|
|
|
*/ |
46
|
|
|
public function __construct(IL10N $l, IRequest $request) { |
47
|
|
|
$this->l = $l; |
48
|
|
|
$this->request = $request; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param IStorage $storage |
53
|
|
|
* @param string $path |
54
|
|
|
*/ |
55
|
|
|
public function setFileInfo(IStorage $storage, $path) { |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $operator |
60
|
|
|
* @param string $value |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public function executeCheck($operator, $value) { |
64
|
|
|
$size = $this->getFileSizeFromHeader(); |
65
|
|
|
|
66
|
|
|
$value = Util::computerFileSize($value); |
67
|
|
|
if ($size !== false) { |
68
|
|
|
switch ($operator) { |
69
|
|
|
case 'less': |
70
|
|
|
return $size < $value; |
71
|
|
|
case '!less': |
72
|
|
|
return $size >= $value; |
73
|
|
|
case 'greater': |
74
|
|
|
return $size > $value; |
75
|
|
|
case '!greater': |
76
|
|
|
return $size <= $value; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $operator |
84
|
|
|
* @param string $value |
85
|
|
|
* @throws \UnexpectedValueException |
86
|
|
|
*/ |
87
|
|
|
public function validateCheck($operator, $value) { |
88
|
|
View Code Duplication |
if (!in_array($operator, ['less', '!less', 'greater', '!greater'])) { |
|
|
|
|
89
|
|
|
throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (!preg_match('/^[0-9]+[ ]?[kmgt]?b$/i', $value)) { |
93
|
|
|
throw new \UnexpectedValueException($this->l->t('The given file size is invalid'), 2); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
protected function getFileSizeFromHeader() { |
101
|
|
|
if ($this->size !== null) { |
102
|
|
|
return $this->size; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$size = $this->request->getHeader('OC-Total-Length'); |
106
|
|
|
if ($size === null) { |
107
|
|
|
if (in_array($this->request->getMethod(), ['POST', 'PUT'])) { |
108
|
|
|
$size = $this->request->getHeader('Content-Length'); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if ($size === null) { |
113
|
|
|
$size = false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$this->size = $size; |
|
|
|
|
117
|
|
|
return $this->size; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.