|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* @copyright Copyright (c) 2019 Matias De lellis <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @author Matias De lellis <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* @license GNU AGPL version 3 or any later version |
|
10
|
|
|
* |
|
11
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
13
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
14
|
|
|
* License, or (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* This program is distributed in the hope that it will be useful, |
|
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19
|
|
|
* GNU Affero General Public License for more details. |
|
20
|
|
|
* |
|
21
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
23
|
|
|
* |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace OCA\FaceRecognition\Service; |
|
27
|
|
|
|
|
28
|
|
|
use OCA\FaceRecognition\Helper\Requirements; |
|
29
|
|
|
|
|
30
|
|
|
use OCP\IConfig; |
|
31
|
|
|
use OCP\Files\IRootFolder; |
|
32
|
|
|
use OCP\Files\File; |
|
33
|
|
|
use OCP\Files\Folder; |
|
34
|
|
|
use OCP\Files\Node; |
|
35
|
|
|
use OCP\ITempManager; |
|
36
|
|
|
|
|
37
|
|
|
use OCP\Files\IHomeStorage; |
|
38
|
|
|
use OCP\Files\NotFoundException; |
|
39
|
|
|
|
|
40
|
|
|
use OCA\Files_Sharing\External\Storage as SharingExternalStorage; |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
class FileService { |
|
43
|
|
|
|
|
44
|
|
|
const NOMEDIA_FILE = ".nomedia"; |
|
45
|
|
|
|
|
46
|
|
|
const FACERECOGNITION_SETTINGS_FILE = ".facerecognition.json"; |
|
47
|
|
|
|
|
48
|
|
|
/** @var string|null */ |
|
49
|
|
|
private $userId; |
|
50
|
|
|
|
|
51
|
|
|
/** @var IConfig Config */ |
|
52
|
|
|
private $config; |
|
53
|
|
|
|
|
54
|
|
|
/** @var IRootFolder */ |
|
55
|
|
|
private $rootFolder; |
|
56
|
|
|
|
|
57
|
|
|
/** @var ITempManager */ |
|
58
|
|
|
private $tempManager; |
|
59
|
|
|
|
|
60
|
|
|
public function __construct($userId, |
|
61
|
|
|
IConfig $config, |
|
62
|
|
|
IRootFolder $rootFolder, |
|
63
|
|
|
ITempManager $tempManager) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->userId = $userId; |
|
66
|
|
|
$this->config = $config; |
|
67
|
|
|
$this->rootFolder = $rootFolder; |
|
68
|
|
|
$this->tempManager = $tempManager; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* TODO: Describe exactly when necessary. |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setupFS(string $userId) { |
|
75
|
|
|
\OC_Util::tearDownFS(); |
|
|
|
|
|
|
76
|
|
|
\OC_Util::setupFS($userId); |
|
77
|
|
|
|
|
78
|
|
|
$this->userId = $userId; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return Node |
|
83
|
|
|
* @throws NotFoundException |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getFileById($fileId, $userId = null): Node { |
|
86
|
|
|
$files = $this->rootFolder->getUserFolder($this->userId ?? $userId)->getById($fileId); |
|
87
|
|
|
if (count($files) === 0) { |
|
88
|
|
|
throw new NotFoundException(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $files[0]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Checks if this file is located somewhere under .nomedia file and should be therefore ignored. |
|
96
|
|
|
* Or with an .facerecognition.json setting file that disable tha analysis |
|
97
|
|
|
* |
|
98
|
|
|
* @param File $file File to search for |
|
99
|
|
|
* @return bool True if file is located under .nomedia or .facerecognition.json that disabled |
|
100
|
|
|
* analysis, false otherwise |
|
101
|
|
|
*/ |
|
102
|
|
|
public function isUnderNoDetection(Node $node): bool { |
|
103
|
|
|
// If we detect .nomedia file anywhere on the path to root folder (id===null), bail out |
|
104
|
|
|
$parentNode = $node->getParent(); |
|
105
|
|
|
while (($parentNode instanceof Folder) && ($parentNode->getId() !== null)) { |
|
106
|
|
|
$allowDetection = $this->allowsChildDetection($parentNode); |
|
107
|
|
|
if (!$allowDetection) |
|
108
|
|
|
return true; |
|
109
|
|
|
$parentNode = $parentNode->getParent(); |
|
110
|
|
|
} |
|
111
|
|
|
return false; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Checks if this folder has .nomedia file an .facerecognition.json setting file that |
|
116
|
|
|
* disable that analysis. |
|
117
|
|
|
* |
|
118
|
|
|
* @param Folder $folder Folder to search for |
|
119
|
|
|
* @return bool true if folder dont have an .nomedia file or .facerecognition.json that disabled |
|
120
|
|
|
* analysis, false otherwise |
|
121
|
|
|
*/ |
|
122
|
|
|
public function allowsChildDetection(Folder $folder): bool { |
|
123
|
|
|
if ($folder->nodeExists(FileService::NOMEDIA_FILE)) { |
|
124
|
|
|
return false; |
|
125
|
|
|
} |
|
126
|
|
|
if ($folder->nodeExists(FileService::FACERECOGNITION_SETTINGS_FILE)) { |
|
127
|
|
|
$file = $folder->get(FileService::FACERECOGNITION_SETTINGS_FILE); |
|
128
|
|
|
$localPath = $this->getLocalFile($file); |
|
129
|
|
|
|
|
130
|
|
|
$settings = json_decode(file_get_contents($localPath)); |
|
131
|
|
|
if ($settings === null || !array_key_exists('detection', $settings)) |
|
132
|
|
|
return true; |
|
133
|
|
|
|
|
134
|
|
|
if ($settings['detection'] === 'off') |
|
135
|
|
|
return false; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return true; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Returns if the file is inside a shared storage. |
|
143
|
|
|
*/ |
|
144
|
|
|
public function isSharedFile(Node $node): bool { |
|
145
|
|
|
return $node->getStorage()->instanceOfStorage(SharingExternalStorage::class); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Returns if the file is inside HomeStorage. |
|
150
|
|
|
*/ |
|
151
|
16 |
|
public function isUserFile(Node $node): bool { |
|
152
|
16 |
|
return $node->getStorage()->instanceOfStorage(IHomeStorage::class); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Get a path to either the local file or temporary file |
|
157
|
|
|
* |
|
158
|
|
|
* @param File $file |
|
159
|
|
|
* @param int $maxSize maximum size for temporary files |
|
160
|
|
|
* @return string |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getLocalFile(File $file, int $maxSize = null): string { |
|
163
|
|
|
$useTempFile = $file->isEncrypted() || !$file->getStorage()->isLocal(); |
|
164
|
|
|
if ($useTempFile) { |
|
165
|
|
|
$absPath = $this->tempManager->getTemporaryFile(); |
|
166
|
|
|
|
|
167
|
|
|
$content = $file->fopen('r'); |
|
168
|
|
|
if ($maxSize !== null) { |
|
169
|
|
|
$content = stream_get_contents($content, $maxSize); |
|
170
|
|
|
} |
|
171
|
|
|
file_put_contents($absPath, $content); |
|
172
|
|
|
|
|
173
|
|
|
return $absPath; |
|
174
|
|
|
} else { |
|
175
|
|
|
return $file->getStorage()->getLocalFile($file->getInternalPath()); |
|
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Return all images from a given folder. |
|
181
|
|
|
* |
|
182
|
|
|
* TODO: It is inefficient since it copies the array recursively. |
|
183
|
|
|
* |
|
184
|
|
|
* @param Folder $folder Folder to get images from |
|
185
|
|
|
* @return array List of all images and folders to continue recursive crawling |
|
186
|
|
|
*/ |
|
187
|
|
|
public function getPicturesFromFolder(Folder $folder, $results = array()) { |
|
188
|
|
|
$handleSharedFiles = $this->config->getAppValue('facerecognition', 'handle-shared-files', 'false'); |
|
189
|
|
|
$nodes = $folder->getDirectoryListing(); |
|
190
|
|
|
foreach ($nodes as $node) { |
|
191
|
|
|
if (!$this->isUserFile($node) || ($this->isSharedFile($node) && $handleSharedFiles !== 'true')) { |
|
192
|
|
|
continue; |
|
193
|
|
|
} |
|
194
|
|
|
if ($node instanceof Folder && $this->allowsChildDetection($node)) { |
|
195
|
|
|
$results = $this->getPicturesFromFolder($node, $results); |
|
196
|
|
|
} |
|
197
|
|
|
else if ($node instanceof File) { |
|
198
|
|
|
if (Requirements::isImageTypeSupported($node->getMimeType())) { |
|
199
|
|
|
$results[] = $node; |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
return $results; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Create a temporary file and return the path |
|
208
|
|
|
*/ |
|
209
|
|
|
public function getTemporaryFile(string $postFix = ''): string { |
|
210
|
|
|
return $this->tempManager->getTemporaryFile($postFix); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Remove any temporary file from the service. |
|
215
|
|
|
*/ |
|
216
|
|
|
public function clean() { |
|
217
|
|
|
$this->tempManager->clean(); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
} |
|
221
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths