Completed
Pull Request — master (#226)
by Victor
21:57 queued 20:26
created

RequestHelper::getUploadSize()   C

Complexity

Conditions 11
Paths 7

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 14.8108

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 32
ccs 13
cts 19
cp 0.6842
rs 5.2653
c 3
b 0
f 0
cc 11
eloc 20
nc 7
nop 1
crap 14.8108

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * ownCloud - files_antivirus
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Viktar Dubiniuk <[email protected]>
9
 *
10
 * @copyright Viktar Dubiniuk 2017-2018
11
 * @license AGPL-3.0
12
 */
13
14
namespace OCA\Files_Antivirus;
15
16
use OC\Cache\CappedMemoryCache;
17
use \OCP\IRequest;
18
19
/**
20
 * Used to detect the size of the uploaded file
21
 *
22
 * @package OCA\Files_Antivirus
23
 */
24
class RequestHelper {
25
	/**
26
	 * @var  IRequest
27
	 */
28
	private $request;
29
30
	/**
31
	 * @var CappedMemoryCache
32
	 */
33
	private $fileSizeCache;
34
35
	/**
36
	 * RequestHelper constructor.
37
	 *
38
	 * @param IRequest $request
39
	 */
40 4
	public function __construct(IRequest $request) {
41 4
		$this->request = $request;
42 4
		$this->fileSizeCache = new CappedMemoryCache();
43 4
	}
44
45
	/**
46
	 * @param string $path
47
	 * @param string $size
48
	 *
49
	 * @return void
50
	 */
51
	public function setSizeForPath($path, $size) {
52
		$this->fileSizeCache->set($path, $size);
53
	}
54
55
	/**
56
	 * Get current upload size
57
	 * returns null for chunks and when there is no upload
58
	 *
59
	 * @param string $path
60
	 *
61
	 * @return int|null
62
	 */
63 5
	public function getUploadSize($path) {
64 5
		$cachedSize = $this->fileSizeCache->get($path);
65 5
		if ($cachedSize > 0) {
66
			return $cachedSize;
67
		}
68
69 5
		$uploadSize = null;
70 5
		$requestMethod = $this->request->getMethod();
71 5
		$isRemoteScript = $this->isScriptName('remote.php');
72 5
		$isPublicScript = $this->isScriptName('public.php');
73
		// Are we uploading anything?
74 5
		if (\in_array($requestMethod, ['PUT']) && $isRemoteScript) {
75
			// v1 && v2 Chunks are not scanned
76 1
			if ($requestMethod === 'PUT' &&  \strpos($path, 'uploads/') === 0) {
77 1
				return null;
78
			}
79
80
			if (\OC_FileChunking::isWebdavChunk() && \strpos($path, 'cache/') === 0) {
81
				return null;
82
			}
83
84
			if ($requestMethod === 'PUT') {
85
				$uploadSize = (int)$this->request->getHeader('CONTENT_LENGTH');
86
			} else {
87
				$uploadSize = (int)$this->request->getHeader('OC_TOTAL_LENGTH');
88
			}
89 4
		} else if ($requestMethod === 'PUT' && $isPublicScript) {
90 1
			$uploadSize = (int)$this->request->getHeader('CONTENT_LENGTH');
91
		}
92
93 4
		return $uploadSize;
94
	}
95
96
	/**
97
	 *
98
	 * @param string $string
99
	 *
100
	 * @return bool
101
	 */
102 5
	public function isScriptName($string) {
103 5
		$pattern = \sprintf('|/%s|', \preg_quote($string));
104 5
		return \preg_match($pattern, $this->request->getScriptName()) === 1;
105
	}
106
}
107