ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilFileSystemAbstractionStorage.php
Go to the documentation of this file.
1<?php
2
23
28{
29 public const STORAGE_WEB = 1;
30 public const STORAGE_DATA = 2;
31 public const STORAGE_SECURED = 3;
32 private const FACTOR = 100;
33 private const MAX_EXPONENT = 3;
34 private const SECURED_DIRECTORY = "sec";
35 protected ?string $path = null;
37
48 public function __construct(private int $storage_type, private bool $path_conversion, private int $container_id)
49 {
50 global $DIC;
51 $this->file_system_service = $DIC->filesystem();
52
53 // Get path info
54 $this->init();
55 }
56
57 public function fileExists(string $a_absolute_path): bool
58 {
59 return $this->getFileSystemService()->has($this->createRelativePathForFileSystem($a_absolute_path));
60 }
61
62 protected function getLegacyFullAbsolutePath(string $relative_path): string
63 {
64 $stream = $this->getFileSystemService()->readStream($relative_path);
65
66 return $stream->getMetadata('uri');
67 }
68
69 protected function getFileSystemService(): Filesystem
70 {
71 switch ($this->getStorageType()) {
73 return $this->file_system_service->storage();
77 return $this->file_system_service->web();
78 }
79 throw new LogicException('cannot determine correct filesystem');
80 }
81
82 public function getContainerId(): int
83 {
84 return $this->container_id;
85 }
86
87 public static function createPathFromId(int $a_container_id, string $a_name): string
88 {
89 $path = [];
90 $found = false;
91 $num = $a_container_id;
92 $path_string = '';
93 for ($i = self::MAX_EXPONENT; $i > 0; $i--) {
94 $factor = self::FACTOR ** $i;
95 if (($tmp = (int) ($num / $factor)) or $found) {
96 $path[] = $tmp;
97 $num %= $factor;
98 $found = true;
99 }
100 }
101
102 if ($path !== []) {
103 $path_string = (implode('/', $path) . '/');
104 }
105
106 return $path_string . $a_name . '_' . $a_container_id;
107 }
108
112 public function writeToFile(string $a_data, $a_absolute_path)
113 {
114 if (!$fp = @fopen($a_absolute_path, 'w+')) {
115 return false;
116 }
117 if (@fwrite($fp, $a_data) === false) {
118 @fclose($fp);
119 return false;
120 }
121 @fclose($fp);
122 return true;
123 }
124
128 public function copyFile(string $a_from, string $a_to): bool
129 {
130 if (@file_exists($a_from)) {
131 @copy($a_from, $a_to);
132 return true;
133 }
134 return false;
135 }
136
141 abstract protected function getPathPrefix(): string;
142
148 abstract protected function getPathPostfix(): string;
149
150 public function create(): void
151 {
152 if (!$this->getFileSystemService()->has($this->path)) {
153 $this->getFileSystemService()->createDir($this->path);
154 }
155 }
156
166 public function getAbsolutePath(): string
167 {
168 return rtrim($this->getLegacyAbsolutePath(), "/");
169 }
170
178 protected function getLegacyAbsolutePath(): string
179 {
180 if (!$this->getFileSystemService()->has($this->path)) {
181 $this->getFileSystemService()->createDir($this->path);
182 }
183
184 if ($this->getStorageType() === self::STORAGE_DATA) {
185 return rtrim(CLIENT_DATA_DIR, '/') . '/' . ltrim((string) $this->path, '/');
186 }
187 return rtrim(CLIENT_WEB_DIR, '/') . '/' . ltrim((string) $this->path, '/');
188 }
189
190 protected function init(): bool
191 {
192 switch ($this->storage_type) {
195 break;
197 $this->path = rtrim($this->path ?? '', '/') . '/' . self::SECURED_DIRECTORY . '/';
198 break;
199 }
200
201 // Append path prefix
202 $this->path .= ($this->getPathPrefix() . '/');
203
204 if ($this->path_conversion) {
205 $this->path .= self::createPathFromId($this->container_id, $this->getPathPostfix());
206 } else {
207 $this->path .= ($this->getPathPostfix() . '_' . $this->container_id);
208 }
209
210 return true;
211 }
212
213 public function delete(): bool
214 {
215 try {
216 $this->getFileSystemService()->deleteDir($this->getAbsolutePath());
217 } catch (Exception) {
218 return false;
219 }
220
221 return true;
222 }
223
224 public function deleteDirectory(string $a_abs_name): bool
225 {
226 $path = $this->createRelativePathForFileSystem($a_abs_name);
227 $this->getFileSystemService()->deleteDir($path);
228 return !$this->getFileSystemService()->has($path);
229 }
230
231 public function deleteFile(string $a_abs_name): bool
232 {
233 $path = $this->createRelativePathForFileSystem($a_abs_name);
234 $this->getFileSystemService()->delete($path);
235 return !$this->getFileSystemService()->has($path);
236 }
237
238 public static function _copyDirectory(string $a_sdir, string $a_tdir): bool
239 {
240 try {
241 $sourceFS = LegacyPathHelper::deriveFilesystemFrom($a_sdir);
242 $targetFS = LegacyPathHelper::deriveFilesystemFrom($a_tdir);
243
244 $sourceDir = LegacyPathHelper::createRelativePath($a_sdir);
245 $targetDir = LegacyPathHelper::createRelativePath($a_tdir);
246
247 // check if arguments are directories
248 if (!$sourceFS->hasDir($sourceDir)) {
249 return false;
250 }
251
252 $sourceList = $sourceFS->listContents($sourceDir, true);
253
254 foreach ($sourceList as $item) {
255 if ($item->isDir()) {
256 continue;
257 }
258
259 $itemPath = $targetDir . '/' . substr($item->getPath(), strlen($sourceDir));
260 $stream = $sourceFS->readStream($sourceDir);
261 $targetFS->writeStream($itemPath, $stream);
262 }
263
264 return true;
265 } catch (\Exception) {
266 return false;
267 }
268 }
269
270 public function appendToPath(string $a_appendix): void
271 {
272 $this->path .= $a_appendix;
273 }
274
275 public function getStorageType(): int
276 {
277 return $this->storage_type;
278 }
279
280 public function getPath(): string
281 {
282 return $this->path;
283 }
284
285 private function createRelativePathForFileSystem(string $a_absolute_path): string
286 {
287 $relative_path = LegacyPathHelper::createRelativePath($a_absolute_path);
288
289 return $relative_path;
290 }
291}
Indicates general problems with the input or output operations.
Definition: IOException.php:28
The legacy path helper provides convenient functions for the integration of the filesystem service wi...
__construct(private int $storage_type, private bool $path_conversion, private int $container_id)
Constructor.
static createPathFromId(int $a_container_id, string $a_name)
static _copyDirectory(string $a_sdir, string $a_tdir)
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
The filesystem interface provides the public interface for the Filesystem service API consumer.
Definition: Filesystem.php:37
The Filesystems interface defines the access methods which can be used to fetch the different filesys...
Definition: Filesystems.php:30
has(string $class_name)
global $DIC
Definition: shib_login.php:26