TempSpaceAvailable.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Côme Chilliet <come.chilliet@nextcloud.com>
  5. *
  6. * @author Côme Chilliet <come.chilliet@nextcloud.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Settings\SetupChecks;
  25. use OCP\IConfig;
  26. use OCP\IL10N;
  27. use OCP\ITempManager;
  28. use OCP\IURLGenerator;
  29. use OCP\SetupCheck\ISetupCheck;
  30. use OCP\SetupCheck\SetupResult;
  31. class TempSpaceAvailable implements ISetupCheck {
  32. public function __construct(
  33. private IL10N $l10n,
  34. private IConfig $config,
  35. private IURLGenerator $urlGenerator,
  36. private ITempManager $tempManager,
  37. ) {
  38. }
  39. public function getName(): string {
  40. return $this->l10n->t('Temporary space available');
  41. }
  42. public function getCategory(): string {
  43. return 'system';
  44. }
  45. private function isPrimaryStorageS3(): bool {
  46. $objectStore = $this->config->getSystemValue('objectstore', null);
  47. $objectStoreMultibucket = $this->config->getSystemValue('objectstore_multibucket', null);
  48. if (!isset($objectStoreMultibucket) && !isset($objectStore)) {
  49. return false;
  50. }
  51. if (isset($objectStoreMultibucket['class']) && $objectStoreMultibucket['class'] !== 'OC\\Files\\ObjectStore\\S3') {
  52. return false;
  53. }
  54. if (isset($objectStore['class']) && $objectStore['class'] !== 'OC\\Files\\ObjectStore\\S3') {
  55. return false;
  56. }
  57. return true;
  58. }
  59. public function run(): SetupResult {
  60. $phpTempPath = sys_get_temp_dir();
  61. $nextcloudTempPath = '';
  62. try {
  63. $nextcloudTempPath = $this->tempManager->getTempBaseDir();
  64. } catch (\Exception $e) {
  65. }
  66. if (empty($nextcloudTempPath)) {
  67. return SetupResult::error('The temporary directory of this instance points to an either non-existing or non-writable directory.');
  68. }
  69. if (!is_dir($phpTempPath)) {
  70. 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]));
  71. }
  72. $freeSpaceInTemp = function_exists('disk_free_space') ? disk_free_space($phpTempPath) : false;
  73. if ($freeSpaceInTemp === false) {
  74. 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]));
  75. }
  76. /** Build details data about temporary directory, either one or two of them */
  77. $freeSpaceInTempInGB = $freeSpaceInTemp / 1024 / 1024 / 1024;
  78. $spaceDetail = $this->l10n->t('- %.1f GiB available in %s (PHP temporary directory)', [round($freeSpaceInTempInGB, 1),$phpTempPath]);
  79. if ($nextcloudTempPath !== $phpTempPath) {
  80. $freeSpaceInNextcloudTemp = function_exists('disk_free_space') ? disk_free_space($nextcloudTempPath) : false;
  81. if ($freeSpaceInNextcloudTemp === false) {
  82. 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]));
  83. }
  84. $freeSpaceInNextcloudTempInGB = $freeSpaceInNextcloudTemp / 1024 / 1024 / 1024;
  85. $spaceDetail .= "\n".$this->l10n->t('- %.1f GiB available in %s (Nextcloud temporary directory)', [round($freeSpaceInNextcloudTempInGB, 1),$nextcloudTempPath]);
  86. }
  87. if (!$this->isPrimaryStorageS3()) {
  88. return SetupResult::success(
  89. $this->l10n->t("Temporary directory is correctly configured:\n%s", [$spaceDetail])
  90. );
  91. }
  92. if ($freeSpaceInTempInGB > 50) {
  93. return SetupResult::success(
  94. $this->l10n->t(
  95. "This instance uses an S3 based object store as primary storage, and has enough space in the temporary directory.\n%s",
  96. [$spaceDetail]
  97. )
  98. );
  99. }
  100. return SetupResult::warning(
  101. $this->l10n->t(
  102. "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",
  103. [round($freeSpaceInTempInGB, 1),$phpTempPath]
  104. )
  105. );
  106. }
  107. }