Image synchronization script optimized
gitea/hedera-web/pipeline/head This commit looks good
Details
gitea/hedera-web/pipeline/head This commit looks good
Details
This commit is contained in:
parent
1c6d346f51
commit
7a46fc0bbc
|
@ -1,4 +1,4 @@
|
||||||
hedera-web (1.407.25) stable; urgency=low
|
hedera-web (1.407.26) stable; urgency=low
|
||||||
|
|
||||||
* Initial Release.
|
* Initial Release.
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "hedera-web",
|
"name": "hedera-web",
|
||||||
"version": "1.407.25",
|
"version": "1.407.26",
|
||||||
"description": "Verdnatura web page",
|
"description": "Verdnatura web page",
|
||||||
"license": "GPL-3.0",
|
"license": "GPL-3.0",
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|
|
@ -3,8 +3,7 @@
|
||||||
require_once(__DIR__.'/util.php');
|
require_once(__DIR__.'/util.php');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Syncronizes the data directory with the database, this may take
|
* Syncronizes the data directory with the database, this may take some time.
|
||||||
* some time.
|
|
||||||
*/
|
*/
|
||||||
class Sync extends Vn\Lib\Method {
|
class Sync extends Vn\Lib\Method {
|
||||||
private $trashSubdir;
|
private $trashSubdir;
|
||||||
|
@ -22,89 +21,64 @@ class Sync extends Vn\Lib\Method {
|
||||||
set_time_limit(0);
|
set_time_limit(0);
|
||||||
$this->trashSubdir = date('YmdHis');
|
$this->trashSubdir = date('YmdHis');
|
||||||
|
|
||||||
$checkCount = 0;
|
$deleteCount = 0;
|
||||||
$query = 'SELECT DISTINCT `%3$s` FROM `%1$s`.`%2$s`
|
|
||||||
WHERE `%3$s` IS NOT NULL AND `%3$s` != \'\'';
|
|
||||||
|
|
||||||
$dir = opendir($this->dataDir);
|
$dir = opendir($this->dataDir);
|
||||||
|
|
||||||
if ($dir)
|
if ($dir)
|
||||||
while ($schema = readdir($dir))
|
while ($collection = readdir($dir))
|
||||||
if (!in_array($schema, ['.', '..'])) {
|
if (!in_array($collection, ['.', '..'])) {
|
||||||
$info = $this->util->loadInfo($schema);
|
$info = $this->util->loadInfo($collection);
|
||||||
$schemaPath = "{$this->dataDir}/$schema";
|
$collectionPath = "{$this->dataDir}/$collection";
|
||||||
|
|
||||||
// Deletes unreferenced schemas.
|
// Deletes unreferenced collections.
|
||||||
|
|
||||||
if (!isset($info)) {
|
if (!isset($info)) {
|
||||||
$this->moveTrash($schema);
|
$this->recycle($collection);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deletes unreferenced sizes.
|
// Deletes unreferenced sizes.
|
||||||
|
|
||||||
$schemaDir = opendir($schemaPath);
|
$collectionDir = opendir($collectionPath);
|
||||||
|
|
||||||
if ($schemaDir)
|
if ($collectionDir)
|
||||||
while ($size = readdir($schemaDir))
|
while ($size = readdir($collectionDir))
|
||||||
if (!in_array($size, ['.', '..', 'full'])
|
if (!in_array($size, ['.', '..', 'full'])
|
||||||
&& !isset($info['sizes'][$size]))
|
&& !isset($info['sizes'][$size]))
|
||||||
$this->moveTrash("$schema/$size");
|
$this->recycle("$collection/$size");
|
||||||
|
|
||||||
// Gets a list of referenced images from the database.
|
|
||||||
|
|
||||||
$result = $db->query(sprintf($query
|
|
||||||
,$info['schema']
|
|
||||||
,$info['table']
|
|
||||||
,$info['column']
|
|
||||||
));
|
|
||||||
|
|
||||||
if (!$result)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
$map = [];
|
|
||||||
|
|
||||||
while ($row = $result->fetch_row()) {
|
|
||||||
$map[$row[0]] = TRUE;
|
|
||||||
$checkCount++;
|
|
||||||
}
|
|
||||||
|
|
||||||
$result->free();
|
|
||||||
|
|
||||||
// Deletes unreferenced images.
|
// Deletes unreferenced images.
|
||||||
|
|
||||||
$this->cleanImages($schema, 'full', $map);
|
|
||||||
|
|
||||||
foreach ($info['sizes'] as $size => $i)
|
$res = $db->query(
|
||||||
$this->cleanImages($schema, $size, $map);
|
'SELECT `name`, collectionFk
|
||||||
|
FROM `image`
|
||||||
|
WHERE nRefs = 0 AND collectionFk = #collection',
|
||||||
|
['collection' => $collection]
|
||||||
|
);
|
||||||
|
|
||||||
|
while ($image = $res->fetch_object()) {
|
||||||
|
$deleteCount++;
|
||||||
|
$this->recycle("$collection/full/{$image->name}.png");
|
||||||
|
|
||||||
|
foreach ($info['sizes'] as $size => $i)
|
||||||
|
$this->recycle("$collection/$size/{$image->name}.png");
|
||||||
|
}
|
||||||
|
|
||||||
|
$res->free();
|
||||||
}
|
}
|
||||||
|
|
||||||
echo "Syncronization finished.\n";
|
echo "Syncronization finished. $deleteCount images moved to trash.\n";
|
||||||
}
|
|
||||||
|
|
||||||
function cleanImages($schema, $size, &$map) {
|
|
||||||
$sizePath = "{$this->dataDir}/$schema/$size";
|
|
||||||
|
|
||||||
if (!is_dir($sizePath))
|
|
||||||
return;
|
|
||||||
|
|
||||||
$iter = new DirectoryIterator($sizePath);
|
|
||||||
|
|
||||||
for (; $iter->valid(); $iter->next())
|
|
||||||
if (!$iter->isDir() && strripos($iter->getFilename(), '.png', -4) !== FALSE) {
|
|
||||||
$name = substr($iter->getFilename(), 0, -4);
|
|
||||||
|
|
||||||
if (!isset($map[$name]))
|
|
||||||
$this->moveTrash("$schema/$size/". $iter->getFilename());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Moves a data directory to the trash.
|
* Moves data file to trash.
|
||||||
*
|
*
|
||||||
* @param string $file The file to move to the trash
|
* @param string $file File or directory to move
|
||||||
*/
|
*/
|
||||||
function moveTrash($file) {
|
function recycle($file) {
|
||||||
|
$filePath = "{$this->dataDir}/$file";
|
||||||
|
if (!file_exists($filePath)) return;
|
||||||
|
|
||||||
$trashBasedir = "{$this->dataDir}/.trash/". $this->trashSubdir;
|
$trashBasedir = "{$this->dataDir}/.trash/". $this->trashSubdir;
|
||||||
$trashdir = "$trashBasedir/". dirname($file);
|
$trashdir = "$trashBasedir/". dirname($file);
|
||||||
|
|
||||||
|
@ -112,9 +86,8 @@ class Sync extends Vn\Lib\Method {
|
||||||
mkdir($trashdir, 0775, TRUE);
|
mkdir($trashdir, 0775, TRUE);
|
||||||
|
|
||||||
rename(
|
rename(
|
||||||
"{$this->dataDir}/$file",
|
"$filePath",
|
||||||
"$trashBasedir/$file"
|
"$trashBasedir/$file"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,17 +15,17 @@ class Util {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads information for specified schema.
|
* Loads information for specified collection.
|
||||||
*
|
*
|
||||||
* @param string $schema The schema name
|
* @param string $collection The schema name
|
||||||
*/
|
*/
|
||||||
function loadInfo($schema) {
|
function loadInfo($collection) {
|
||||||
$db = $this->app->getSysConn();
|
$db = $this->app->getSysConn();
|
||||||
|
|
||||||
$info = $db->getRow(
|
$info = $db->getRow(
|
||||||
'SELECT id, maxWidth, maxHeight, `schema`, `table`, `column`
|
'SELECT id, maxWidth, maxHeight, `schema`, `table`, `column`
|
||||||
FROM imageCollection WHERE name = #schema'
|
FROM imageCollection WHERE name = #collection'
|
||||||
,['schema' => $schema]
|
,['collection' => $collection]
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!$info)
|
if (!$info)
|
||||||
|
|
Loading…
Reference in New Issue