1 | <?php |
||
25 | class RequestHelper { |
||
26 | /** |
||
27 | * @var IRequest |
||
28 | */ |
||
29 | private $request; |
||
30 | |||
31 | /** |
||
32 | * @var CappedMemoryCache |
||
33 | */ |
||
34 | private static $fileSizeCache; |
||
35 | |||
36 | /** |
||
37 | * RequestHelper constructor. |
||
38 | * |
||
39 | * @param IRequest $request |
||
40 | */ |
||
41 | 5 | public function __construct(IRequest $request) { |
|
44 | |||
45 | /** |
||
46 | * @return CappedMemoryCache |
||
47 | */ |
||
48 | 4 | public function getCache() { |
|
54 | |||
55 | /** |
||
56 | * @param string $path |
||
57 | * @param string $size |
||
58 | * |
||
59 | * @return void |
||
60 | */ |
||
61 | 1 | public function setSizeForPath($path, $size) { |
|
62 | 1 | $this->getCache()->set($path, $size); |
|
63 | 1 | } |
|
64 | |||
65 | /** |
||
66 | * Get current upload size |
||
67 | * returns null for chunks and when there is no upload |
||
68 | * |
||
69 | * @param Storage $storage |
||
70 | * @param string $path |
||
71 | * |
||
72 | * @return int|null |
||
73 | */ |
||
74 | 4 | public function getUploadSize(Storage $storage, $path) { |
|
75 | // remove .ocTransferId444531916.part from part files |
||
76 | 4 | $cleanedPath = preg_replace( |
|
77 | 4 | '|\.ocTransferId\d+\.part$|', |
|
78 | 4 | '', |
|
79 | 4 | $path |
|
80 | ); |
||
81 | // cache uses dav path in /files/$user/path format |
||
82 | 4 | $translatedPath = preg_replace( |
|
83 | 4 | '|^files/|', |
|
84 | 4 | 'files/' . $storage->getOwner('/') . '/', |
|
85 | 4 | $cleanedPath |
|
86 | ); |
||
87 | 4 | $cachedSize = $this->getCache()->get($translatedPath); |
|
88 | 4 | if ($cachedSize > 0) { |
|
89 | 1 | return $cachedSize; |
|
90 | } |
||
91 | |||
92 | 3 | $requestMethod = $this->request->getMethod(); |
|
93 | // Are we uploading anything? |
||
94 | 3 | if ($requestMethod !== 'PUT') { |
|
95 | 1 | return null; |
|
96 | } |
||
97 | 2 | $isRemoteScript = $this->isScriptName('remote.php'); |
|
98 | 2 | $isPublicScript = $this->isScriptName('public.php'); |
|
99 | 2 | if (!$isRemoteScript && !$isPublicScript) { |
|
100 | return null; |
||
101 | } |
||
102 | |||
103 | 2 | if ($isRemoteScript) { |
|
104 | // v1 && v2 Chunks are not scanned |
||
105 | 1 | if (\strpos($path, 'uploads/') === 0) { |
|
106 | 1 | return null; |
|
107 | } |
||
108 | |||
109 | if (\OC_FileChunking::isWebdavChunk() |
||
110 | && \strpos($path, 'cache/') === 0 |
||
111 | ) { |
||
112 | return null; |
||
113 | } |
||
114 | } |
||
115 | 1 | $uploadSize = (int)$this->request->getHeader('CONTENT_LENGTH'); |
|
116 | |||
117 | 1 | return $uploadSize; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * |
||
122 | * @param string $string |
||
123 | * |
||
124 | * @return bool |
||
125 | */ |
||
126 | 2 | public function isScriptName($string) { |
|
130 | } |
||
131 |