|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * @copyright Copyright (c) 2022 Raul Ferreira Fuentes <raul@nextcloud.com> |
| 7 | + * |
| 8 | + * @author Raul Ferreira Fuentes <raul@nextcloud.com> |
| 9 | + * |
| 10 | + * @license GNU AGPL version 3 or any later version |
| 11 | + * |
| 12 | + * This program is free software: you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU Affero General Public License as |
| 14 | + * published by the Free Software Foundation, either version 3 of the |
| 15 | + * License, or (at your option) any later version. |
| 16 | + * |
| 17 | + * This program is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU Affero General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU Affero General Public License |
| 23 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | + * |
| 25 | + */ |
| 26 | + |
| 27 | +namespace OCA\Text; |
| 28 | + |
| 29 | +use OCA\Text\Service\EncodingService; |
| 30 | +use OCP\Files\NotFoundException; |
| 31 | +use OCP\Files\SimpleFS\ISimpleFile; |
| 32 | + |
| 33 | +/** |
| 34 | + * Wrapper around a ISimpleFile object to ensure that it is correctly encoded (UTF-8) for the text app. |
| 35 | + */ |
| 36 | +class TextFile implements ISimpleFile { |
| 37 | + |
| 38 | + /** @var ISimpleFile */ |
| 39 | + private $file; |
| 40 | + /** @var EncodingService */ |
| 41 | + private $encodingService; |
| 42 | + |
| 43 | + public function __construct(ISimpleFile $file, EncodingService $encodingService) { |
| 44 | + $this->file = $file; |
| 45 | + $this->encodingService = $encodingService; |
| 46 | + } |
| 47 | + |
| 48 | + public function getName() { |
| 49 | + return $this->file->getName(); |
| 50 | + } |
| 51 | + |
| 52 | + public function getSize() { |
| 53 | + return $this->file->getSize(); |
| 54 | + } |
| 55 | + |
| 56 | + public function getETag() { |
| 57 | + return $this->file->getETag(); |
| 58 | + } |
| 59 | + |
| 60 | + public function getMTime() { |
| 61 | + return $this->file->getMTime(); |
| 62 | + } |
| 63 | + |
| 64 | + public function getContent() { |
| 65 | + $content = $this->encodingService->encodeToUtf8($this->file->getContent()); |
| 66 | + if (!$content) { |
| 67 | + throw new NotFoundException('File not compatible with text because it could not be encoded to UTF-8.'); |
| 68 | + } |
| 69 | + |
| 70 | + return $content; |
| 71 | + } |
| 72 | + |
| 73 | + public function putContent($data) { |
| 74 | + return $this->file->putContent($data); |
| 75 | + } |
| 76 | + |
| 77 | + public function delete() { |
| 78 | + $this->file->delete(); |
| 79 | + } |
| 80 | + |
| 81 | + public function getMimeType() { |
| 82 | + return 'text/plain;encoding=utf-8'; |
| 83 | + } |
| 84 | + |
| 85 | + public function read() { |
| 86 | + return $this->file->read(); |
| 87 | + } |
| 88 | + |
| 89 | + public function write() { |
| 90 | + return $this->file->write(); |
| 91 | + } |
| 92 | +} |
0 commit comments