Skip to content

Commit

Permalink
Merge branch '2023.11' into 2024.11
Browse files Browse the repository at this point in the history
  • Loading branch information
gitlabci committed Jan 25, 2024
2 parents 0195c6e + f392c3d commit 46ebc16
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 23 deletions.
80 changes: 61 additions & 19 deletions tine20/Tinebase/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4176,17 +4176,22 @@ public function sanitizeMimeTypes()
/**
* create preview for files without a preview, delete previews for already deleted files
*
* @param array $ids
* @return bool
* @throws Tinebase_Exception_InvalidArgument
* @throws Zend_Db_Statement_Exception
*/
public function sanitizePreviews(): bool
public function sanitizePreviews(array $ids = []): bool
{
if (! $this->isPreviewActive()) {
Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Previews are disabled');
return true;
}

Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Starting to sanitize previews');
if (! empty($ids)) {
Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ids: ' . print_r($ids, true));
}

$treeNodeBackend = $this->_getTreeNodeBackend();
$previewController = Tinebase_FileSystem_Previews::getInstance();
Expand All @@ -4195,17 +4200,27 @@ public function sanitizePreviews(): bool
$created = 0;
$deleted = 0;

foreach ($treeNodeBackend->search(
new Tinebase_Model_Tree_Node_Filter([
['field' => 'type', 'operator' => 'equals', 'value' => Tinebase_Model_Tree_FileObject::TYPE_FILE]
], '', ['ignoreAcl' => true])
, null, true) as $id) {

$filterData = [[
'field' => 'type',
'operator' => 'equals',
'value' => Tinebase_Model_Tree_FileObject::TYPE_FILE
]];
if (! empty($ids)) {
$filterData[] = ['field' => 'id', 'operator' => 'in', 'value' => $ids];
}
$nodeIds = $treeNodeBackend->search(
new Tinebase_Model_Tree_Node_Filter($filterData, '', ['ignoreAcl' => true]),
null, true);
Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Found '
. count($nodeIds) . ' file nodes');

foreach ($nodeIds as $id) {
/** @var Tinebase_Model_Tree_Node $node */
try {
$treeNodeBackend->setRevision(null);
$node = $treeNodeBackend->get($id);
} catch (Tinebase_Exception_NotFound $tenf) {
Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Node not found');
continue;
}

Expand All @@ -4219,6 +4234,7 @@ public function sanitizePreviews(): bool
try {
$actualNode = $treeNodeBackend->get($id);
} catch (Tinebase_Exception_NotFound $tenf) {
Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Node not found');
continue;
} finally {
$treeNodeBackend->setRevision(null);
Expand All @@ -4228,16 +4244,19 @@ public function sanitizePreviews(): bool
}

if (!$previewController->canNodeHavePreviews($actualNode)) {
Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Node cannot have previews');
continue;
}

if ($previewController->hasPreviews($actualNode)) {
Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Node already has preview');
$validHashes[$actualNode->hash] = true;
continue;
}

try {
if (!$previewController->createPreviews($actualNode)) {
Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Preview creation failed');
continue;
}
} catch (Tinebase_Exception_QuotaExceeded $teqe) {
Expand All @@ -4255,10 +4274,18 @@ public function sanitizePreviews(): bool
$treeNodeBackend->setRevision(null);

$parents = array();
foreach($treeNodeBackend->search(
new Tinebase_Model_Tree_Node_Filter([
['field' => 'type', 'operator' => 'equals', 'value' => Tinebase_Model_Tree_FileObject::TYPE_PREVIEW]
], '', ['ignoreAcl' => true])

$filterData = [[
'field' => 'type',
'operator' => 'equals',
'value' => Tinebase_Model_Tree_FileObject::TYPE_PREVIEW
]];
if (! empty($ids)) {
$filterData[] = ['field' => 'id', 'operator' => 'in', 'value' => $ids];
}

foreach ($treeNodeBackend->search(
new Tinebase_Model_Tree_Node_Filter($filterData, '', ['ignoreAcl' => true])
, null, true) as $id) {
/** @var Tinebase_Model_Tree_Node $fileNode */
try {
Expand Down Expand Up @@ -4307,14 +4334,31 @@ public function sanitizePreviews(): bool
Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
. ' created ' . $created . ' new previews, deleted ' . $deleted . ' previews.');

// check for empty preview folders and delete them
if (empty($ids)) {
$this->_removeEmptyPreviewFolders();
}

return true;
}

/**
* check for empty preview folders and delete them
*
* @return void
* @throws Tinebase_Exception_InvalidArgument
*/
protected function _removeEmptyPreviewFolders(): void
{
$treeNodeBackend = $this->_getTreeNodeBackend();
$previewController = Tinebase_FileSystem_Previews::getInstance();

$baseNode = $previewController->getBasePathNode();
foreach($treeNodeBackend->search(
new Tinebase_Model_Tree_Node_Filter([
['field' => 'type', 'operator' => 'equals', 'value' => Tinebase_Model_Tree_FileObject::TYPE_FOLDER],
['field' => 'parent_id', 'operator' => 'equals', 'value' => $baseNode->getId()],
], '', ['ignoreAcl' => true])
, null, true) as $id) {
new Tinebase_Model_Tree_Node_Filter([
['field' => 'type', 'operator' => 'equals', 'value' => Tinebase_Model_Tree_FileObject::TYPE_FOLDER],
['field' => 'parent_id', 'operator' => 'equals', 'value' => $baseNode->getId()],
], '', ['ignoreAcl' => true])
, null, true) as $id) {
if (count($treeNodeBackend->search(
new Tinebase_Model_Tree_Node_Filter([
['field' => 'parent_id', 'operator' => 'equals', 'value' => $id],
Expand All @@ -4327,8 +4371,6 @@ public function sanitizePreviews(): bool
Tinebase_Lock::keepLocksAlive();
}
}

return true;
}

/**
Expand Down
1 change: 0 additions & 1 deletion tine20/Tinebase/FileSystem/Previews.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ public function canNodeHavePreviews(Tinebase_Model_Tree_Node $node)
if ($node->type !== Tinebase_Model_Tree_FileObject::TYPE_FILE
|| empty($node->hash)
|| $node->size == 0
|| $node->preview_status > 0
|| Tinebase_Config::getInstance()->{Tinebase_Config::FILESYSTEM}->
{Tinebase_Config::FILESYSTEM_PREVIEW_MAX_FILE_SIZE} < $node->size
|| Tinebase_Config::getInstance()->{Tinebase_Config::FILESYSTEM}->
Expand Down
17 changes: 14 additions & 3 deletions tine20/Tinebase/Frontend/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -1903,17 +1903,28 @@ public function fileSystemCheckIndexing()

/**
* checks if there are files missing previews and creates them synchronously
* that means this can be very time consuming
* that means this can be very time-consuming
* also deletes previews of files that no longer exist
*
* accepts (node) ids=XXXX,YYYY as param
*
* @param Zend_Console_Getopt $opts
* @return int
* @throws Zend_Db_Statement_Exception
* @throws Tinebase_Exception_InvalidArgument
*/
public function fileSystemCheckPreviews()
public function fileSystemCheckPreviews(Zend_Console_Getopt $opts): int
{
$this->_checkAdminRight();

$data = $this->_parseArgs($opts);
$ids = [];
if (isset($data['ids'])) {
$ids = explode(',', $data['ids']);
}

Tinebase_FileSystem_Previews::getInstance()->resetErrorCount();
Tinebase_FileSystem::getInstance()->sanitizePreviews();
Tinebase_FileSystem::getInstance()->sanitizePreviews($ids);

return 0;
}
Expand Down

0 comments on commit 46ebc16

Please sign in to comment.