forked from stellwerke-info/tile_cache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcron.php
More file actions
76 lines (63 loc) · 2.52 KB
/
Copy pathcron.php
File metadata and controls
76 lines (63 loc) · 2.52 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
<?php
\chdir( __DIR__ );
if ( ! \file_exists( 'config.php' ) ) {
die( 'No config file' );
}
require_once( 'config.php' );
if ( \php_sapi_name() !== 'cli' ) {
\http_response_code( 403 );
die();
}
if ( \getcwd() !== __DIR__ ) {
die( 'Could not change into tile cache root directory...' );
}
// this function is based on: https://github.com/cyclestreets/tilecache/blob/master/index.php.
function clean_tiles( int $expiryDays ) : void {
$layers = \array_keys( TILECACHE_LAYERS ?? [] );
$layers = \array_filter( $layers, fn( string $l ) => \strlen( $l ) <= 10 && \preg_match( '/^[a-z]+$/', $l ) );
if ( $layers === [] ) {
echo 'No layers defined!' . \PHP_EOL;
return;
}
$layers_shellsafe = \implode( ',', $layers );
$clean_maxz = 0;
foreach ( TILECACHE_LAYERS as [ $_, $maxz ] ) {
$clean_maxz = \max([$clean_maxz, $maxz]);
}
$repo_safe = TILECACHE_REPOSITORY ?? '';
if ( ! \is_string( $repo_safe ) || ! \preg_match( '/^[a-z_]+$/', $repo_safe ) ) {
echo 'Invalid repo' . \PHP_EOL;
return;
}
if ( $repo_safe != '' ) {
$repo_safe = '/' . $repo_safe;
if ( ! is_dir( __DIR__ . '/' . $repo_safe ) ) {
echo 'Invalid repo' . \PHP_EOL;
return;
}
}
// Command to clear out the tiles from subfolders
$command = 'find .' . $repo_safe . ' -mindepth 2 -type f -regex \'.*\\.\\(png\\|webp\\|avif\\)\' -mtime +' . $expiryDays . ' -delete';
echo 'Starting tile clearance: ' . $command . \PHP_EOL;
$lastLine = \exec($command);
echo 'Completed tile clearance: ' . $lastLine . \PHP_EOL;
if (
\defined( 'TILECACHE_CLEAN_DAYS_HIGH_RES' ) && is_int( TILECACHE_CLEAN_DAYS_HIGH_RES ) && TILECACHE_CLEAN_DAYS_HIGH_RES > 0
&& \is_int( $clean_maxz ) && $clean_maxz > 15
) {
$command = 'find .' . $repo_safe . '/{'. $layers_shellsafe . '}/{15..' . $clean_maxz . '} -type f -regex \'.*\\.\\(png\\|webp\\|avif\\)\' -mtime +' . ( (int) TILECACHE_CLEAN_DAYS_HIGH_RES ) . ' -delete';
echo 'Starting high-res tile clearance: ' . $command . \PHP_EOL;
$lastLine = \exec($command);
echo 'Completed high-res tile clearance: ' . $lastLine . \PHP_EOL;
}
// Remove all empty folders
$command = 'find .' . $repo_safe . ' -mindepth 2 -type d -empty -exec rmdir {} \; -prune';
echo 'Starting empty folder clearance: ' . $command . \PHP_EOL;
$lastLine = \exec($command);
echo 'Completed empty folder clearance: ' . $lastLine . \PHP_EOL;
$quota = \shell_exec('quota -gls');
if ( $quota !== \PHP_EOL ) {
echo \PHP_EOL . \PHP_EOL . 'Quota: ' . \PHP_EOL . $quota . \PHP_EOL;
}
}
clean_tiles( TILECACHE_CLEAN_DAYS ?? 35 );