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 | 1 | public function getCache() { |
|
49 | 1 | if (self::$fileSizeCache === null) { |
|
50 | 1 | self::$fileSizeCache = new CappedMemoryCache(); |
|
51 | 1 | } |
|
52 | 1 | return self::$fileSizeCache; |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param string $path |
||
57 | * @param string $size |
||
58 | * |
||
59 | * @return void |
||
60 | */ |
||
61 | 1 | public function setSizeForPath($path, $size) { |
|
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 | 4 | $requestMethod = $this->request->getMethod(); |
|
76 | |||
77 | // Handle MOVE first |
||
78 | // the size is cached by the app dav plugin in this case |
||
79 | 4 | if ($requestMethod === 'MOVE') { |
|
80 | // remove .ocTransferId444531916.part from part files |
||
81 | 1 | $cleanedPath = \preg_replace( |
|
82 | 1 | '|\.ocTransferId\d+\.part$|', |
|
83 | 1 | '', |
|
84 | $path |
||
85 | 1 | ); |
|
86 | // cache uses dav path in /files/$user/path format |
||
87 | 1 | $translatedPath = \preg_replace( |
|
88 | 1 | '|^files/|', |
|
89 | 1 | 'files/' . $storage->getOwner('/') . '/', |
|
90 | $cleanedPath |
||
91 | 1 | ); |
|
92 | 1 | $cachedSize = $this->getCache()->get($translatedPath); |
|
93 | 1 | if ($cachedSize > 0) { |
|
94 | 1 | return $cachedSize; |
|
95 | } |
||
96 | } |
||
97 | |||
98 | // Are we uploading anything? |
||
99 | 3 | if ($requestMethod !== 'PUT') { |
|
100 | 1 | return null; |
|
101 | } |
||
102 | 2 | $isRemoteScript = $this->isScriptName('remote.php'); |
|
103 | 2 | $isPublicScript = $this->isScriptName('public.php'); |
|
104 | 2 | if (!$isRemoteScript && !$isPublicScript) { |
|
105 | return null; |
||
106 | } |
||
107 | |||
108 | 2 | if ($isRemoteScript) { |
|
109 | // v1 && v2 Chunks are not scanned |
||
110 | 1 | if (\strpos($path, 'uploads/') === 0) { |
|
111 | 1 | return null; |
|
112 | } |
||
113 | |||
114 | if (\OC_FileChunking::isWebdavChunk() |
||
115 | && \strpos($path, 'cache/') === 0 |
||
116 | ) { |
||
117 | return null; |
||
118 | } |
||
119 | } |
||
120 | 1 | $uploadSize = (int)$this->request->getHeader('CONTENT_LENGTH'); |
|
121 | |||
122 | 1 | return $uploadSize; |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * |
||
127 | * @param string $string |
||
128 | * |
||
129 | * @return bool |
||
130 | */ |
||
131 | 2 | public function isScriptName($string) { |
|
135 | } |
||
136 |