1<?php
2/**
3 * @copyright Copyright (c) 2016, ownCloud, Inc.
4 *
5 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
6 * @author Thomas Müller <thomas.mueller@tmit.eu>
7 *
8 * @license AGPL-3.0
9 *
10 * This code is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License, version 3,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License, version 3,
20 * along with this program. If not, see <http://www.gnu.org/licenses/>
21 *
22 */
23require '../../../../3rdparty/autoload.php';
24
25if ($argc !== 6) {
26	echo "Invalid number of arguments" . PHP_EOL;
27	exit;
28}
29
30/**
31 * @param \Sabre\DAV\Client $client
32 * @param $uploadUrl
33 * @return mixed
34 */
35function request($client, $method, $uploadUrl, $data = null, $headers = []) {
36	echo "$method $uploadUrl ... ";
37	$t0 = microtime(true);
38	$result = $client->request($method, $uploadUrl, $data, $headers);
39	$t1 = microtime(true);
40	echo $result['statusCode'] . " - " . ($t1 - $t0) . ' seconds' . PHP_EOL;
41	if (!in_array($result['statusCode'],  [200, 201])) {
42		echo $result['body'] . PHP_EOL;
43	}
44	return $result;
45}
46
47$baseUri = $argv[1];
48$userName = $argv[2];
49$password = $argv[3];
50$file = $argv[4];
51$chunkSize = $argv[5] * 1024 * 1024;
52
53$client = new \Sabre\DAV\Client([
54	'baseUri' => $baseUri,
55	'userName' => $userName,
56	'password' => $password
57]);
58
59$transfer = uniqid('transfer', true);
60$uploadUrl = "$baseUri/uploads/$userName/$transfer";
61
62request($client, 'MKCOL', $uploadUrl);
63
64$size = filesize($file);
65$stream = fopen($file, 'r');
66
67$index = 0;
68while (!feof($stream)) {
69	request($client, 'PUT', "$uploadUrl/$index", fread($stream, $chunkSize));
70	$index++;
71}
72
73$destination = pathinfo($file, PATHINFO_BASENAME);
74//echo "Moving $uploadUrl/.file to it's final destination $baseUri/files/$userName/$destination" . PHP_EOL;
75request($client, 'MOVE', "$uploadUrl/.file", null, [
76	'Destination' => "$baseUri/files/$userName/$destination",
77	'OC-Total-Length' => filesize($file),
78	'X-OC-MTime' => filemtime($file)
79]);
80