LogSettingsController.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Settings\Controller;
  8. use OC\Log;
  9. use OCP\AppFramework\Controller;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
  12. use OCP\AppFramework\Http\StreamResponse;
  13. use OCP\IRequest;
  14. class LogSettingsController extends Controller {
  15. /** @var Log */
  16. private $log;
  17. public function __construct(string $appName, IRequest $request, Log $logger) {
  18. parent::__construct($appName, $request);
  19. $this->log = $logger;
  20. }
  21. /**
  22. * download logfile
  23. *
  24. * @psalm-suppress MoreSpecificReturnType The value of Content-Disposition is not relevant
  25. * @psalm-suppress LessSpecificReturnStatement The value of Content-Disposition is not relevant
  26. * @return StreamResponse<Http::STATUS_OK, array{Content-Type: 'application/octet-stream', 'Content-Disposition': string}>
  27. *
  28. * 200: Logfile returned
  29. */
  30. #[NoCSRFRequired]
  31. public function download() {
  32. if (!$this->log instanceof Log) {
  33. throw new \UnexpectedValueException('Log file not available');
  34. }
  35. $resp = new StreamResponse($this->log->getLogPath());
  36. $resp->setHeaders([
  37. 'Content-Type' => 'application/octet-stream',
  38. 'Content-Disposition' => 'attachment; filename="nextcloud.log"',
  39. ]);
  40. return $resp;
  41. }
  42. }