ILIAS  release_8 Revision v8.25-1-g13de6a5eca6
class.ilFileSystemAbstractionStorage.php
Go to the documentation of this file.
1<?php
2
22
27{
28 public const STORAGE_WEB = 1;
29 public const STORAGE_DATA = 2;
30 public const STORAGE_SECURED = 3;
31 private const FACTOR = 100;
32 private const MAX_EXPONENT = 3;
33 private const SECURED_DIRECTORY = "sec";
34 private int $container_id;
35 private int $storage_type;
36 private bool $path_conversion = false;
37 protected ?string $path = null;
38 protected \ILIAS\Filesystem\Filesystems $file_system_service;
39
50 public function __construct(int $a_storage_type, bool $a_path_conversion, int $a_container_id)
51 {
52 global $DIC;
53 $this->storage_type = $a_storage_type;
54 $this->path_conversion = $a_path_conversion;
55 $this->container_id = $a_container_id;
56 $this->file_system_service = $DIC->filesystem();
57
58 // Get path info
59 $this->init();
60 }
61
62 public function fileExists(string $a_absolute_path): bool
63 {
64 return $this->getFileSystemService()->has($this->createRelativePathForFileSystem($a_absolute_path));
65 }
66
67 protected function getLegacyFullAbsolutePath(string $relative_path): string
68 {
69 $stream = $this->getFileSystemService()->readStream($relative_path);
70
71 return $stream->getMetadata('uri');
72 }
73
74 protected function getFileSystemService(): Filesystem
75 {
76 switch ($this->getStorageType()) {
78 return $this->file_system_service->storage();
82 return $this->file_system_service->web();
83 }
84 throw new LogicException('cannot determine correct filesystem');
85 }
86
87 public function getContainerId(): int
88 {
90 }
91
92 public static function createPathFromId(int $a_container_id, string $a_name): string
93 {
94 $path = [];
95 $found = false;
96 $num = $a_container_id;
97 $path_string = '';
98 for ($i = self::MAX_EXPONENT; $i > 0; $i--) {
99 $factor = pow(self::FACTOR, $i);
100 if (($tmp = (int) ($num / $factor)) or $found) {
101 $path[] = $tmp;
102 $num = $num % $factor;
103 $found = true;
104 }
105 }
106
107 if (count($path)) {
108 $path_string = (implode('/', $path) . '/');
109 }
110
111 return $path_string . $a_name . '_' . $a_container_id;
112 }
113
117 public function writeToFile(string $a_data, $a_absolute_path)
118 {
119 if (!$fp = @fopen($a_absolute_path, 'w+')) {
120 return false;
121 }
122 if (@fwrite($fp, $a_data) === false) {
123 @fclose($fp);
124 return false;
125 }
126 @fclose($fp);
127 return true;
128 }
129
133 public function copyFile(string $a_from, string $a_to): bool
134 {
135 if (@file_exists($a_from)) {
136 @copy($a_from, $a_to);
137 return true;
138 }
139 return false;
140 }
141
146 abstract protected function getPathPrefix(): string;
147
153 abstract protected function getPathPostfix(): string;
154
155 public function create(): void
156 {
157 if (!$this->getFileSystemService()->has($this->path)) {
158 $this->getFileSystemService()->createDir($this->path);
159 }
160 }
161
171 public function getAbsolutePath(): string
172 {
173 return rtrim($this->getLegacyAbsolutePath(), "/");
174 }
175
183 protected function getLegacyAbsolutePath(): string
184 {
185 if (!$this->getFileSystemService()->has($this->path)) {
186 $this->getFileSystemService()->createDir($this->path);
187 }
188
189 if ($this->getStorageType() === self::STORAGE_DATA) {
190 return rtrim(CLIENT_DATA_DIR, '/') . '/' . ltrim($this->path, '/');
191 }
192 return rtrim(CLIENT_WEB_DIR, '/') . '/' . ltrim($this->path, '/');
193 }
194
195 protected function init(): bool
196 {
197 switch ($this->storage_type) {
200 break;
202 $this->path = rtrim($this->path, '/') . '/' . self::SECURED_DIRECTORY . '/';
203 break;
204 }
205
206 // Append path prefix
207 $this->path .= ($this->getPathPrefix() . '/');
208
209 if ($this->path_conversion) {
210 $this->path .= self::createPathFromId($this->container_id, $this->getPathPostfix());
211 } else {
212 $this->path .= ($this->getPathPostfix() . '_' . $this->container_id);
213 }
214
215 return true;
216 }
217
218 public function delete(): bool
219 {
220 try {
221 $this->getFileSystemService()->deleteDir($this->getAbsolutePath());
222 } catch (Exception $e) {
223 return false;
224 }
225
226 return true;
227 }
228
229 public function deleteDirectory(string $a_abs_name): bool
230 {
231 $path = $this->createRelativePathForFileSystem($a_abs_name);
232 $this->getFileSystemService()->deleteDir($path);
233 return !$this->getFileSystemService()->has($path);
234 }
235
236 public function deleteFile(string $a_abs_name): bool
237 {
238 $path = $this->createRelativePathForFileSystem($a_abs_name);
239 $this->getFileSystemService()->delete($path);
240 return !$this->getFileSystemService()->has($path);
241 }
242
243 public static function _copyDirectory(string $a_sdir, string $a_tdir): bool
244 {
245 try {
246 $sourceFS = LegacyPathHelper::deriveFilesystemFrom($a_sdir);
247 $targetFS = LegacyPathHelper::deriveFilesystemFrom($a_tdir);
248
249 $sourceDir = LegacyPathHelper::createRelativePath($a_sdir);
250 $targetDir = LegacyPathHelper::createRelativePath($a_tdir);
251
252 // check if arguments are directories
253 if (!$sourceFS->hasDir($sourceDir)) {
254 return false;
255 }
256
257 $sourceList = $sourceFS->listContents($sourceDir, true);
258
259 foreach ($sourceList as $item) {
260 if ($item->isDir()) {
261 continue;
262 }
263
264 $itemPath = $targetDir . '/' . substr($item->getPath(), strlen($sourceDir));
265 $stream = $sourceFS->readStream($sourceDir);
266 $targetFS->writeStream($itemPath, $stream);
267 }
268
269 return true;
270 } catch (\Exception $exception) {
271 return false;
272 }
273 }
274
275 public function appendToPath(string $a_appendix): void
276 {
277 $this->path .= $a_appendix;
278 }
279
280 public function getStorageType(): int
281 {
282 return $this->storage_type;
283 }
284
285 public function getPath(): string
286 {
287 return $this->path;
288 }
289
290 private function createRelativePathForFileSystem(string $a_absolute_path): string
291 {
292 $relative_path = ILIAS\Filesystem\Util\LegacyPathHelper::createRelativePath($a_absolute_path);
293
294 return $relative_path;
295 }
296}
static createRelativePath(string $absolute_path)
Creates a relative path from an absolute path which starts with a valid storage location.
static createPathFromId(int $a_container_id, string $a_name)
static _copyDirectory(string $a_sdir, string $a_tdir)
__construct(int $a_storage_type, bool $a_path_conversion, int $a_container_id)
Constructor.
getAbsolutePath()
Calculates the full path on the filesystem.
getLegacyAbsolutePath()
Calculates the absolute filesystem storage location.
getPathPrefix()
Get path prefix.
getPathPostfix()
Get directory name.
const CLIENT_WEB_DIR
Definition: constants.php:47
const CLIENT_DATA_DIR
Definition: constants.php:46
global $DIC
Definition: feed.php:28
Interface Filesystem.
Definition: Filesystem.php:40
$i
Definition: metadata.php:41
Class FlySystemFileAccessTest \Provider\FlySystem @runTestsInSeparateProcesses @preserveGlobalState d...
has(string $class_name)