1
0

TempSpaceAvailable.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\SetupChecks;
  8. use OCP\IConfig;
  9. use OCP\IL10N;
  10. use OCP\ITempManager;
  11. use OCP\IURLGenerator;
  12. use OCP\SetupCheck\ISetupCheck;
  13. use OCP\SetupCheck\SetupResult;
  14. class TempSpaceAvailable implements ISetupCheck {
  15. public function __construct(
  16. private IL10N $l10n,
  17. private IConfig $config,
  18. private IURLGenerator $urlGenerator,
  19. private ITempManager $tempManager,
  20. ) {
  21. }
  22. public function getName(): string {
  23. return $this->l10n->t('Temporary space available');
  24. }
  25. public function getCategory(): string {
  26. return 'system';
  27. }
  28. private function isPrimaryStorageS3(): bool {
  29. $objectStore = $this->config->getSystemValue('objectstore', null);
  30. $objectStoreMultibucket = $this->config->getSystemValue('objectstore_multibucket', null);
  31. if (!isset($objectStoreMultibucket) && !isset($objectStore)) {
  32. return false;
  33. }
  34. if (isset($objectStoreMultibucket['class']) && $objectStoreMultibucket['class'] !== 'OC\\Files\\ObjectStore\\S3') {
  35. return false;
  36. }
  37. if (isset($objectStore['class']) && $objectStore['class'] !== 'OC\\Files\\ObjectStore\\S3') {
  38. return false;
  39. }
  40. return true;
  41. }
  42. public function run(): SetupResult {
  43. $phpTempPath = sys_get_temp_dir();
  44. $nextcloudTempPath = '';
  45. try {
  46. $nextcloudTempPath = $this->tempManager->getTempBaseDir();
  47. } catch (\Exception $e) {
  48. }
  49. if (empty($nextcloudTempPath)) {
  50. return SetupResult::error('The temporary directory of this instance points to an either non-existing or non-writable directory.');
  51. }
  52. if (!is_dir($phpTempPath)) {
  53. return SetupResult::error($this->l10n->t('Error while checking the temporary PHP path - it was not properly set to a directory. Returned value: %s', [$phpTempPath]));
  54. }
  55. if (!function_exists('disk_free_space')) {
  56. return SetupResult::info($this->l10n->t('The PHP function "disk_free_space" is disabled, which prevents the check for enough space in the temporary directories.'));
  57. }
  58. $freeSpaceInTemp = disk_free_space($phpTempPath);
  59. if ($freeSpaceInTemp === false) {
  60. return SetupResult::error($this->l10n->t('Error while checking the available disk space of temporary PHP path or no free disk space returned. Temporary path: %s', [$phpTempPath]));
  61. }
  62. /** Build details data about temporary directory, either one or two of them */
  63. $freeSpaceInTempInGB = $freeSpaceInTemp / 1024 / 1024 / 1024;
  64. $spaceDetail = $this->l10n->t('- %.1f GiB available in %s (PHP temporary directory)', [round($freeSpaceInTempInGB, 1),$phpTempPath]);
  65. if ($nextcloudTempPath !== $phpTempPath) {
  66. $freeSpaceInNextcloudTemp = disk_free_space($nextcloudTempPath);
  67. if ($freeSpaceInNextcloudTemp === false) {
  68. return SetupResult::error($this->l10n->t('Error while checking the available disk space of temporary PHP path or no free disk space returned. Temporary path: %s', [$nextcloudTempPath]));
  69. }
  70. $freeSpaceInNextcloudTempInGB = $freeSpaceInNextcloudTemp / 1024 / 1024 / 1024;
  71. $spaceDetail .= "\n" . $this->l10n->t('- %.1f GiB available in %s (Nextcloud temporary directory)', [round($freeSpaceInNextcloudTempInGB, 1),$nextcloudTempPath]);
  72. }
  73. if (!$this->isPrimaryStorageS3()) {
  74. return SetupResult::success(
  75. $this->l10n->t("Temporary directory is correctly configured:\n%s", [$spaceDetail])
  76. );
  77. }
  78. if ($freeSpaceInTempInGB > 50) {
  79. return SetupResult::success(
  80. $this->l10n->t(
  81. "This instance uses an S3 based object store as primary storage, and has enough space in the temporary directory.\n%s",
  82. [$spaceDetail]
  83. )
  84. );
  85. }
  86. return SetupResult::warning(
  87. $this->l10n->t(
  88. "This instance uses an S3 based object store as primary storage. The uploaded files are stored temporarily on the server and thus it is recommended to have 50 GiB of free space available in the temp directory of PHP. To improve this please change the temporary directory in the php.ini or make more space available in that path. \nChecking the available space in the temporary path resulted in %.1f GiB instead of the recommended 50 GiB. Path: %s",
  89. [round($freeSpaceInTempInGB, 1),$phpTempPath]
  90. )
  91. );
  92. }
  93. }