1
0

CleanupCardDAVPhotoCache.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Repair\NC16;
  8. use OCP\Files\IAppData;
  9. use OCP\Files\NotFoundException;
  10. use OCP\Files\SimpleFS\ISimpleFolder;
  11. use OCP\IConfig;
  12. use OCP\Migration\IOutput;
  13. use OCP\Migration\IRepairStep;
  14. use Psr\Log\LoggerInterface;
  15. use RuntimeException;
  16. /**
  17. * Class CleanupCardDAVPhotoCache
  18. *
  19. * This repair step removes "photo." files created by photocache
  20. *
  21. * Before https://github.com/nextcloud/server/pull/13843 a "photo." file could be created
  22. * for unsupported image formats by photocache. Because a file is present but not jpg, png or gif no
  23. * photo could be returned for this vcard. These invalid files are removed by this migration step.
  24. */
  25. class CleanupCardDAVPhotoCache implements IRepairStep {
  26. /** @var IConfig */
  27. private $config;
  28. /** @var IAppData */
  29. private $appData;
  30. private LoggerInterface $logger;
  31. public function __construct(IConfig $config, IAppData $appData, LoggerInterface $logger) {
  32. $this->config = $config;
  33. $this->appData = $appData;
  34. $this->logger = $logger;
  35. }
  36. public function getName(): string {
  37. return 'Cleanup invalid photocache files for carddav';
  38. }
  39. private function repair(IOutput $output): void {
  40. try {
  41. $folders = $this->appData->getDirectoryListing();
  42. } catch (NotFoundException $e) {
  43. return;
  44. } catch (RuntimeException $e) {
  45. $this->logger->error('Failed to fetch directory listing in CleanupCardDAVPhotoCache', ['exception' => $e]);
  46. return;
  47. }
  48. $folders = array_filter($folders, function (ISimpleFolder $folder) {
  49. return $folder->fileExists('photo.');
  50. });
  51. if (empty($folders)) {
  52. return;
  53. }
  54. $output->info('Delete ' . count($folders) . ' "photo." files');
  55. foreach ($folders as $folder) {
  56. try {
  57. /** @var ISimpleFolder $folder */
  58. $folder->getFile('photo.')->delete();
  59. } catch (\Exception $e) {
  60. $this->logger->error($e->getMessage(), ['exception' => $e]);
  61. $output->warning('Could not delete file "dav-photocache/' . $folder->getName() . '/photo."');
  62. }
  63. }
  64. }
  65. private function shouldRun(): bool {
  66. return version_compare(
  67. $this->config->getSystemValueString('version', '0.0.0.0'),
  68. '16.0.0.0',
  69. '<='
  70. );
  71. }
  72. public function run(IOutput $output): void {
  73. if ($this->shouldRun()) {
  74. $this->repair($output);
  75. }
  76. }
  77. }