CleanupCardDAVPhotoCache.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Daniel Kesselberg (mail@danielkesselberg.de)
  5. *
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Repair\NC16;
  26. use OCP\Files\IAppData;
  27. use OCP\Files\NotFoundException;
  28. use OCP\Files\SimpleFS\ISimpleFolder;
  29. use OCP\IConfig;
  30. use OCP\Migration\IOutput;
  31. use OCP\Migration\IRepairStep;
  32. use Psr\Log\LoggerInterface;
  33. use RuntimeException;
  34. /**
  35. * Class CleanupCardDAVPhotoCache
  36. *
  37. * This repair step removes "photo." files created by photocache
  38. *
  39. * Before https://github.com/nextcloud/server/pull/13843 a "photo." file could be created
  40. * for unsupported image formats by photocache. Because a file is present but not jpg, png or gif no
  41. * photo could be returned for this vcard. These invalid files are removed by this migration step.
  42. */
  43. class CleanupCardDAVPhotoCache implements IRepairStep {
  44. /** @var IConfig */
  45. private $config;
  46. /** @var IAppData */
  47. private $appData;
  48. private LoggerInterface $logger;
  49. public function __construct(IConfig $config, IAppData $appData, LoggerInterface $logger) {
  50. $this->config = $config;
  51. $this->appData = $appData;
  52. $this->logger = $logger;
  53. }
  54. public function getName(): string {
  55. return 'Cleanup invalid photocache files for carddav';
  56. }
  57. private function repair(IOutput $output): void {
  58. try {
  59. $folders = $this->appData->getDirectoryListing();
  60. } catch (NotFoundException $e) {
  61. return;
  62. } catch (RuntimeException $e) {
  63. $this->logger->error('Failed to fetch directory listing in CleanupCardDAVPhotoCache', ['exception' => $e]);
  64. return;
  65. }
  66. $folders = array_filter($folders, function (ISimpleFolder $folder) {
  67. return $folder->fileExists('photo.');
  68. });
  69. if (empty($folders)) {
  70. return;
  71. }
  72. $output->info('Delete ' . count($folders) . ' "photo." files');
  73. foreach ($folders as $folder) {
  74. try {
  75. /** @var ISimpleFolder $folder */
  76. $folder->getFile('photo.')->delete();
  77. } catch (\Exception $e) {
  78. $this->logger->error($e->getMessage(), ['exception' => $e]);
  79. $output->warning('Could not delete file "dav-photocache/' . $folder->getName() . '/photo."');
  80. }
  81. }
  82. }
  83. private function shouldRun(): bool {
  84. return version_compare(
  85. $this->config->getSystemValueString('version', '0.0.0.0'),
  86. '16.0.0.0',
  87. '<='
  88. );
  89. }
  90. public function run(IOutput $output): void {
  91. if ($this->shouldRun()) {
  92. $this->repair($output);
  93. }
  94. }
  95. }