Skip to content

Commit

Permalink
Improve Dockerman icon caching.
Browse files Browse the repository at this point in the history
  • Loading branch information
dcflachs committed Dec 1, 2023
1 parent 988e005 commit a5bd646
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ function cpu_pinning() {
$oldXML = simplexml_load_file($filename);
if ($oldXML->Icon != $_POST['contIcon']) {
if (!strpos($Repository,":")) $Repository .= ":latest";
$iconPath = $DockerTemplates->getIcon($Repository,$Name);
@unlink("$docroot/$iconPath");
@unlink("{$dockerManPaths['images']}/".basename($iconPath));
$DockerTemplates->purgeUnusedIconFiles($Name);
}
}
file_put_contents($filename, $postXML);
Expand Down
49 changes: 42 additions & 7 deletions emhttp/plugins/dynamix.docker.manager/include/DockerClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public function getAllInfo($reload=false,$com=true,$communityApplications=false)
if (isset($ct['Icon'])) $tmp['icon'] = $ct['Icon'];
if (isset($ct['Shell'])) $tmp['shell'] = $ct['Shell'];
if (!$communityApplications) {
if (!is_file($tmp['icon']) || $reload) $tmp['icon'] = $this->getIcon($image,$name,$tmp['icon']);
if (!is_file($tmp['icon']) || $reload) $tmp['icon'] = $this->getIcon($image,$name,$ct['Icon']);
}
if ($ct['Running']) {
$port = &$ct['Ports'][0];
Expand Down Expand Up @@ -344,15 +344,17 @@ public function getAllInfo($reload=false,$com=true,$communityApplications=false)
return $info;
}

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

$iconRAM = sprintf('%s/%s-%s.png', $dockerManPaths['images-ram'], $contName, 'icon');
$icon = sprintf('%s/%s-%s.png', $dockerManPaths['images'], $contName, 'icon');
$imgUrlHash = sha1($imgUrl);
$iconFile = sprintf('%s-%s.png', 'icon', $imgUrlHash);

$iconRAM = sprintf('%s/%s-%s', $dockerManPaths['images-ram'], $contName, $iconFile);
$icon = sprintf('%s/%s', $dockerManPaths['images'], $iconFile);

if (!is_dir(dirname($iconRAM))) mkdir(dirname($iconRAM), 0755, true);
if (!is_dir(dirname($icon))) mkdir(dirname($icon), 0755, true);

Expand All @@ -361,14 +363,47 @@ public function getIcon($Repository,$contName,$tmpIconUrl='') {
@copy($icon, $iconRAM);
}
if (!is_file($icon) && is_file($iconRAM)) {
@copy($iconRAM,$icon);
@copy($iconRAM, $icon);
}
if (!is_file($iconRAM)) {
exec("logger -t webGUI -- \"$contName: Could not download icon $imgUrl\"");
}
else {
$this->purgeUnusedIconFiles($contName, $iconFile);
}

return (is_file($iconRAM)) ? str_replace($docroot, '', $iconRAM) : '';
}

public function purgeUnusedIconFiles($contName, $keepIcon='') {
global $docroot, $dockerManPaths;

$icon_glob = sprintf('%s/%s-*.png', $dockerManPaths['images-ram'], $contName);
$ramFiles = glob($icon_glob);
foreach ($ramFiles as $filename) {
if ( ($keepIcon === '') || !(strpos($filename, $keepIcon) !== false) ) {
@unlink($filename);
}
}

$icon_glob = sprintf('%s/%s*.png', $dockerManPaths['images'], $contName);
foreach (glob($icon_glob) as $filename) {
if ( ($keepIcon === '') || !(strpos($filename, $keepIcon) !== false) ) {
@unlink($filename);
}
}

$icon_glob = sprintf('%s/%s*.png', $dockerManPaths['images'], $contName);
foreach ($ramFiles as $ramFile) {
if ( strpos($ramFile, '-icon-') !== false ) {
$suffix = end(explode('-', $ramFile));
if ( !glob($dockerManPaths['images-ram'].'/*icon-'.$suffix) ) {
$filename = sprintf('%s/icon-%s', $dockerManPaths['images'], $suffix);
@unlink($filename);
}
}
}
}
}

####################################
Expand Down

0 comments on commit a5bd646

Please sign in to comment.