Passed
Push — shared-storage-experiments ( 654060...ffc80d )
by Matias
07:58
created

FileService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 2
rs 10
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 OCP\Files\IRootFolder;
29
use OCP\Files\File;
30
use OCP\Files\Folder;
31
use OCP\Files\Node;
32
use OCP\ITempManager;
33
34
use OCP\Files\IHomeStorage;
35
use OCP\Files\NotFoundException;
36
37
use OCA\Files_Sharing\External\Storage as SharingExternalStorage;
0 ignored issues
show
Bug introduced by
The type OCA\Files_Sharing\External\Storage was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
38
39
class FileService {
40
41
	const NOMEDIA_FILE = ".nomedia";
42
43
	const FACERECOGNITION_SETTINGS_FILE = ".facerecognition.json";
44
45
	/**  @var string|null */
46
	private $userId;
47
48
	/** @var IRootFolder */
49
	private $rootFolder;
50
51
	/** @var ITempManager */
52
	private $tempManager;
53
54
	public function __construct($userId,
55
	                            IRootFolder  $rootFolder,
56
	                            ITempManager $tempManager)
57
	{
58
		$this->userId      = $userId;
59
		$this->rootFolder  = $rootFolder;
60
		$this->tempManager = $tempManager;
61
	}
62
63
	/**
64
	 * TODO: Describe exactly when necessary.
65
	 */
66
	public function setupFS(string $userId) {
67
		\OC_Util::tearDownFS();
0 ignored issues
show
Bug introduced by
The type OC_Util was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
68
		\OC_Util::setupFS($userId);
69
70
		$this->userId = $userId;
71
	}
72
73
	/**
74
	 * @return Node
75
	 * @throws NotFoundException
76
	 */
77
	public function getFileById($fileId, $userId = null): Node {
78
		$files = $this->rootFolder->getUserFolder($this->userId ?? $userId)->getById($fileId);
79
		if (count($files) === 0) {
80
			throw new NotFoundException();
81
		}
82
83
		return $files[0];
84
	}
85
86
	/**
87
	 * Checks if this file is located somewhere under .nomedia file and should be therefore ignored.
88
	 * Or with an .facerecognition.json setting file that disable tha analysis
89
	 *
90
	 * @param File $file File to search for
91
	 * @return bool True if file is located under .nomedia or .facerecognition.json that disabled
92
	 * analysis, false otherwise
93
	 */
94
	public function isUnderNoDetection(Node $node): bool {
95
		// If we detect .nomedia file anywhere on the path to root folder (id===null), bail out
96
		$parentNode = $node->getParent();
97
		while (($parentNode instanceof Folder) && ($parentNode->getId() !== null)) {
98
			$allowDetection = $this->allowsChildDetection($parentNode);
99
			if (!$allowDetection)
100
				return true;
101
			$parentNode = $parentNode->getParent();
102
		}
103
		return false;
104
	}
105
106
	/**
107
	 * Checks if this folder has .nomedia file an .facerecognition.json setting file that
108
	 * disable that analysis.
109
	 *
110
	 * @param Folder $folder Folder to search for
111
	 * @return bool true if folder dont have an .nomedia file or .facerecognition.json that disabled
112
	 * analysis, false otherwise
113
	 */
114
	public function allowsChildDetection(Folder $folder): bool {
115
		if ($folder->nodeExists(FileService::NOMEDIA_FILE)) {
116
			return false;
117
		}
118
		if ($folder->nodeExists(FileService::FACERECOGNITION_SETTINGS_FILE)) {
119
			$file = $folder->get(FileService::FACERECOGNITION_SETTINGS_FILE);
120
			$localPath = $this->getLocalFile($file);
121
122
			$settings = json_decode(file_get_contents($localPath));
123
			if ($settings === null || !array_key_exists('detection', $settings))
124
				return true;
125
126
			if ($settings['detection'] === 'off')
127
				return false;
128
		}
129
130
		return true;
131
	}
132
133
	/**
134
	 * Returns if the file is inside a shared storage.
135
	 */
136
	public function isSharedFile(Node $node): bool {
137
		return $node->getStorage()->instanceOfStorage(SharingExternalStorage::class);
138
	}
139
140
	/**
141
	 * Returns if the file is inside HomeStorage.
142
	 */
143 16
	public function isUserFile(Node $node): bool {
144 16
		return $node->getStorage()->instanceOfStorage(IHomeStorage::class);
145
	}
146
147
	/**
148
	 * Get a path to either the local file or temporary file
149
	 *
150
	 * @param File $file
151
	 * @param int $maxSize maximum size for temporary files
152
	 * @return string
153
	 */
154
	public function getLocalFile(File $file, int $maxSize = null): string {
155
		$useTempFile = $file->isEncrypted() || !$file->getStorage()->isLocal();
156
		if ($useTempFile) {
157
			$absPath = $this->tempManager->getTemporaryFile();
158
159
			$content = $file->fopen('r');
160
			if ($maxSize !== null) {
161
				$content = stream_get_contents($content, $maxSize);
162
			}
163
			file_put_contents($absPath, $content);
164
165
			return $absPath;
166
		} else {
167
			return $file->getStorage()->getLocalFile($file->getInternalPath());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $file->getStorage...ile->getInternalPath()) could return the type false which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
168
		}
169
	}
170
171
	/**
172
	 * Create a temporary file and return the path
173
	 */
174
	public function getTemporaryFile(string $postFix = ''): string {
175
		return $this->tempManager->getTemporaryFile($postFix);
176
	}
177
178
	/**
179
	 * Remove any temporary file from the service.
180
	 */
181
	public function clean() {
182
		$this->tempManager->clean();
183
	}
184
185
}
186