ILIAS  release_8 Revision v8.24
class.ilObjFileAbstractZipProcessor.php
Go to the documentation of this file.
1<?php
2
23
30{
34 private const IRSS_FILEPATH_KEY = 'uri';
35
39 private $tree;
40
41 private ?ZipArchive $archive = null;
42 private int $id_type;
43
52
56 public function __construct(
61 $tree
62 ) {
64
65 $this->id_type = $gui_object->getIdType();
66 $this->tree = $tree;
67 }
68
70 {
71 // create one base container with the name of the zip itself, all other containers will be created inside this one
72 $zip_name = $this->storage->manage()->getCurrentRevision($rid)->getInformation()->getTitle();
73 $info = new SplFileInfo($zip_name);
74 $base_path = $info->getBasename("." . $info->getExtension());
75 $base_container = $this->createContainerObj($base_path, $this->gui_object->getParentId());
76
77 return (int) $base_container->getRefId();
78 }
79
83 protected function createContainerObj(string $dir_name, int $parent_id, array $options = []): ilObject
84 {
85 $container_obj = $this->getPossibleContainerObj($parent_id);
86 $container_obj->setTitle($dir_name);
87
88 if (!empty($options)) {
89 $this->applyOptions($container_obj, $options);
90 }
91
92 $container_obj->create();
93 $container_obj->createReference();
94
95 $this->gui_object->putObjectInTree($container_obj, $parent_id);
96
97 return $container_obj;
98 }
99
103 protected function openZip(ResourceIdentification $rid): void
104 {
105 if (null !== $this->archive) {
106 throw new LogicException("openZip() can only be called once, yet it was called again.");
107 }
108
109 $file_uri = $this->storage->consume()->stream($rid)->getStream()->getMetadata(self::IRSS_FILEPATH_KEY);
110 $this->archive = new ZipArchive();
111 $this->archive->open($file_uri);
112 }
113
118 private function getZipPaths(): Generator
119 {
120 if (null === $this->archive) {
121 throw new LogicException("cannot read content of unopened zip archive");
122 }
123
124 for ($i = 0, $i_max = $this->archive->count(); $i < $i_max; $i++) {
125 $path = $this->archive->getNameIndex($i, ZipArchive::FL_UNCHANGED);
126 if (strpos($path, '__MACOSX') !== false || strpos($path, '.DS_') !== false) {
127 continue;
128 }
129
130 yield $path;
131 }
132 }
133
138 protected function getZipFiles(): Generator
139 {
140 foreach ($this->getZipPaths() as $path) {
141 if (substr($path, -1) !== "/" && substr($path, -1) !== "\\") {
142 yield $path;
143 }
144 }
145 }
146
147 protected function hasMultipleRootEntriesInZip(): bool
148 {
149 $amount = 0;
150 foreach ($this->getZipDirectories() as $zip_directory) {
151 $dirname = dirname($zip_directory);
152 if ($dirname === '.') {
153 $amount++;
154 }
155 if ($amount > 1) {
156 return true;
157 }
158 }
159 foreach ($this->getZipFiles() as $zip_file) {
160 $dirname = dirname($zip_file);
161 if ($dirname === '.') {
162 $amount++;
163 }
164 if ($amount > 1) {
165 return true;
166 }
167 }
168 return false;
169 }
170
176 protected function getZipDirectories(): Generator
177 {
178 $directories = [];
179 foreach ($this->getZipPaths() as $path) {
180 if (substr($path, -1) === "/" || substr($path, -1) === "\\") {
181 $directories[] = $path;
182 }
183 }
184
185 $directories_with_parents = [];
186
187 foreach ($directories as $directory) {
188 $parent = dirname($directory) . '/';
189 if ($parent !== './' && !in_array($parent, $directories)) {
190 $directories_with_parents[] = $parent;
191 }
192 $directories_with_parents[] = $directory;
193 }
194
195 $directories_with_parents = array_unique($directories_with_parents);
196 sort($directories_with_parents);
197 yield from $directories_with_parents;
198 }
199
200
204 protected function storeZippedFile(string $file_path): ResourceIdentification
205 {
206 if (null === $this->archive) {
207 throw new LogicException("No archive has been opened yet, call openZip() first in order to read files.");
208 }
209
210 return $this->storage->manage()->stream(
211 Streams::ofString($this->archive->getFromName($file_path)),
212 $this->stakeholder,
213 basename($file_path)
214 );
215 }
216
220 protected function closeZip(): void
221 {
222 if ($this->archive !== null) {
223 $this->archive->close();
224 }
225 }
226
230 protected function isWorkspace(): bool
231 {
232 return (ilObject2GUI::WORKSPACE_NODE_ID === $this->id_type);
233 }
234
241 private function getPossibleContainerObj(int $parent_id): ilObject
242 {
243 $type = ($this->isWorkspace()) ?
244 ilObject::_lookupType($this->tree->lookupObjectId($parent_id)) :
245 ilObject::_lookupType($parent_id, true)
246 ;
247
248 switch ($type) {
249 case 'wfld':
250 case 'wsrt':
251 return new ilObjWorkspaceFolder();
252
253 case 'cat':
254 case 'root':
255 return new ilObjCategory();
256
257 case 'fold':
258 case 'crs':
259
260 default:
261 return new ilObjFolder();
262 }
263 }
264}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilObjFileAbstractProcessorInterface.
applyOptions(ilObject $obj, array $options)
Apply provided options to the given object.
Class ilObjFileAbstractZipProcessor.
createSurroundingContainer(ResourceIdentification $rid)
getZipPaths()
Yields all paths of the currently open zip-archive (without some macos stuff).
closeZip()
Closes the currently open zip-archive.
getZipDirectories()
Yields the directory-paths of the currently open zip-archive.
bool $create_base_container_for_multiple_root_entries
@description Unzip on operating systems may behave differently when unzipping if there are only one o...
getZipFiles()
Yields the file-paths of the currently open zip-archive.
storeZippedFile(string $file_path)
Creates an IRSS resource from the given filepath.
openZip(ResourceIdentification $rid)
Opens the zip archive of the given resource.
getPossibleContainerObj(int $parent_id)
Returns a container object that is possible for the given parent.
createContainerObj(string $dir_name, int $parent_id, array $options=[])
Creates a container object depending on the parent's node type and returns it.
isWorkspace()
Returns whether the current context is workspace.
__construct(ResourceStakeholder $stakeholder, ilObjFileGUI $gui_object, Services $storage, ilFileServicesSettings $settings, $tree)
GUI class for file objects.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupType(int $id, bool $reference=false)
$path
Definition: ltiservices.php:32
$i
Definition: metadata.php:41
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$type