You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CleanupCardDAVPhotoCache.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\ILogger;
  31. use OCP\Migration\IOutput;
  32. use OCP\Migration\IRepairStep;
  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. /** @var ILogger */
  49. private $logger;
  50. public function __construct(IConfig $config, IAppData $appData, ILogger $logger) {
  51. $this->config = $config;
  52. $this->appData = $appData;
  53. $this->logger = $logger;
  54. }
  55. public function getName(): string {
  56. return 'Cleanup invalid photocache files for carddav';
  57. }
  58. private function repair(IOutput $output): void {
  59. try {
  60. $folders = $this->appData->getDirectoryListing();
  61. } catch (NotFoundException $e) {
  62. return;
  63. } catch (RuntimeException $e) {
  64. $this->logger->logException($e, ['message' => 'Failed to fetch directory listing in CleanupCardDAVPhotoCache']);
  65. return;
  66. }
  67. $folders = array_filter($folders, function (ISimpleFolder $folder) {
  68. return $folder->fileExists('photo.');
  69. });
  70. if (empty($folders)) {
  71. return;
  72. }
  73. $output->info('Delete ' . count($folders) . ' "photo." files');
  74. foreach ($folders as $folder) {
  75. try {
  76. /** @var ISimpleFolder $folder */
  77. $folder->getFile('photo.')->delete();
  78. } catch (\Exception $e) {
  79. $this->logger->logException($e);
  80. $output->warning('Could not delete file "dav-photocache/' . $folder->getName() . '/photo."');
  81. }
  82. }
  83. }
  84. private function shouldRun(): bool {
  85. return version_compare(
  86. $this->config->getSystemValue('version', '0.0.0.0'),
  87. '16.0.0.0',
  88. '<='
  89. );
  90. }
  91. public function run(IOutput $output): void {
  92. if ($this->shouldRun()) {
  93. $this->repair($output);
  94. }
  95. }
  96. }