LogSettingsController.php 1.3 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\StreamResponse;
  12. use OCP\IRequest;
  13. class LogSettingsController extends Controller {
  14. /** @var Log */
  15. private $log;
  16. public function __construct(string $appName, IRequest $request, Log $logger) {
  17. parent::__construct($appName, $request);
  18. $this->log = $logger;
  19. }
  20. /**
  21. * download logfile
  22. *
  23. * @NoCSRFRequired
  24. *
  25. * @psalm-suppress MoreSpecificReturnType The value of Content-Disposition is not relevant
  26. * @psalm-suppress LessSpecificReturnStatement The value of Content-Disposition is not relevant
  27. * @return StreamResponse<Http::STATUS_OK, array{Content-Type: 'application/octet-stream', 'Content-Disposition': string}>
  28. *
  29. * 200: Logfile returned
  30. */
  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. }