|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @author Robin Appelman <[email protected]> |
| 4 | + * |
| 5 | + * @copyright Copyright (c) 2015, ownCloud, Inc. |
| 6 | + * @license AGPL-3.0 |
| 7 | + * |
| 8 | + * This code is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Affero General Public License, version 3, |
| 10 | + * as published by the Free Software Foundation. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Affero General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License, version 3, |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/> |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +namespace OCA\LogReader\Controller; |
| 23 | + |
| 24 | +use OCA\LogReader\Log\LogIterator; |
| 25 | +use OCA\LogReader\Log\SearchFilter; |
| 26 | +use OCP\AppFramework\Controller; |
| 27 | +use OCP\AppFramework\Http\JSONResponse; |
| 28 | +use OCP\AppFramework\Http\TemplateResponse; |
| 29 | + |
| 30 | +/** |
| 31 | + * Class LogController |
| 32 | + * |
| 33 | + * @package OCA\LogReader\Controller |
| 34 | + */ |
| 35 | +class LogController extends Controller { |
| 36 | + /** |
| 37 | + * @param int $count |
| 38 | + * @param int $offset |
| 39 | + * @return TemplateResponse |
| 40 | + */ |
| 41 | + public function get($count = 50, $offset = 0) { |
| 42 | + $iterator = new LogIterator(fopen(\OC_Log_Owncloud::getLogFilePath(), 'rb')); |
| 43 | + return $this->responseFromIterator($iterator, $count, $offset); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @param string $query |
| 48 | + * @param int $count |
| 49 | + * @param int $offset |
| 50 | + * @return TemplateResponse |
| 51 | + */ |
| 52 | + public function search($query = '', $count = 50, $offset = 0) { |
| 53 | + $iterator = new LogIterator(fopen(\OC_Log_Owncloud::getLogFilePath(), 'rb')); |
| 54 | + $iterator = new SearchFilter($iterator, $query); |
| 55 | + return $this->responseFromIterator($iterator, $count, $offset); |
| 56 | + } |
| 57 | + |
| 58 | + protected function responseFromIterator(\Iterator $iterator, $count, $offset) { |
| 59 | + for ($i = 0; $i < $offset; $i++) { |
| 60 | + $iterator->next(); |
| 61 | + } |
| 62 | + $data = []; |
| 63 | + for ($i = 0; $i < $count; $i++) { |
| 64 | + $line = $iterator->current(); |
| 65 | + if (!is_null($line)) { |
| 66 | + $data[] = $line; |
| 67 | + } |
| 68 | + $iterator->next(); |
| 69 | + } |
| 70 | + return new JSONResponse([ |
| 71 | + 'data' => $data, |
| 72 | + 'remain' => $iterator->valid() |
| 73 | + ]); |
| 74 | + } |
| 75 | +} |
0 commit comments