Passed
Push — shared-storage-experiments ( 2addb2...654060 )
by Matias
17:34
created

FileService::isUserFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
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\Node;
31
use OCP\ITempManager;
32
33
use OCP\Files\IHomeStorage;
34
use OCP\Files\NotFoundException;
35
36
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...
37
38
class FileService {
39
40
	/**  @var string|null */
41
	private $userId;
42
43
	/** @var IRootFolder */
44
	private $rootFolder;
45
46
	/** @var ITempManager */
47
	private $tempManager;
48
49
	public function __construct($userId,
50
	                            IRootFolder  $rootFolder,
51
	                            ITempManager $tempManager)
52
	{
53
		$this->userId      = $userId;
54
		$this->rootFolder  = $rootFolder;
55
		$this->tempManager = $tempManager;
56
	}
57
58
	/**
59
	 * TODO: Describe exactly when necessary.
60
	 */
61
	public function setupFS(string $userId) {
62
		\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...
63
		\OC_Util::setupFS($userId);
64
65
		$this->userId = $userId;
66
	}
67
68
	/**
69
	 * @return Node
70
	 * @throws NotFoundException
71
	 */
72
	public function getFileById($fileId, $userId = null): Node {
73
		$files = $this->rootFolder->getUserFolder($this->userId ?? $userId)->getById($fileId);
74
		if (count($files) === 0) {
75
			throw new NotFoundException();
76
		}
77
78
		return $files[0];
79
	}
80
81
	/**
82
	 * Checks if this file is located somewhere under .nomedia file and should be therefore ignored.
83
	 * Or with an .facerecognition.json setting file that disable tha analysis
84
	 *
85
	 * @param File $file File to search for
86
	 * @return bool True if file is located under .nomedia or .facerecognition.json that disabled
87
	 * analysis, false otherwise
88
	 */
89
	public function isUnderNoDetection(Node $node): bool {
90
		// If we detect .nomedia file anywhere on the path to root folder (id===null), bail out
91
		$parentNode = $node->getParent();
92
		while (($parentNode instanceof Folder) && ($parentNode->getId() !== null)) {
0 ignored issues
show
Bug introduced by
The type OCA\FaceRecognition\Service\Folder 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...
93
			if ($parentNode->nodeExists('.nomedia')) {
94
				return true;
95
			}
96
			if ($parentNode->nodeExists('.facerecognition.json')) {
97
				$file = $this->folder->get('.facerecognition.json');
0 ignored issues
show
Bug Best Practice introduced by
The property folder does not exist on OCA\FaceRecognition\Service\FileService. Did you maybe forget to declare it?
Loading history...
98
				$localPath = $this->getLocalFile($file);
99
100
				$settings = json_decode(file_get_contents($localPath));
101
102
				if ($settings['detection'] === 'off')
103
					return true;
104
			}
105
			$parentNode = $parentNode->getParent();
106
		}
107
		return false;
108
	}
109
110
	/**
111
	 * Returns if the file is inside a shared storage.
112
	 */
113
	public function isSharedFile(Node $node): bool {
114
		return $node->getStorage()->instanceOfStorage(SharingExternalStorage::class);
115
	}
116
117
	/**
118
	 * Returns if the file is inside HomeStorage.
119
	 */
120 16
	public function isUserFile(Node $node): bool {
121 16
		return $node->getStorage()->instanceOfStorage(IHomeStorage::class);
122
	}
123
124
	/**
125
	 * Get a path to either the local file or temporary file
126
	 *
127
	 * @param File $file
128
	 * @param int $maxSize maximum size for temporary files
129
	 * @return string
130
	 */
131
	public function getLocalFile(File $file, int $maxSize = null): string {
132
		$useTempFile = $file->isEncrypted() || !$file->getStorage()->isLocal();
133
		if ($useTempFile) {
134
			$absPath = $this->tempManager->getTemporaryFile();
135
136
			$content = $file->fopen('r');
137
			if ($maxSize !== null) {
138
				$content = stream_get_contents($content, $maxSize);
139
			}
140
			file_put_contents($absPath, $content);
141
142
			return $absPath;
143
		} else {
144
			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...
145
		}
146
	}
147
148
	/**
149
	 * Create a temporary file and return the path
150
	 */
151
	public function getTemporaryFile(string $postFix = ''): string {
152
		return $this->tempManager->getTemporaryFile($postFix);
153
	}
154
155
	/**
156
	 * Remove any temporary file from the service.
157
	 */
158
	public function clean() {
159
		$this->tempManager->clean();
160
	}
161
162
}
163