Passed
Push — dependabot/npm_and_yarn/ellipt... ( eb0fee )
by
unknown
24:49
created

ModelService::getFileModelPath()   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 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2019, Matias De lellis <[email protected]>
4
 *
5
 * @author Matias De lellis <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\FaceRecognition\Service;
25
26
use OCP\IConfig;
27
use OCP\Files\IAppData;
28
use OCP\Files\IRootFolder;
29
use OCP\Files\NotFoundException;
30
31
use OCA\FaceRecognition\Service\SettingsService;
32
33
class ModelService {
34
35
	/** @var IConfig */
36
	private $config;
37
38
	/** @var IAppData */
39
	private $appData;
40
41
	/** @var IRootFolder */
42
	private $rootFolder;
43
44
	/** @var string */
45
	private $modelsFolder;
46
47
	/** @var SettingsService */
48
	private $settingsService;
49
50 1
	public function __construct(IConfig         $config,
51
	                            IAppData        $appData,
52
	                            IRootFolder     $rootFolder,
53
	                            SettingsService $settingsService)
54
	{
55 1
		$this->config         = $config;
56 1
		$this->appData         = $appData;
57 1
		$this->rootFolder      = $rootFolder;
58 1
		$this->settingsService = $settingsService;
59
60 1
		$this->modelsFolder = $this->getModelsFolder();
61 1
	}
62
63
	/**
64
	 * @return string
65
	 */
66 4
	public function getFileModelPath(int $modelId, string $file): string {
67 4
		return $this->modelsFolder . '/' . $modelId . '/' . $file;
68
	}
69
70
	/**
71
	 * @return bool
72
	 */
73 4
	public function modelFileExists(int $modelId, string $file): bool {
74 4
		return file_exists($this->getFileModelPath($modelId, $file));
75
	}
76
77
	/**
78
	 * @return void
79
	 */
80 1
	public function prepareModelFolder(int $modelId) {
81
		// Custum folder
82 1
		if (!is_null($this->settingsService->getSystemModelPath())) {
83
			$modelFolder = $this->modelsFolder . '/' . $modelId;
84
			if (!is_dir($modelFolder)) {
85
				mkdir($modelFolder, 0770, true);
86
			}
87
			return;
88
		}
89
90
		// Default folder
91
		try {
92 1
			$this->appData->getFolder('/models/' . $modelId);
93 1
		} catch (NotFoundException $e) {
94 1
			$this->appData->newFolder('/models/' . $modelId);
95
		}
96 1
	}
97
98
	/**
99
	 * @return string
100
	 */
101 1
	private function getModelsFolder(): string {
102
		// Custum folder
103 1
		$customFolder = $this->settingsService->getSystemModelPath();
104 1
		if (!is_null($customFolder)) {
105
			if (!is_dir($customFolder)) {
106
				mkdir($customFolder, 0770, true);
107
			}
108
			return $customFolder;
109
		}
110
111
		// Default folder
112
		try {
113 1
			$this->appData->getFolder('/models');
114 1
		} catch (NotFoundException $e) {
115 1
			$this->appData->newFolder('/models');
116
		}
117
118 1
		$instanceId = $this->config->getSystemValue('instanceid', null);
119 1
		$appData = $this->rootFolder->get('appdata_'.$instanceId)->getPath();
120 1
		$dataDir = $this->config->getSystemValue('datadirectory', null);
121
122 1
		return $dataDir . '/' . $appData . '/facerecognition/models/';
123
	}
124
125
}