Skip to content

Commit 40a96fc

Browse files
Raudiusjuliusknorr
authored andcommitted
Encode file to UTF-8 on fetch call
Signed-off-by: Raul <raul@nextcloud.com>
1 parent 4228c40 commit 40a96fc

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

lib/Service/ApiService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use OC\Files\Node\File;
3131
use OCA\Text\DocumentHasUnsavedChangesException;
3232
use OCA\Text\DocumentSaveConflictException;
33+
use OCA\Text\TextFile;
3334
use OCA\Text\VersionMismatchException;
3435
use OCP\AppFramework\Db\DoesNotExistException;
3536
use OCP\AppFramework\Http;
@@ -111,7 +112,7 @@ public function fetch($documentId, $sessionId, $sessionToken) {
111112
if ($this->sessionService->isValidSession($documentId, $sessionId, $sessionToken)) {
112113
$this->sessionService->removeInactiveSessions($documentId);
113114
try {
114-
$file = $this->documentService->getBaseFile($documentId);
115+
$file = new TextFile($this->documentService->getBaseFile($documentId), $this->encodingService);
115116
} catch (NotFoundException $e) {
116117
return new NotFoundResponse();
117118
}

lib/TextFile.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)