Passed
Push — master ( c7fee6...094995 )
by Maxence
02:09
created

FileService::getDirectoriesFromAppDataFolder()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 11
nc 6
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
		$appNode = $this->getAppDataFolder();
76
		try {
77
			$appNode->get($dir);
78
		} catch (NotFoundException $e) {
79
			$this->createAppDataFolder($appNode, $dir);
80
		}
81
82
		foreach (new DirectoryIterator($this->getAppDataFolderPath($dir, true)) as $file) {
83
			if (!$file->isDir() || substr($file->getFilename(), 0, 1) === '.') {
84
				continue;
85
			}
86
87
			$all[] = $file->getFilename();
88
		}
89
90
		return $all;
91
	}
92
93
94
	/**
95
	 * @param string $base
96
	 * @param string $dir
97
	 *
98
	 * @return string[]
99
	 */
100 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...
101
102
		$base = MiscService::endSlash($base);
103
		$dir = MiscService::endSlash($dir);
104
105
		$files = [];
106
		foreach (new DirectoryIterator($base . $dir) as $file) {
107
108
			if (substr($file->getFilename(), 0, 1) === '.') {
109
				continue;
110
			}
111
112
			if ($file->isDir()) {
113
				$files[] = $dir . $file->getFilename() . '/';
114
				$files = array_merge($files, $this->getSourceFiles($base, $dir . $file->getFilename()));
115
				continue;
116
			}
117
118
			$files[] = $dir . $file->getFilename();
119
		}
120
121
		return $files;
122
	}
123
124
125
	/**
126
	 * // TODO: this function should use File from nc, not read on the filesystem
127
	 *
128
	 * @param string $base
129
	 * @param string $dir
130
	 *
131
	 * @return string[]
132
	 */
133 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...
134
135
		$base = MiscService::endSlash($base);
136
		$dir = MiscService::endSlash($dir);
137
138
		$files = [];
139
		foreach (new DirectoryIterator($base . $dir) as $file) {
140
141
			if (substr($file->getFilename(), 0, 1) === '.') {
142
				continue;
143
			}
144
145
			if ($file->isDir()) {
146
				$files[] = $dir . $file->getFilename() . '/';
147
				$files = array_merge($files, $this->getSourceFiles($base, $dir . $file->getFilename()));
148
				continue;
149
			}
150
151
			$files[] = $dir . $file->getFilename();
152
		}
153
154
		return $files;
155
	}
156
157
158
	/**
159
	 * @param string $dir
160
	 *
161
	 * @param bool $absolute
162
	 *
163
	 * @return string
164
	 */
165
	public function getAppDataFolderPath($dir, $absolute = false) {
166
167
		$appNode = $this->getAppDataFolder();
168
169
		try {
170
			$appNode->get($dir);
171
		} catch (NotFoundException $e) {
172
			$this->createAppDataFolder($appNode, $dir);
173
		}
174
175
		$appPath = '';
176
		if ($absolute) {
177
			$dataDir = $this->configService->getSystemValue('datadirectory', null);
178
			$appPath .= MiscService::endSlash($dataDir);
179
		}
180
181
		$appPath .= MiscService::endSlash($appNode->getInternalPath());
182
		$appPath .= MiscService::endSlash($dir);
183
184
		return $appPath;
185
	}
186
187
188
	/**
189
	 * Create Appdata Subfolder and duplicate the content from apps/cms_pico/Pico/
190
	 *
191
	 * @param Folder $appNode
192
	 * @param string $dir
193
	 */
194
	private function createAppDataFolder(Folder $appNode, $dir) {
195
		$appFolder = $appNode->newFolder($dir);
196
197
		$files = $this->getSourceFiles(self::INSTALL_DIR . $dir);
198
		foreach ($files as $file) {
199
			if (substr($file, -1) === '/') {
200
				$appFolder->newFolder($file);
201
			} else {
202
				$newFile = $appFolder->newFile($file);
203
				$newFile->putContent(file_get_contents(self::INSTALL_DIR . $dir . '/' . $file));
204
			}
205
		}
206
	}
207
208
209
	/**
210
	 * Get AppData Folder for this app, create it otherwise.
211
	 *
212
	 * @return Folder
213
	 */
214
	private function getAppDataFolder() {
215
		if ($this->appDataFolder === null) {
216
217
			$instanceId = $this->configService->getSystemValue('instanceid', null);
218
			$name = 'appdata_' . $instanceId;
219
220
			/** @var Folder $globalAppDataFolder */
221
			try {
222
				$globalAppDataFolder = $this->rootFolder->get($name);
223
			} catch (NotFoundException $e) {
224
				$globalAppDataFolder = $this->rootFolder->newFolder($name);
225
			}
226
227
			try {
228
				$this->appDataFolder = $globalAppDataFolder->get(Application::APP_NAME);
229
			} catch (NotFoundException $e) {
230
				$this->appDataFolder = $globalAppDataFolder->newFolder(Application::APP_NAME);
231
			}
232
		}
233
234
		return $this->appDataFolder;
235
	}
236
237
238
}
239