Skip to content

Commit 9a5642a

Browse files
committed
Add file size as check
1 parent da3a289 commit 9a5642a

3 files changed

Lines changed: 166 additions & 0 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
3+
*
4+
* @license GNU AGPL version 3 or any later version
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as
8+
* published by the Free Software Foundation, either version 3 of the
9+
* License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
*/
20+
21+
(function() {
22+
23+
OCA.WorkflowEngine = OCA.WorkflowEngine || {};
24+
OCA.WorkflowEngine.Plugins = OCA.WorkflowEngine.Plugins || {};
25+
26+
OCA.WorkflowEngine.Plugins.FileSizePlugin = {
27+
getCheck: function() {
28+
return {
29+
'class': 'OCA\\WorkflowEngine\\Check\\FileSize',
30+
'name': t('workflowengine', 'File size'),
31+
'operators': [
32+
{'operator': 'less', 'name': t('workflowengine', 'less')},
33+
{'operator': '!greater', 'name': t('workflowengine', 'less or equals')},
34+
{'operator': '!less', 'name': t('workflowengine', 'greater or equals')},
35+
{'operator': 'greater', 'name': t('workflowengine', 'greater')}
36+
]
37+
};
38+
},
39+
render: function(element, classname, value) {
40+
if (classname !== 'OCA\\WorkflowEngine\\Check\\FileSize') {
41+
return;
42+
}
43+
44+
// FIXME add GB/MB/KB support
45+
$(element).css('width', '250px')
46+
.attr('placeholder', t('workflowengine', '12 MB'))
47+
;
48+
}
49+
};
50+
})();
51+
52+
OC.Plugins.register('OCA.WorkflowEngine.CheckPlugins', OCA.WorkflowEngine.Plugins.FileSizePlugin);

apps/workflowengine/lib/AppInfo/Application.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ function() {
5555
'admin',
5656

5757
// Check plugins
58+
'filesizeplugin',
5859
'filesystemtagsplugin',
5960
'usergroupmembershipplugin',
6061
]);
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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

Comments
 (0)