Passed
Push — master ( 094995...440a0c )
by Maxence
02:06
created

FileService::getDirectoriesFromAppDataFolder()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.2
cc 4
eloc 6
nc 3
nop 1
1
<?php
2
/**
3
 * CMS Pico - Integration of Pico within your files to create websites.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\CMSPico\Service;
28
29
use DirectoryIterator;
30
use Exception;
31
use OCA\CMSPico\AppInfo\Application;
32
use OCP\Files\Folder;
33
use OCP\Files\IRootFolder;
34
use OCP\Files\NotFoundException;
35
36
class FileService {
37
38
39
	const INSTALL_DIR = __DIR__ . '/../../Pico/';
40
41
	/** @var IRootFolder */
42
	private $rootFolder;
43
44
	/** @var ConfigService */
45
	private $configService;
46
47
	/** @var MiscService */
48
	private $miscService;
49
50
	/** @var Folder */
51
	private $appDataFolder;
52
53
54
	/**
55
	 * ConfigService constructor.
56
	 *
57
	 * @param IRootFolder $rootFolder
58
	 * @param ConfigService $configService
59
	 * @param MiscService $miscService
60
	 */
61
	public function __construct(
62
		IRootFolder $rootFolder, ConfigService $configService, MiscService $miscService
63
	) {
64
		$this->rootFolder = $rootFolder;
65
		$this->configService = $configService;
66
		$this->miscService = $miscService;
67
	}
68
69
70
//	public function getAppDataFolderContent($dir) {
71
	public function getDirectoriesFromAppDataFolder($dir) {
72
// do we still use DirectoryIterator as files are in DB ?
73
		$all = [];
74
75
		foreach (new DirectoryIterator($this->getAppDataFolderPath($dir, true)) as $file) {
76
			if (!$file->isDir() || substr($file->getFilename(), 0, 1) === '.') {
77
				continue;
78
			}
79
80
			$all[] = $file->getFilename();
81
		}
82
83
		return $all;
84
	}
85
86
87
	/**
88
	 * @param string $base
89
	 * @param string $dir
90
	 *
91
	 * @return string[]
92
	 */
93 View Code Duplication
	public function getSourceFiles($base, $dir = '') {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
95
		$base = MiscService::endSlash($base);
96
		$dir = MiscService::endSlash($dir);
97
98
		$files = [];
99
		foreach (new DirectoryIterator($base . $dir) as $file) {
100
101
			if (substr($file->getFilename(), 0, 1) === '.') {
102
				continue;
103
			}
104
105
			if ($file->isDir()) {
106
				$files[] = $dir . $file->getFilename() . '/';
107
				$files = array_merge($files, $this->getSourceFiles($base, $dir . $file->getFilename()));
108
				continue;
109
			}
110
111
			$files[] = $dir . $file->getFilename();
112
		}
113
114
		return $files;
115
	}
116
117
118
	/**
119
	 * // TODO: this function should use File from nc, not read on the filesystem
120
	 *
121
	 * @param string $base
122
	 * @param string $dir
123
	 *
124
	 * @return string[]
125
	 */
126 View Code Duplication
	public function getAppDataFiles($base, $dir = '') {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
128
		$base = MiscService::endSlash($base);
129
		$dir = MiscService::endSlash($dir);
130
131
		$files = [];
132
		foreach (new DirectoryIterator($base . $dir) as $file) {
133
134
			if (substr($file->getFilename(), 0, 1) === '.') {
135
				continue;
136
			}
137
138
			if ($file->isDir()) {
139
				$files[] = $dir . $file->getFilename() . '/';
140
				$files = array_merge($files, $this->getSourceFiles($base, $dir . $file->getFilename()));
141
				continue;
142
			}
143
144
			$files[] = $dir . $file->getFilename();
145
		}
146
147
		return $files;
148
	}
149
150
151
	/**
152
	 * @param string $dir
153
	 *
154
	 * @param bool $absolute
155
	 *
156
	 * @return string
157
	 */
158
	public function getAppDataFolderPath($dir, $absolute = false) {
159
160
		$appNode = $this->getAppDataFolder();
161
162
		try {
163
			$appNode->get($dir);
164
		} catch (NotFoundException $e) {
165
			$this->createAppDataFolder($appNode, $dir);
166
		}
167
168
		$appPath = '';
169
		if ($absolute) {
170
			$dataDir = $this->configService->getSystemValue('datadirectory', null);
171
			$appPath .= MiscService::endSlash($dataDir);
172
		}
173
174
		$appPath .= MiscService::endSlash($appNode->getInternalPath());
175
		$appPath .= MiscService::endSlash($dir);
176
177
		return $appPath;
178
	}
179
180
181
	/**
182
	 * Create Appdata Subfolder and duplicate the content from apps/cms_pico/Pico/
183
	 *
184
	 * @param Folder $appNode
185
	 * @param string $dir
186
	 */
187
	private function createAppDataFolder(Folder $appNode, $dir) {
188
		$appFolder = $appNode->newFolder($dir);
189
190
		$files = $this->getSourceFiles(self::INSTALL_DIR . $dir);
191
		foreach ($files as $file) {
192
			if (substr($file, -1) === '/') {
193
				$appFolder->newFolder($file);
194
			} else {
195
				$newFile = $appFolder->newFile($file);
196
				$newFile->putContent(file_get_contents(self::INSTALL_DIR . $dir . '/' . $file));
197
			}
198
		}
199
	}
200
201
202
	/**
203
	 * Get AppData Folder for this app, create it otherwise.
204
	 *
205
	 * @return Folder
206
	 */
207
	private function getAppDataFolder() {
208
		if ($this->appDataFolder === null) {
209
210
			$instanceId = $this->configService->getSystemValue('instanceid', null);
211
			$name = 'appdata_' . $instanceId;
212
213
			/** @var Folder $globalAppDataFolder */
214
			try {
215
				$globalAppDataFolder = $this->rootFolder->get($name);
216
			} catch (NotFoundException $e) {
217
				$globalAppDataFolder = $this->rootFolder->newFolder($name);
218
			}
219
220
			try {
221
				$this->appDataFolder = $globalAppDataFolder->get(Application::APP_NAME);
222
			} catch (NotFoundException $e) {
223
				$this->appDataFolder = $globalAppDataFolder->newFolder(Application::APP_NAME);
224
			}
225
		}
226
227
		return $this->appDataFolder;
228
	}
229
230
231
}
232