@@ -46,8 +46,7 @@ class UploadHelper extends Assert {
4646 * @param string|null $xRequestId
4747 * @param array|null $headers
4848 * @param int|null $davPathVersionToUse (1|2)
49- * @param int|null $chunkingVersion (1|2|null)
50- * if set to null chunking will not be used
49+ * @param bool $doChunkUpload
5150 * @param int|null $noOfChunks how many chunks to upload
5251 * @param bool|null $isGivenStep
5352 *
@@ -63,12 +62,11 @@ public static function upload(
6362 ?string $ xRequestId = '' ,
6463 ?array $ headers = [],
6564 ?int $ davPathVersionToUse = 1 ,
66- ? int $ chunkingVersion = null ,
65+ bool $ doChunkUpload = false ,
6766 ?int $ noOfChunks = 1 ,
68- ?bool $ isGivenStep = false
67+ ?bool $ isGivenStep = false ,
6968 ): ResponseInterface {
70- //simple upload with no chunking
71- if ($ chunkingVersion === null ) {
69+ if (!$ doChunkUpload ) {
7270 $ data = \file_get_contents ($ source );
7371 return WebDavHelper::makeDavRequest (
7472 $ baseUrl ,
@@ -91,56 +89,16 @@ public static function upload(
9189 null ,
9290 $ isGivenStep
9391 );
94- } else {
95- //prepare chunking
96- $ chunks = self ::chunkFile ($ source , $ noOfChunks );
97- $ chunkingId = 'chunking- ' . \rand (1000 , 9999 );
98- $ v2ChunksDestination = '/uploads/ ' . $ user . '/ ' . $ chunkingId ;
9992 }
10093
94+ //prepare chunking
95+ $ chunks = self ::chunkFile ($ source , $ noOfChunks );
96+ $ chunkingId = 'chunking- ' . \rand (1000 , 9999 );
10197 $ result = null ;
10298
103- //prepare chunking version specific stuff
104- if ($ chunkingVersion === 1 ) {
105- $ headers ['OC-Chunked ' ] = '1 ' ;
106- } elseif ($ chunkingVersion === 2 ) {
107- $ result = WebDavHelper::makeDavRequest (
108- $ baseUrl ,
109- $ user ,
110- $ password ,
111- 'MKCOL ' ,
112- $ v2ChunksDestination ,
113- $ headers ,
114- null ,
115- $ xRequestId ,
116- null ,
117- $ davPathVersionToUse ,
118- "uploads " ,
119- null ,
120- "basic " ,
121- false ,
122- 0 ,
123- null ,
124- [],
125- null ,
126- $ isGivenStep
127- );
128- if ($ result ->getStatusCode () >= 400 ) {
129- return $ result ;
130- }
131- }
132-
13399 //upload chunks
134100 foreach ($ chunks as $ index => $ chunk ) {
135- if ($ chunkingVersion === 1 ) {
136- $ filename = $ destination . "- " . $ chunkingId . "- " .
137- \count ($ chunks ) . '- ' . $ index ;
138- $ davRequestType = "files " ;
139- } else {
140- // do chunking version 2
141- $ filename = $ v2ChunksDestination . '/ ' . $ index ;
142- $ davRequestType = "uploads " ;
143- }
101+ $ filename = $ destination . "- " . $ chunkingId . "- " . \count ($ chunks ) . '- ' . $ index ;
144102 $ result = WebDavHelper::makeDavRequest (
145103 $ baseUrl ,
146104 $ user ,
@@ -152,38 +110,7 @@ public static function upload(
152110 $ xRequestId ,
153111 $ chunk ,
154112 $ davPathVersionToUse ,
155- $ davRequestType ,
156- null ,
157- "basic " ,
158- false ,
159- 0 ,
160- null ,
161- [],
162- null ,
163- $ isGivenStep
164- );
165- if ($ result ->getStatusCode () >= 400 ) {
166- return $ result ;
167- }
168- }
169- //finish upload for new chunking
170- if ($ chunkingVersion === 2 ) {
171- $ source = $ v2ChunksDestination . '/.file ' ;
172- $ headers ['Destination ' ] = $ baseUrl . "/ " .
173- WebDavHelper::getDavPath ($ davPathVersionToUse , $ user ) .
174- $ destination ;
175- $ result = WebDavHelper::makeDavRequest (
176- $ baseUrl ,
177- $ user ,
178- $ password ,
179- 'MOVE ' ,
180- $ source ,
181- $ headers ,
182- null ,
183- $ xRequestId ,
184- null ,
185- $ davPathVersionToUse ,
186- "uploads " ,
113+ "files " ,
187114 null ,
188115 "basic " ,
189116 false ,
@@ -197,88 +124,9 @@ public static function upload(
197124 return $ result ;
198125 }
199126 }
200- self ::assertNotNull ($ result , __METHOD__ . " chunking version $ chunkingVersion was requested but no upload was done. " );
201- return $ result ;
202- }
203127
204- /**
205- * Upload the same file multiple times with different mechanisms.
206- *
207- * @param string|null $baseUrl URL of owncloud
208- * @param string|null $user user who uploads
209- * @param string|null $password
210- * @param string|null $source source file path
211- * @param string|null $destination destination path on the server
212- * @param string|null $xRequestId
213- * @param bool $overwriteMode when false creates separate files to test uploading brand-new files,
214- * when true it just overwrites the same file over and over again with the same name
215- * @param string|null $exceptChunkingType empty string or "old" or "new"
216- *
217- * @return array of ResponseInterface
218- * @throws GuzzleException
219- */
220- public static function uploadWithAllMechanisms (
221- ?string $ baseUrl ,
222- ?string $ user ,
223- ?string $ password ,
224- ?string $ source ,
225- ?string $ destination ,
226- ?string $ xRequestId = '' ,
227- ?bool $ overwriteMode = false ,
228- ?string $ exceptChunkingType = ''
229- ):array {
230- $ responses = [];
231- foreach ([1 , 2 ] as $ davPathVersion ) {
232- if ($ davPathVersion === 1 ) {
233- $ davHuman = 'old ' ;
234- } else {
235- $ davHuman = 'new ' ;
236- }
237-
238- switch ($ exceptChunkingType ) {
239- case 'old ' :
240- $ exceptChunkingVersion = 1 ;
241- break ;
242- case 'new ' :
243- $ exceptChunkingVersion = 2 ;
244- break ;
245- default :
246- $ exceptChunkingVersion = -1 ;
247- break ;
248- }
249-
250- foreach ([null , 1 , 2 ] as $ chunkingVersion ) {
251- if ($ chunkingVersion === $ exceptChunkingVersion ) {
252- continue ;
253- }
254- $ valid = WebDavHelper::isValidDavChunkingCombination (
255- $ davPathVersion ,
256- $ chunkingVersion
257- );
258- if ($ valid === false ) {
259- continue ;
260- }
261- $ finalDestination = $ destination ;
262- if (!$ overwriteMode && $ chunkingVersion !== null ) {
263- $ finalDestination .= "- {$ davHuman }dav- {$ davHuman }chunking " ;
264- } elseif (!$ overwriteMode && $ chunkingVersion === null ) {
265- $ finalDestination .= "- {$ davHuman }dav-regular " ;
266- }
267- $ responses [] = self ::upload (
268- $ baseUrl ,
269- $ user ,
270- $ password ,
271- $ source ,
272- $ finalDestination ,
273- $ xRequestId ,
274- [],
275- $ davPathVersion ,
276- $ chunkingVersion ,
277- 2
278- );
279- }
280- }
281- return $ responses ;
128+ self ::assertNotNull ($ result , __METHOD__ . " chunking was requested but no upload was done. " );
129+ return $ result ;
282130 }
283131
284132 /**
0 commit comments