-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathTextFile.php
76 lines (58 loc) · 1.64 KB
/
TextFile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Text;
use OCA\Text\Service\EncodingService;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
/**
* Wrapper around a ISimpleFile object to ensure that it is correctly encoded (UTF-8) for the text app.
*/
class TextFile implements ISimpleFile {
private ISimpleFile $file;
private EncodingService $encodingService;
public function __construct(ISimpleFile $file, EncodingService $encodingService) {
$this->file = $file;
$this->encodingService = $encodingService;
}
public function getName(): string {
return $this->file->getName();
}
public function getSize(): float|int {
return $this->file->getSize();
}
public function getETag(): string {
return $this->file->getETag();
}
public function getMTime(): int {
return $this->file->getMTime();
}
public function getContent(): string {
$content = $this->encodingService->encodeToUtf8($this->file->getContent());
if ($content === null) {
throw new NotFoundException('File not compatible with text because it could not be encoded to UTF-8.');
}
return $content;
}
public function putContent($data): void {
$this->file->putContent($data);
}
public function delete(): void {
$this->file->delete();
}
public function getMimeType(): string {
return 'text/plain;encoding=utf-8';
}
public function getExtension(): string {
return $this->file->getExtension();
}
public function read() {
return $this->file->read();
}
public function write() {
return $this->file->write();
}
}