Skip to content

Commit 2f8ebfa

Browse files
dcflachselibosley
authored andcommitted
Improve Dockerman icon caching.
1 parent cc6a800 commit 2f8ebfa

2 files changed

Lines changed: 43 additions & 10 deletions

File tree

emhttp/plugins/dynamix.docker.manager/include/CreateDocker.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,7 @@ function dockerUIBlockerScript($enable) {
152152
$oldXML = simplexml_load_file($filename);
153153
if ($oldXML->Icon != $_POST['contIcon']) {
154154
if (!strpos($Repository,":")) $Repository .= ":latest";
155-
$iconPath = $DockerTemplates->getIcon($Repository,$Name);
156-
@unlink("$docroot/$iconPath");
157-
@unlink("{$dockerManPaths['images']}/".basename($iconPath));
155+
$DockerTemplates->purgeUnusedIconFiles($Name);
158156
}
159157
}
160158
file_put_contents($filename, $postXML);

emhttp/plugins/dynamix.docker.manager/include/DockerClient.php

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ public function getAllInfo($reload=false,$com=true,$communityApplications=false)
340340
$iconExists = !empty($tmp['icon'])
341341
&& (is_file($tmp['icon']) || is_file($docroot . $tmp['icon']));
342342
if (!$iconExists || $reload) {
343-
$tmp['icon'] = $this->getIcon($image, $name, $labelIconUrl ?: ($tmp['icon'] ?? ''));
343+
$tmp['icon'] = $this->getIcon($image, $name, $labelIconUrl);
344344
// Explicitly return the fallback asset, so that subsequent polls see
345345
// the local file instead of rerunning the expensive template scan
346346
if (empty($tmp['icon'])) $tmp['icon'] = '/plugins/dynamix.docker.manager/images/question.png';
@@ -430,14 +430,16 @@ public function getAllInfo($reload=false,$com=true,$communityApplications=false)
430430
return $info;
431431
}
432432

433-
public function getIcon($Repository,$contName,$tmpIconUrl='') {
433+
public function getIcon($Repository,$contName,$iconUrl='') {
434434
global $docroot, $dockerManPaths;
435-
$imgUrl = $this->getTemplateValue($Repository, 'Icon','all',$contName);
436-
if (!$imgUrl) $imgUrl = $tmpIconUrl;
435+
$imgUrl = $iconUrl ?: $this->getTemplateValue($Repository, 'Icon','all',$contName);
437436
if (!$imgUrl || trim($imgUrl) == "/plugins/dynamix.docker.manager/images/question.png") return '';
438437

439-
$iconRAM = sprintf('%s/%s-%s.png', $dockerManPaths['images-ram'], $contName, 'icon');
440-
$icon = sprintf('%s/%s-%s.png', $dockerManPaths['images'], $contName, 'icon');
438+
$imgUrlHash = sha1($imgUrl);
439+
$iconFile = sprintf('%s-%s.png', 'icon', $imgUrlHash);
440+
441+
$iconRAM = sprintf('%s/%s-%s', $dockerManPaths['images-ram'], $contName, $iconFile);
442+
$icon = sprintf('%s/%s', $dockerManPaths['images'], $iconFile);
441443

442444
if (!is_dir(dirname($iconRAM))) mkdir(dirname($iconRAM), 0755, true);
443445
if (!is_dir(dirname($icon))) mkdir(dirname($icon), 0755, true);
@@ -447,14 +449,47 @@ public function getIcon($Repository,$contName,$tmpIconUrl='') {
447449
@copy($icon, $iconRAM);
448450
}
449451
if (!is_file($icon) && is_file($iconRAM)) {
450-
@copy($iconRAM,$icon);
452+
@copy($iconRAM, $icon);
451453
}
452454
if (!is_file($iconRAM)) {
453455
my_logger("$contName: Could not download icon $imgUrl");
454456
}
457+
else {
458+
$this->purgeUnusedIconFiles($contName, $iconFile);
459+
}
455460

456461
return (is_file($iconRAM)) ? str_replace($docroot, '', $iconRAM) : '';
457462
}
463+
464+
public function purgeUnusedIconFiles($contName, $keepIcon='') {
465+
global $docroot, $dockerManPaths;
466+
467+
$icon_glob = sprintf('%s/%s-*.png', $dockerManPaths['images-ram'], $contName);
468+
$ramFiles = glob($icon_glob);
469+
foreach ($ramFiles as $filename) {
470+
if ( ($keepIcon === '') || !(strpos($filename, $keepIcon) !== false) ) {
471+
@unlink($filename);
472+
}
473+
}
474+
475+
$icon_glob = sprintf('%s/%s*.png', $dockerManPaths['images'], $contName);
476+
foreach (glob($icon_glob) as $filename) {
477+
if ( ($keepIcon === '') || !(strpos($filename, $keepIcon) !== false) ) {
478+
@unlink($filename);
479+
}
480+
}
481+
482+
$icon_glob = sprintf('%s/%s*.png', $dockerManPaths['images'], $contName);
483+
foreach ($ramFiles as $ramFile) {
484+
if ( strpos($ramFile, '-icon-') !== false ) {
485+
$suffix = end(explode('-', $ramFile));
486+
if ( !glob($dockerManPaths['images-ram'].'/*icon-'.$suffix) ) {
487+
$filename = sprintf('%s/icon-%s', $dockerManPaths['images'], $suffix);
488+
@unlink($filename);
489+
}
490+
}
491+
}
492+
}
458493
}
459494

460495
####################################

0 commit comments

Comments
 (0)