-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathFileContentProvider.php
More file actions
146 lines (119 loc) · 3.97 KB
/
FileContentProvider.php
File metadata and controls
146 lines (119 loc) · 3.97 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Service\File;
use OCA\Libresign\Exception\LibresignException;
use OCP\Files\IRootFolder;
use OCP\Http\Client\IClientService;
use OCP\IL10N;
use Psr\Log\LoggerInterface;
class FileContentProvider {
public function __construct(
private IClientService $client,
private MimeService $mimeService,
private IRootFolder $root,
private LoggerInterface $logger,
private IL10N $l10n,
) {
}
/**
* Get file content from a URL
*
* @throws \Exception if URL is invalid or content cannot be retrieved
*/
public function getContentFromUrl(string $url): string {
if (!filter_var($url, FILTER_VALIDATE_URL)) {
throw new LibresignException($this->l10n->t('Invalid URL file'), 422);
}
try {
$response = $this->client->newClient()->get($url);
} catch (\Throwable) {
throw new LibresignException($this->l10n->t('Invalid URL file'), 422);
}
$content = (string)$response->getBody();
if (!$content) {
throw new LibresignException($this->l10n->t('Empty file'), 422);
}
$mimetypeFromHeader = $response->getHeader('Content-Type');
// Strip parameters like "; charset=utf-8"
if (str_contains($mimetypeFromHeader, ';')) {
$mimetypeFromHeader = trim(explode(';', $mimetypeFromHeader)[0]);
}
$mimeTypeFromContent = $this->mimeService->getMimeType($content);
// application/octet-stream is a generic fallback — trust content detection
if ($mimetypeFromHeader !== 'application/octet-stream' && $mimetypeFromHeader !== $mimeTypeFromContent) {
throw new LibresignException($this->l10n->t('Invalid URL file'), 422);
}
return $content;
}
/**
* Decode base64 content and validate MIME type
*
* @throws \Exception if MIME types don't match
*/
public function getContentFromBase64(string $base64): string {
$withMime = explode(',', $base64);
if (count($withMime) === 2) {
$withMime[0] = explode(';', $withMime[0]);
$withMime[0][0] = explode(':', $withMime[0][0]);
$mimeTypeFromType = $withMime[0][0][1];
$base64 = $withMime[1];
$content = base64_decode($base64);
$mimeTypeFromContent = $this->mimeService->getMimeType($content);
if ($mimeTypeFromType !== $mimeTypeFromContent) {
throw new LibresignException($this->l10n->t('Invalid URL file'), 422);
}
$this->mimeService->setMimeType($mimeTypeFromContent);
} else {
$content = base64_decode($base64);
$this->mimeService->getMimeType($content);
}
return $content;
}
/**
* Get raw file content from URL or base64 data array
*
* @param array $data Data array containing 'file' with 'url' or 'base64'
* @return string File content
* @throws \Exception if data is invalid
*/
public function getContentFromData(array $data): string {
if (!empty($data['file']['url'])) {
return $this->getContentFromUrl($data['file']['url']);
}
if (!empty($data['file']['base64'])) {
return $this->getContentFromBase64($data['file']['base64']);
}
throw new LibresignException($this->l10n->t('No file source provided'), 422);
}
/**
* Get file content from a LibreSign File entity
*
* @param \OCA\Libresign\Db\File $file
* @throws LibresignException
*/
public function getContentFromLibresignFile(\OCA\Libresign\Db\File $file): string {
try {
$nodeId = $file->getSignedNodeId();
if (!$nodeId) {
$nodeId = $file->getNodeId();
}
$fileNode = $this->root->getUserFolder($file->getUserId())->getFirstNodeById($nodeId);
if (!$fileNode instanceof \OCP\Files\File) {
throw new LibresignException($this->l10n->t('File not found'), 404);
}
return $fileNode->getContent();
} catch (LibresignException $e) {
throw $e;
} catch (\Throwable $e) {
$this->logger->error('Failed to get file content: ' . $e->getMessage(), [
'fileId' => $file->getId(),
'exception' => $e,
]);
throw new LibresignException($this->l10n->t('Invalid data to validate file'), 404, $e);
}
}
}