Skip to content

Commit 69c3236

Browse files
committed
Add EncodingService
1 parent f0a3661 commit 69c3236

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

lib/Service/ApiService.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,20 @@ class ApiService {
5050
protected $documentService;
5151
protected $logger;
5252
private $imageService;
53+
private $encodingService;
5354

5455
public function __construct(IRequest $request,
5556
SessionService $sessionService,
5657
DocumentService $documentService,
5758
ImageService $imageService,
59+
EncodingService $encodingService,
5860
ILogger $logger) {
5961
$this->request = $request;
6062
$this->sessionService = $sessionService;
6163
$this->documentService = $documentService;
6264
$this->logger = $logger;
6365
$this->imageService = $imageService;
66+
$this->encodingService = $encodingService;
6467
}
6568

6669
public function create($fileId = null, $filePath = null, $token = null, $guestName = null, bool $forceRecreate = false): DataResponse {
@@ -105,6 +108,11 @@ public function create($fileId = null, $filePath = null, $token = null, $guestNa
105108
try {
106109
$baseFile = $this->documentService->getBaseFile($document->getId());
107110
$content = $baseFile->getContent();
111+
112+
$content = $this->encodingService->encodeToUtf8($content);
113+
if (!$content) {
114+
$this->logger->log(ILogger::WARN, 'Failed to encode file to UTF8. File ID: ' . $file->getId());
115+
}
108116
} catch (NotFoundException $e) {
109117
$this->logger->logException($e, ['level' => ILogger::INFO]);
110118
$content = null;

lib/Service/EncodingService.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2022 Raul Ferreira Fuentes <[email protected]>
7+
*
8+
* @author Raul Ferreira Fuentes <[email protected]>
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+
namespace OCA\Text\Service;
27+
28+
class EncodingService {
29+
const COMMON_ENCODINGS = ['UTF-8', 'WINDOWS-1252', 'ISO-8859-15', 'ISO-8859-1', 'ASCII'];
30+
31+
public function encodeToUtf8(string $string): ?string {
32+
$encoding = mb_detect_encoding($string, $this->getEncodings(), true);
33+
if (!$encoding) {
34+
return null;
35+
}
36+
37+
return mb_convert_encoding($string, 'UTF-8', $encoding);
38+
}
39+
40+
/**
41+
* @return string[]
42+
*/
43+
private function getEncodings(): array {
44+
$mb_order = mb_detect_order() ?: [];
45+
return array_merge($mb_order, self::COMMON_ENCODINGS);
46+
}
47+
}

0 commit comments

Comments
 (0)