Skip to content

Commit

Permalink
Improve Dockerman icon caching.
Browse files Browse the repository at this point in the history
  • Loading branch information
dcflachs authored Aug 5, 2022
1 parent 814d8d4 commit 0365a1e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
4 changes: 1 addition & 3 deletions plugins/dynamix.docker.manager/include/CreateDocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,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
52 changes: 43 additions & 9 deletions plugins/dynamix.docker.manager/include/DockerClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public function getAllInfo($reload=false,$com=true,$communityApplications=false)
if ($ct['Url'] && !$tmp['url']) $tmp['url'] = $ct['Url'];
if ($ct['Icon']) $tmp['icon'] = $ct['Icon'];
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 @@ -336,15 +336,16 @@ 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 '';

$imageName = $contName ?: $name;
$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 @@ -354,14 +355,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("/usr/bin/logger ".escapeshellarg("$imageName: Could not download icon $imgUrl"));
exec("/usr/bin/logger ".escapeshellarg("$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 0365a1e

Please sign in to comment.