Passed
Push — shared-storage-experiments ( 606554...2addb2 )
by Matias
08:21
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\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
	 *
84
	 * @param File $file File to search for
85
	 * @return bool True if file is located under .nomedia, false otherwise
86
	 */
87
	public function isUnderNoMedia(Node $node): bool {
88
		// If we detect .nomedia file anywhere on the path to root folder (id===null), bail out
89
		$parentNode = $node->getParent();
90
		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...
91
			if ($parentNode->nodeExists('.nomedia')) {
92
				return true;
93
			}
94
			$parentNode = $parentNode->getParent();
95
		}
96
		return false;
97
	}
98
99
	/**
100
	 * Returns if the file is inside a shared storage.
101
	 */
102
	public function isSharedFile(Node $node): bool {
103
		return $node->getStorage()->instanceOfStorage(SharingExternalStorage::class);
104
	}
105
106
	/**
107
	 * Returns if the file is inside HomeStorage.
108
	 */
109 16
	public function isUserFile(Node $node): bool {
110 16
		return $node->getStorage()->instanceOfStorage(IHomeStorage::class);
111
	}
112
113
	/**
114
	 * Get a path to either the local file or temporary file
115
	 *
116
	 * @param File $file
117
	 * @param int $maxSize maximum size for temporary files
118
	 * @return string
119
	 */
120
	public function getLocalFile(File $file, int $maxSize = null): string {
121
		$useTempFile = $file->isEncrypted() || !$file->getStorage()->isLocal();
122
		if ($useTempFile) {
123
			$absPath = $this->tempManager->getTemporaryFile();
124
125
			$content = $file->fopen('r');
126
			if ($maxSize !== null) {
127
				$content = stream_get_contents($content, $maxSize);
128
			}
129
			file_put_contents($absPath, $content);
130
131
			return $absPath;
132
		} else {
133
			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...
134
		}
135
	}
136
137
	/**
138
	 * Create a temporary file and return the path
139
	 */
140
	public function getTemporaryFile(string $postFix = ''): string {
141
		return $this->tempManager->getTemporaryFile($postFix);
142
	}
143
144
	/**
145
	 * Remove any temporary file from the service.
146
	 */
147
	public function clean() {
148
		$this->tempManager->clean();
149
	}
150
151
}
152