forked from verdnatura/hedera-web
Image synchronization script optimized
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.
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "hedera-web",
|
||||
"version": "1.407.25",
|
||||
"version": "1.407.26",
|
||||
"description": "Verdnatura web page",
|
||||
"license": "GPL-3.0",
|
||||
"repository": {
|
||||
|
|
|
@ -3,8 +3,7 @@
|
|||
require_once(__DIR__.'/util.php');
|
||||
|
||||
/**
|
||||
* Syncronizes the data directory with the database, this may take
|
||||
* some time.
|
||||
* Syncronizes the data directory with the database, this may take some time.
|
||||
*/
|
||||
class Sync extends Vn\Lib\Method {
|
||||
private $trashSubdir;
|
||||
|
@ -22,89 +21,64 @@ class Sync extends Vn\Lib\Method {
|
|||
set_time_limit(0);
|
||||
$this->trashSubdir = date('YmdHis');
|
||||
|
||||
$checkCount = 0;
|
||||
$query = 'SELECT DISTINCT `%3$s` FROM `%1$s`.`%2$s`
|
||||
WHERE `%3$s` IS NOT NULL AND `%3$s` != \'\'';
|
||||
|
||||
$deleteCount = 0;
|
||||
$dir = opendir($this->dataDir);
|
||||
|
||||
|
||||
if ($dir)
|
||||
while ($schema = readdir($dir))
|
||||
if (!in_array($schema, ['.', '..'])) {
|
||||
$info = $this->util->loadInfo($schema);
|
||||
$schemaPath = "{$this->dataDir}/$schema";
|
||||
|
||||
// Deletes unreferenced schemas.
|
||||
|
||||
while ($collection = readdir($dir))
|
||||
if (!in_array($collection, ['.', '..'])) {
|
||||
$info = $this->util->loadInfo($collection);
|
||||
$collectionPath = "{$this->dataDir}/$collection";
|
||||
|
||||
// Deletes unreferenced collections.
|
||||
|
||||
if (!isset($info)) {
|
||||
$this->moveTrash($schema);
|
||||
$this->recycle($collection);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Deletes unreferenced sizes.
|
||||
|
||||
$schemaDir = opendir($schemaPath);
|
||||
$collectionDir = opendir($collectionPath);
|
||||
|
||||
if ($schemaDir)
|
||||
while ($size = readdir($schemaDir))
|
||||
if ($collectionDir)
|
||||
while ($size = readdir($collectionDir))
|
||||
if (!in_array($size, ['.', '..', 'full'])
|
||||
&& !isset($info['sizes'][$size]))
|
||||
$this->moveTrash("$schema/$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();
|
||||
$this->recycle("$collection/$size");
|
||||
|
||||
// Deletes unreferenced images.
|
||||
|
||||
$this->cleanImages($schema, 'full', $map);
|
||||
|
||||
foreach ($info['sizes'] as $size => $i)
|
||||
$this->cleanImages($schema, $size, $map);
|
||||
$res = $db->query(
|
||||
'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";
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
echo "Syncronization finished. $deleteCount images moved to trash.\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
$trashdir = "$trashBasedir/". dirname($file);
|
||||
|
||||
|
@ -112,9 +86,8 @@ class Sync extends Vn\Lib\Method {
|
|||
mkdir($trashdir, 0775, TRUE);
|
||||
|
||||
rename(
|
||||
"{$this->dataDir}/$file",
|
||||
"$filePath",
|
||||
"$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();
|
||||
|
||||
$info = $db->getRow(
|
||||
'SELECT id, maxWidth, maxHeight, `schema`, `table`, `column`
|
||||
FROM imageCollection WHERE name = #schema'
|
||||
,['schema' => $schema]
|
||||
FROM imageCollection WHERE name = #collection'
|
||||
,['collection' => $collection]
|
||||
);
|
||||
|
||||
if (!$info)
|
||||
|
|
Loading…
Reference in New Issue