Skip to content

Commit

Permalink
Processed files for deleted sys_file record should be deleted as well #5
Browse files Browse the repository at this point in the history
 (#11)

Co-authored-by: Mathis Koblin <[email protected]>
  • Loading branch information
morja and Mathis Koblin authored Jul 29, 2024
1 parent 307a4cb commit 3eb0346
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
52 changes: 48 additions & 4 deletions Classes/Command/UnduplicateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Expand Down Expand Up @@ -122,7 +123,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$this->dryRun = $input->getOption('dry-run');
$onlyThisIdentifier = $input->getOption('identifier');
$onlyThisStorage = (int) $input->getOption('storage') ;
$onlyThisStorage = (int)$input->getOption('storage');

$queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_file');
$queryBuilder->count('*')
Expand Down Expand Up @@ -156,7 +157,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->output->warning('Found empty identifier');
continue;
}
$storage = (int) $row['storage'];
$storage = (int)$row['storage'];

$files = $this->findDuplicateFilesForIdentifier($identifier, $storage);
$originalUid = null;
Expand Down Expand Up @@ -185,7 +186,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
));
if (!$this->dryRun) {
$this->findAndUpdateReferences($originalUid, $oldFileUid);
$this->deleteOldFileRecord($fileRow['uid']);
$this->deleteOldFileRecord($oldFileUid);
$this->findAndDeleteOldProcessedFile($oldFileUid);
}
}
}
Expand Down Expand Up @@ -336,7 +338,7 @@ private function deleteReferencedRecord(array $referenceRow)
$recordDeleteQueryBuilder->createNamedParameter($referenceRow['recuid'], PDO::PARAM_INT)
)
)
->executeStatement();
->executeStatement();
}

private function deleteReference(array $referenceRow)
Expand Down Expand Up @@ -393,4 +395,46 @@ private function deleteOldFileRecord(int $oldFileUid)
)
->executeStatement();
}

private function findAndDeleteOldProcessedFile(int $oldFileUid): void
{
$recordQueryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_file_processedfile');
$results = $recordQueryBuilder->select('identifier')
->from('sys_file_processedfile')
->where(
$recordQueryBuilder->expr()->eq(
'original',
$recordQueryBuilder->createNamedParameter($oldFileUid)
)
)
->executeQuery();
while ($record = $results->fetchAssociative()) {
// delete each file from file system
$this->output->writeln('Deleting processed file: ' . $record['identifier']);
$this->deleteProcessedFile($record['identifier']);
}
// delete all records in sys_file_processedfile
$recordQueryBuilder->delete('sys_file_processedfile')
->where(
$recordQueryBuilder->expr()->eq(
'original',
$recordQueryBuilder->createNamedParameter($oldFileUid, PDO::PARAM_INT)
)
)
->executeStatement();
}

private function deleteProcessedFile(mixed $identifier): void
{
$file = Environment::getPublicPath() . "/fileadmin" . $identifier;
if (file_exists($file)) {
unlink($file);
// delete all empty parent folders
$dir = dirname($file);
while ($dir !== Environment::getPublicPath() . "/fileadmin" && count(scandir($dir)) === 2) {
rmdir($dir);
$dir = dirname($dir);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"sys_file",,,
,"uid","identifier","storage"
,1,"/test/abc.jpg",1
,2,"/test/abc.jpg",1
"sys_file_processedfile",,,,,,,
,"uid","original","storage","identifier"
,"991","1","1","/_processed_/3/c/csm_myfile_975bcb8fba.jpg"
,"992","2","1","/_processed_/3/c/csm_myfile_23231sdi99.jpg"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"sys_file",,,
,"uid","identifier","storage"
,2,"/test/abc.jpg",1
"sys_file_processedfile",,,,,,,
,"uid","original","storage","identifier"
,"992","2","1","/_processed_/3/c/csm_myfile_23231sdi99.jpg"
19 changes: 19 additions & 0 deletions Tests/Functional/Command/UnduplicateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ class UnduplicateCommandTest extends FunctionalTestCase
self::assertEquals(0, $result['status']);
}

/**
* Provide a processed file for the test run, so that it can be deleted
* @var array<string, string>
*/
protected array $pathsToProvideInTestInstance = [
'typo3/sysext/frontend/Resources/Public/Icons/Extension.svg' => 'fileadmin/_processed_/3/c/csm_myfile_975bcb8fba.jpg',
];

#[Test] public function unduplicateCommandFixesDuplicatesWithProcessedFiles()
{
$this->importCSVDataSet(__DIR__ . '/DataSet/sys_file_duplicates_with_processed_files.csv');

$result = $this->executeConsoleCommand('unduplicate:sysfile');

// the processed files are updated, so that the newer sys_file entry (uid=2) is used
$this->assertCSVDataSet(__DIR__ . '/DataSet/sys_file_duplicates_with_processed_files_RESULT.csv');
self::assertEquals(0, $result['status']);
}


/**
* based on TYPO3\CMS\Core\Tests\Functional\Command\AbstractCommandTest::executeConsoleCommand
Expand Down

0 comments on commit 3eb0346

Please sign in to comment.