ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilObjFileAbstractZipDelegate.php
Go to the documentation of this file.
1<?php
2
22
28{
32 protected $access_handler;
36 protected $node_type;
40 protected $tree;
44 protected $path_map = [];
48 protected $zip;
52 protected $uploaded_suffixes = [];
53
62
69 public function __construct(object $access_checker, int $node_type, ilTree $tree)
70 {
71 $this->access_handler = $access_checker;
72 $this->node_type = $node_type;
73 $this->tree = $tree;
74 }
75
76 protected function createSurroundingContainer(UploadResult $result, int $parent_id) : int
77 {
78 // create one base container with the name of the zip itself, all other containers will be created inside this one
79 $info = new SplFileInfo($result->getName());
80 $base_path = $info->getBasename("." . $info->getExtension());
81 $base_container = $this->createContainer($base_path, $parent_id);
82
83 return (int) $base_container->getRefId();
84 }
85
86
87
88 protected function tearDown() : void
89 {
90 $this->zip->close();
91 }
92
96 protected function initZip(UploadResult $result) : void
97 {
98 $this->zip = new ZipArchive();
99 $this->zip->open($result->getPath());
100 }
101
102
103 protected function isInWorkspace() : bool
104 {
105 return $this->node_type === ilObjFileGUI::WORKSPACE_NODE_ID;
106 }
107
113 protected function createContainer(string $original_path, int $parent_id) : ilObject
114 {
115 // Create folder or cat or WorkspaceFolder
116 $obj = $this->getPossibleContainer($parent_id);
117 $obj->setTitle(basename($original_path));
118 $obj->create();
119
120 if (!$this->isInWorkspace()) {
121 $obj->createReference();
122 $obj->putInTree($parent_id);
123 $obj->setPermissions($parent_id);
124 } else {
125 $node_id = $this->tree->insertObject($parent_id, $obj->getId());
126 $this->access_handler->setPermissions($parent_id, $node_id);
127 $obj->setRefId($node_id);
128 }
129
130 if ($obj->getType() === "cat") {
131 global $DIC;
132
133 $lng = $DIC->language();
134 $obj->addTranslation(basename($original_path), "", $lng->getLangKey(), $lng->getLangKey());
135 }
136
137 return $obj;
138 }
139
140
146 protected function createFile(string $original_path, int $parent_id) : ilObjFile
147 {
148 $obj = new ilObjFile();
149 $obj->setTitle(basename($original_path));
150 $obj->create();
151
152 $obj->appendStream(Streams::ofString($this->zip->getFromName($original_path)), basename($original_path));
153
154 if (!$this->isInWorkspace()) {
155 $obj->createReference();
156 $obj->putInTree($parent_id);
157 $obj->setPermissions($parent_id);
158 } else {
159 $node_id = $this->tree->insertObject($parent_id, $obj->getId());
160 $this->access_handler->setPermissions($parent_id, $node_id);
161 $obj->setRefId($node_id);
162 }
163
164 $this->uploaded_suffixes[] = $obj->getFileExtension();
165
166 return $obj;
167 }
168
172 protected function getNextPath() : Generator
173 {
174 $paths = [];
175
176 for ($i = 0; $i < $this->zip->numFiles; $i++) {
177 $original_path = $this->zip->getNameIndex($i, ZipArchive::FL_UNCHANGED);
178 if (strpos($original_path, '__MACOSX') !== false || strpos($original_path, '.DS_') !== false) {
179 continue;
180 }
181 $paths[] = $original_path;
182 }
183
184 $path_with_root_folders = [];
185
186 foreach ($paths as $path) {
187 $parent = dirname($path) . '/';
188 if ($parent !== './' && !in_array($parent, $paths)) {
189 $path_with_root_folders[] = $parent;
190 }
191 $path_with_root_folders[] = $path;
192 }
193
194 $path_with_root_folders = array_unique($path_with_root_folders);
195 sort($path_with_root_folders);
196
197 yield from $path_with_root_folders;
198 }
199
200 protected function hasMultipleRootEntriesInZip() : bool
201 {
202 $amount = 0;
203 foreach ($this->getNextPath() as $zip_directory) {
204 $dirname = dirname($zip_directory);
205 if ($dirname === '.') {
206 $amount++;
207 }
208 if ($amount > 1) {
209 return true;
210 }
211 }
212 return false;
213 }
214
215 public function getUploadedSuffixes() : array
216 {
217 return array_map('strtolower', $this->uploaded_suffixes);
218 }
219}
$result
An exception for terminatinating execution or to throw for unit testing.
Class Streams Stream factory which enables the user to create streams without the knowledge of the co...
Definition: Streams.php:17
Class ilObjFileAbstractZipDelegate.
__construct(object $access_checker, int $node_type, ilTree $tree)
ilObjFileAbstractZipDelegate constructor.
createSurroundingContainer(UploadResult $result, int $parent_id)
createContainer(string $original_path, int $parent_id)
createFile(string $original_path, int $parent_id)
$create_base_container_for_multiple_root_entries
@description Unzip on operating systems may behave differently when unzipping if there are only one o...
Class ilObjFile.
Class ilObject Basic functions for all objects.
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
global $DIC
Definition: goto.php:24
Interface ilObjUploadDelegateInterface.
$i
Definition: metadata.php:24
$lng