ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
AbstractCtrlAwareIRSSUploadHandler.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
27
34{
35 protected \ilFileServicesFilenameSanitizer $sanitizer;
36 protected \ilLanguage $language;
37 protected Services $irss;
40 protected array $class_path;
41
42 public function __construct()
43 {
44 global $DIC;
45
46 $this->irss = $DIC->resourceStorage();
47 $this->stakeholder = $this->getStakeholder();
48 $this->temp_filesystem = $DIC->filesystem()->temp();
49 $this->class_path = $this->getClassPath();
50 $this->language = $DIC->language();
51 $this->sanitizer = new \ilFileServicesFilenameSanitizer(
52 $DIC->fileServiceSettings()
53 );
54
56 }
57
58 abstract protected function getStakeholder(): ResourceStakeholder;
59
60 abstract protected function getClassPath(): array;
61
62 protected function getUploadResult(): HandlerResult
63 {
64 $this->upload->process(); // Process the uploads to rund things like PreProcessors
65
66 $result_array = $this->upload->getResults();
67 $result = end($result_array); // get the last result aka the Upload of the user
68
69 if ($result instanceof UploadResult && $result->isOK()) {
70 if ($this->is_chunked) {
71 return $this->processChunckedUpload($result);
72 }
73
74 $identifier = $this->irss->manage()->upload($result, $this->stakeholder)->serialize();
76 $message = "file upload OK";
77 } else {
78 $identifier = '';
80 $message = $this->language->txt(
81 'msg_info_blacklisted'
82 ); // this is the most common reason for a failed upload
83 }
84
85 return new BasicHandlerResult($this->getFileIdentifierParameterName(), $status, $identifier, $message);
86 }
87
89 {
90 $temp_path = $this->sanitizer->sanitize("$this->chunk_id/{$result->getName()}");
91
92 try {
93 if ($this->temp_filesystem->has($temp_path)) {
94 $stream = fopen($this->temp_filesystem->readStream($temp_path)->getMetadata()['uri'], 'ab');
95 fwrite($stream, file_get_contents($result->getPath()));
96 } else {
97 $this->temp_filesystem->write($temp_path, file_get_contents($result->getPath()));
98 }
99 } catch (\Throwable $t) {
100 return new BasicHandlerResult(
103 '',
104 $t->getMessage()
105 );
106 }
107
108 if (($this->chunk_index + 1) === $this->amount_of_chunks) {
109 $whole_file = $this->temp_filesystem->readStream($temp_path);
110 $id = $this->irss->manage()->stream($whole_file, $this->stakeholder, $result->getName());
111
112 return new BasicHandlerResult(
115 $id->serialize(),
116 "file upload OK"
117 );
118 }
119
120 return new BasicHandlerResult(
123 '',
124 "chunk upload OK"
125 );
126 }
127
128 #[\Override]
129 public function getUploadURL(): string
130 {
131 return $this->ctrl->getLinkTargetByClass($this->class_path, self::CMD_UPLOAD, null, true);
132 }
133
134 #[\Override]
135 public function getExistingFileInfoURL(): string
136 {
137 return $this->ctrl->getLinkTargetByClass($this->class_path, self::CMD_INFO, null, true);
138 }
139
140 #[\Override]
141 public function getFileRemovalURL(): string
142 {
143 return $this->ctrl->getLinkTargetByClass($this->class_path, self::CMD_REMOVE, null, true);
144 }
145
146 protected function getRemoveResult(string $identifier): HandlerResult
147 {
148 if (null !== ($id = $this->irss->manage()->find($identifier))) {
149 $this->irss->manage()->remove($id, $this->stakeholder);
150 $status = HandlerResult::STATUS_OK;
151 $message = "file removal OK";
152 } else {
153 $status = HandlerResult::STATUS_OK;
154 $message = "file with identifier '$identifier' doesn't exist, nothing to do.";
155 }
156
157 return new BasicHandlerResult($this->getFileIdentifierParameterName(), $status, $identifier, $message);
158 }
159
160 public function getInfoResult(string $identifier): ?FileInfoResult
161 {
162 if (null !== ($id = $this->irss->manage()->find($identifier))) {
163 $revision = $this->irss->manage()->getCurrentRevision($id)->getInformation();
164 $title = $revision->getTitle();
165 $size = $revision->getSize();
166 $mime = $revision->getMimeType();
167 } else {
168 $title = $mime = 'unknown';
169 $size = 0;
170 }
171
172 return new BasicFileInfoResult($this->getFileIdentifierParameterName(), $identifier, $title, $size, $mime);
173 }
174
175 public function getInfoForExistingFiles(array $file_ids): array
176 {
177 return array_map(fn($file_id): FileInfoResult => $this->getInfoResult($file_id), $file_ids);
178 }
179}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
The filesystem interface provides the public interface for the Filesystem service API consumer.
Definition: Filesystem.php:37
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26
$message
Definition: xapiexit.php:31