Completed
Push — fix-179 ( 161381 )
by Victor
08:27
created

RequestHelper::setSizeForPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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
class RequestHelper {
20
21
	/**
22
	 * @var  IRequest
23
	 */
24
	private $request;
25
26
	/**
27
	 * @var CappedMemoryCache
28
	 */
29
	private $fileSizeCache;
30
31
	public function __construct(IRequest $request) {
32
		$this->request = $request;
33
		$this->fileSizeCache = new CappedMemoryCache();
34
	}
35
36
	public function setSizeForPath($path, $size) {
37
		$this->fileSizeCache->set($path, $size);
38
	}
39
40
	/**
41
	 * Get current upload size
42
	 * returns null for chunks and when there is no upload
43
	 *
44
	 * @param string $path
45
	 *
46
	 * @return int|null
47
	 */
48
	public function getUploadSize($path) {
49
		$cachedSize = $this->fileSizeCache->get($path);
50
		if ($cachedSize > 0) {
51
			return $cachedSize;
52
		}
53
54
		$uploadSize = null;
55
		$requestMethod = $this->request->getMethod();
56
		$isRemoteScript = $this->isScriptName('remote.php');
57
		$isPublicScript = $this->isScriptName('public.php');
58
		// Are we uploading anything?
59
		if (\in_array($requestMethod, ['PUT']) && $isRemoteScript) {
60
			// v1 && v2 Chunks are not scanned
61
			if ($requestMethod === 'PUT' &&  \strpos($path, 'uploads/') === 0) {
62
				return null;
63
			}
64
65
			if (\OC_FileChunking::isWebdavChunk() && \strpos($path, 'cache/') === 0) {
66
				return null;
67
			}
68
69
			if ($requestMethod === 'PUT') {
70
				$uploadSize = (int)$this->request->getHeader('CONTENT_LENGTH');
71
			} else {
72
				$uploadSize = (int)$this->request->getHeader('OC_TOTAL_LENGTH');
73
			}
74
		} else if ($requestMethod === 'PUT' && $isPublicScript) {
75
			$uploadSize = (int)$this->request->getHeader('CONTENT_LENGTH');
76
		}
77
78
		return $uploadSize;
79
	}
80
81
	/**
82
	 *
83
	 * @param string $string
84
	 *
85
	 * @return bool
86
	 */
87
	public function isScriptName($string) {
88
		$pattern = \sprintf('|/%s|', \preg_quote($string));
89
		return \preg_match($pattern, $this->request->getScriptName()) === 1;
90
	}
91
}
92