-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsync.php
More file actions
125 lines (97 loc) · 3.43 KB
/
sync.php
File metadata and controls
125 lines (97 loc) · 3.43 KB
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
125
#!/usr/bin/env php
<?php
/**
* SPDX-FileCopyrightText: 2016-2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
if (php_sapi_name() !== 'cli') {
die('Can only be invoked from CLI');
}
$allVersionsJson = file_get_contents('https://apps.nextcloud.com/api/v1/platforms.json');
if ($allVersionsJson === false) {
die('Unable to read platforms.json');
}
$allVersions = json_decode($allVersionsJson, true);
$supportedVersionObjects = array_filter($allVersions, fn (array $ver): bool => $ver['isSupported'] && str_ends_with($ver['version'], '.0.0'));
$supportedVersions = array_map(fn (array $ver): string => $ver['version'], $supportedVersionObjects);
const MAX_SCREENSHOT_SIZE = 2 * 1024 * 1024; // 2 MiB
const HTTP_TIMEOUT_S = 30;
/**
* @param string $cacheUrl path to screenshot in cache
* @param string $url HTTP URL of screenshot
* @param string $message warning message to display
*/
function generateWarningImage(string $cacheUrl, string $url, string $message): void {
$data = imagecreatetruecolor(640, 360);
if ($data === false) {
// This should never happen, but just in case, replace current cache data with an empty file instead
file_put_contents($cacheUrl, '');
echo(sprintf("Synced url %s (%s, unable to generate warning image)\n", $url, $message));
return;
}
$textColorError = imagecolorallocate($data, 255, 0, 0); // red
$textColorNormal = imagecolorallocate($data, 255, 255, 255); // white
imagestring($data, 5, 8, 150, 'Preview not available', $textColorError);
imagestring($data, 5, 8, 170, $message, $textColorNormal);
imagestring($data, 5, 8, 190, basename($url), $textColorNormal);
imagepng($data, $cacheUrl);
echo(sprintf("Synced url %s (%s)\n", $url, $message));
}
/**
* @param array $apps decoded JSON from appstore
*/
function handleApps(array $apps): void {
foreach ($apps as $app) {
foreach ($app['screenshots'] as $screenshot) {
$url = $screenshot['url'];
$trimmedUrl = trim($url);
if (!str_starts_with($trimmedUrl, 'https://')) {
continue;
}
$cacheUrl = __DIR__ . '/cache/' . base64_encode($url);
if (file_exists($cacheUrl)) {
continue;
}
$ctx = stream_context_create([
'http' => [
'timeout' => HTTP_TIMEOUT_S,
'max_redirects' => 3,
'user_agent' => 'nextcloud-usercontent-sync/1.0',
],
]);
$data = @file_get_contents($trimmedUrl, false, $ctx, 0, MAX_SCREENSHOT_SIZE + 1);
if ($data === false) {
generateWarningImage($cacheUrl, $url, 'Failed to fetch image');
continue;
}
if (strlen($data) > MAX_SCREENSHOT_SIZE) {
generateWarningImage($cacheUrl, $url, 'Image exceeds file size limit');
continue;
}
file_put_contents($cacheUrl, $data);
$mimeType = mime_content_type($cacheUrl);
if (!str_starts_with($mimeType, 'image/')) {
generateWarningImage($cacheUrl, $url, 'Image not recognized');
continue;
}
echo(sprintf("Synced url %s\n", $url));
}
}
}
foreach($supportedVersions as $version) {
$json = file_get_contents(
sprintf('https://apps.nextcloud.com/api/v1/platform/%s/apps.json', $version)
);
if ($json === false) {
echo(sprintf("Unable to read apps.json for version %s\n", $version));
continue;
}
$apps = json_decode($json, true);
handleApps($apps);
}
$json = file_get_contents('https://apps.nextcloud.com/api/v1/appapi_apps.json');
if ($json === false) {
die('Unable to read appapi_apps.json');
}
$apps = json_decode($json, true);
handleApps($apps);