-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgp2nc.php
124 lines (99 loc) · 4 KB
/
gp2nc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
require __DIR__ . '/vendor/autoload.php';
use Rikmeijer\Googlephotos2nextcloud\DirectoryTask;
use Rikmeijer\Googlephotos2nextcloud\IO;
use Amp\Future;
use Amp\Parallel\Worker;
use Amp\Parallel\Worker\ContextWorkerPool;
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->safeLoad();
define('WORKING_DIRECTORY', getcwd());
if (WORKING_DIRECTORY === false) {
exit('Could not determine current working directory');
}
IO::write('Working from ' . WORKING_DIRECTORY);
if (isset($_ENV['NEXTCLOUD_URL']) === false) {
exit('Nextcloud URL missing, please set in .env or as environment variable');
}
$parsed_url = parse_url($_ENV['NEXTCLOUD_URL']);
$origin = [$parsed_url['scheme'] . '://', $parsed_url['host']];
if (isset($parsed_url['port'])) {
$origin[] = $parsed_url['port'];
}
define('NEXTCLOUD_URL', implode($origin));
IO::write('Working on ' . NEXTCLOUD_URL);
define('NEXTCLOUD_USER', $parsed_url['user']);
IO::write('Working as ' . NEXTCLOUD_USER);
define('NEXTCLOUD_PASSWORD', $parsed_url['pass'] ?? null);
if (NEXTCLOUD_PASSWORD !== null) {
IO::write('Identifing with a password.');
}
define('NEXTCLOUD_UPLOAD_PATH', $parsed_url['path']);
define('UPLOAD_MODE', $_ENV['UPLOAD_MODE'] ?? 'copy');
if (UPLOAD_MODE === 'move') {
IO::write('Moving photos to ' . NEXTCLOUD_UPLOAD_PATH);
} else {
IO::write('Copying photos to ' . NEXTCLOUD_UPLOAD_PATH);
}
$client = new Sabre\DAV\Client([
'baseUri' => NEXTCLOUD_URL . '/remote.php/dav',
'userName' => NEXTCLOUD_USER,
'password' => NEXTCLOUD_PASSWORD
]);
$user_albums = IO::readJson(WORKING_DIRECTORY . "/user-generated-memory-titles.json")['title'];
IO::write('Found ' . count($user_albums) . ' user albums');
$files_base_path = '/remote.php/dav/files/' . NEXTCLOUD_USER;
$albums_base_path = '/remote.php/dav/photos/' . NEXTCLOUD_USER . '/albums';
$available_album_resources = $client->propfind($albums_base_path, [
'{DAV:}displayname',
'{DAV:}getcontentlength',
], 1);
$available_albums = [];
foreach ($available_album_resources as $album_id => $available_album) {
$available_albums[] = rawurldecode(basename($album_id));
}
IO::write('Found ' . count($available_albums) . ' available albums');
$createable_albums = array_diff($user_albums, $available_albums);
IO::write('Creating ' . count($createable_albums) . ' albums');
foreach ($createable_albums as $creatable_album) {
IO::write('--> ' . rawurlencode($creatable_album));
$client->request('MKCOL', $albums_base_path . '/' . rawurlencode($creatable_album));
}
$available_directory_resources = $client->propfind($files_base_path . NEXTCLOUD_UPLOAD_PATH, [
'{DAV:}displayname',
'{DAV:}getcontentlength',
], 1);
$available_directories = [];
foreach ($available_directory_resources as $directory_id => $available_directory_resource) {
$available_directories[] = rawurldecode(basename($directory_id));
}
IO::write('Walking directories...');
$pool = Worker\workerPool(new ContextWorkerPool(5));
$executions = [];
foreach (glob(WORKING_DIRECTORY . '/*') as $path) {
if (is_dir($path) === false) {
continue;
}
// FetchTask is just an example, you'll have to implement
// the Task interface for your task.
$executions[$path] = Worker\submit(new DirectoryTask(
$path,
$files_base_path . NEXTCLOUD_UPLOAD_PATH,
$albums_base_path,
$user_albums,
NEXTCLOUD_URL,
NEXTCLOUD_USER,
NEXTCLOUD_PASSWORD,
$_ENV['UPLOAD_MODE'] ?? 'copy'
));
}
// Each submission returns an Execution instance to allow two-way
// communication with a task. Here we're only interested in the
// task result, so we use the Future from Execution::getFuture()
$responses = Future\await(array_map(
fn(Worker\Execution $e) => $e->getFuture(),
$executions,
));
foreach ($responses as $url => $response) {
\printf("Read %d bytes from %s\n", \strlen($response), $url);
}