|
25 | 25 | use OCA\LogReader\Log\SearchFilter; |
26 | 26 | use OCP\AppFramework\Controller; |
27 | 27 | use OCP\AppFramework\Http\JSONResponse; |
28 | | -use OCP\AppFramework\Http\TemplateResponse; |
29 | 28 | use OCP\IConfig; |
30 | 29 | use OCP\IRequest; |
31 | 30 |
|
@@ -53,11 +52,33 @@ public function __construct($appName, |
53 | 52 | * @param int $count |
54 | 53 | * @param int $offset |
55 | 54 | * @param string $levels |
56 | | - * @return TemplateResponse |
| 55 | + * @return JSONResponse |
57 | 56 | */ |
58 | | - public function get($count = 50, $offset = 0, $levels = '11111') { |
59 | | - $iterator = $this->logIteratorFactory->getLogIterator($levels); |
60 | | - return $this->responseFromIterator($iterator, $count, $offset); |
| 57 | + public function get($count = 50, $offset = 0, $levels = '11111'): JSONResponse { |
| 58 | + $logType = $this->config->getSystemValue('log_type', 'file'); |
| 59 | + if ($logType === 'file') { // we only support web access when `log_type` is set to `file` (the default) |
| 60 | + $iterator = $this->logIteratorFactory->getLogIterator($levels); |
| 61 | + return $this->responseFromIterator($iterator, $count, $offset); |
| 62 | + } else { // A log_type other than `file` seems to be configured so: |
| 63 | + // * Generate a dummy entry so we don't error out |
| 64 | + // * Use the dummy entry to inform the admin to look elsewhere and/or correct their configuration |
| 65 | + $dummyLine["id"] = uniqid(); |
| 66 | + $dummyLine["reqid"] = "00000000000000000000"; // irrelevant |
| 67 | + $dummyLine["level"] = 1; // INFO |
| 68 | + $dummyLine["time"] = date(DATE_ATOM, time()); |
| 69 | + $dummyLine["remoteAddr"] = "0.0.0.0"; |
| 70 | + $dummyLine["user"] = "---"; |
| 71 | + $dummyLine["app"] = "Logreader"; |
| 72 | + $dummyLine["method"] = "---"; |
| 73 | + $dummyLine["url"] = "---"; |
| 74 | + $dummyLine["message"] = |
| 75 | + 'File-based logging must be enabled to access logs from the Web UI. Your `log_type` is currently ' |
| 76 | + . 'set to: [' . $logType . ']. If you feel this is an error, please verify `log_type` in your ' |
| 77 | + . 'config.php and check the Nextcloud Administration Manual. This is not an actual log entry.'; |
| 78 | + $dummyLine["userAgent"] = "---"; |
| 79 | + $dummyLine["version"] = "---"; |
| 80 | + return new JSONResponse(['data' => $dummyLine, 'remain' => false]); |
| 81 | + } |
61 | 82 | } |
62 | 83 |
|
63 | 84 |
|
@@ -91,6 +112,12 @@ public function poll(string $lastReqId, string $levels = '11111'): JSONResponse |
91 | 112 | $cycles = 0; |
92 | 113 | $maxCycles = 20; |
93 | 114 |
|
| 115 | + $logType = $this->config->getSystemValue('log_type', 'file'); |
| 116 | + if ($logType !== 'file') { // we only support access when `log_type` is set to `file` (the default) |
| 117 | + // TODO: Don't even attempt polling in the front-end |
| 118 | + sleep(20); |
| 119 | + return new JSONResponse([]); |
| 120 | + } |
94 | 121 | $lastItem = $this->getLastItem($levels); |
95 | 122 | while ($lastItem === null || $lastItem['reqId'] === $lastReqId) { |
96 | 123 | sleep(1); |
@@ -128,11 +155,11 @@ public function poll(string $lastReqId, string $levels = '11111'): JSONResponse |
128 | 155 | * @param int $count |
129 | 156 | * @param int $offset |
130 | 157 | * @param string $levels |
131 | | - * @return TemplateResponse |
| 158 | + * @return JSONResponse |
132 | 159 | * |
133 | 160 | * @NoCSRFRequired |
134 | 161 | */ |
135 | | - public function search($query = '', $count = 50, $offset = 0, $levels = '11111') { |
| 162 | + public function search($query = '', $count = 50, $offset = 0, $levels = '11111'): JSONResponse { |
136 | 163 | $iterator = $this->logIteratorFactory->getLogIterator($levels); |
137 | 164 | $iterator = new \LimitIterator($iterator, 0, 100000); // limit the number of message we search to avoid huge search times |
138 | 165 | $iterator->rewind(); |
@@ -193,7 +220,7 @@ public function setLevels(string $levels): int { |
193 | 220 | return $minLevel; |
194 | 221 | } |
195 | 222 |
|
196 | | - protected function responseFromIterator(\Iterator $iterator, $count, $offset) { |
| 223 | + protected function responseFromIterator(\Iterator $iterator, $count, $offset): JSONResponse { |
197 | 224 | $iterator->rewind(); |
198 | 225 | for ($i = 0; $i < $offset; $i++) { |
199 | 226 | $iterator->next(); |
|
0 commit comments