ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
AbstractCtrlAwareUploadHandler.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25use ILIAS\HTTP\Services as HttpServices;
26use ilCtrl;
27
34{
35 protected const CMD_UPLOAD = 'upload';
36 protected const CMD_REMOVE = 'remove';
37 protected const CMD_INFO = 'info';
38
39 protected HttpServices $http;
40 protected ilCtrl $ctrl;
42
43 protected bool $is_chunked = false;
44 protected int $chunk_index = 0;
45 protected int $amount_of_chunks = 0;
46 protected ?string $chunk_id = null;
47 protected int $chunk_total_size = 0;
48
49
53 public function __construct()
54 {
55 global $DIC;
56 $this->ctrl = $DIC->ctrl();
57 $this->upload = $DIC->upload();
58 $this->http = $DIC->http();
59 }
60
61
65 public function getFileIdentifierParameterName(): string
66 {
68 }
69
70
74 public function getUploadURL(): string
75 {
76 return $this->ctrl->getLinkTargetByClass([static::class], self::CMD_UPLOAD);
77 }
78
79
83 public function getExistingFileInfoURL(): string
84 {
85 return $this->ctrl->getLinkTargetByClass([static::class], self::CMD_INFO);
86 }
87
88
92 public function getFileRemovalURL(): string
93 {
94 return $this->ctrl->getLinkTargetByClass([static::class], self::CMD_REMOVE);
95 }
96
97 protected function readChunkedInformation(): void
98 {
99 $body = $this->http->request()->getParsedBody();
100 $this->chunk_id = $body['dzuuid'] ?? null;
101 $this->amount_of_chunks = (int) ($body['dztotalchunkcount'] ?? 0);
102 $this->chunk_index = (int) ($body['dzchunkindex'] ?? 0);
103 $this->chunk_total_size = (int) ($body['dztotalfilesize'] ?? 0);
104 $this->is_chunked = ($this->chunk_id !== null && $this->amount_of_chunks > 0);
105 }
106
107
108 public function executeCommand(): void
109 {
110 switch ($this->ctrl->getCmd()) {
111 case self::CMD_UPLOAD:
112 // Here you must save the file and tell the input item the
113 // file-id which will be a FileStorage-ID in a later version
114 // of ILIAS and for now you must implement an own ID which allows
115 // identifying the file after the request
116 try {
117 $this->readChunkedInformation();
118 $content = json_encode($this->getUploadResult());
119 } catch (\Throwable $t) {
120 $content = json_encode(
124 '',
125 $t->getMessage()
126 )
127 );
128 }
129 break;
130 case self::CMD_REMOVE:
131 // here you delete the previously uploaded file again, you know
132 // which file to delete since you defined what 'my_file_id' is.
133 $file_identifier = $this->http->request()->getQueryParams()[$this->getFileIdentifierParameterName()];
134 $content = json_encode($this->getRemoveResult($file_identifier));
135 break;
136 case self::CMD_INFO:
137 // here you give info about an already existing file
138 // return a JsonEncoded \ILIAS\FileUpload\Handler\FileInfoResult
139 $file_identifier = $this->http->request()->getQueryParams()[$this->getFileIdentifierParameterName()];
140 $content = json_encode($this->getInfoResult($file_identifier));
141 break;
142 default:
143 $content = '';
144 break;
145 }
146 $response = $this->http->response()->withBody(Streams::ofString($content));
147 $this->http->saveResponse($response);
148 $this->http->sendResponse();
149 $this->http->close();
150 }
151
152
153 abstract protected function getUploadResult(): HandlerResult;
154
155
156 abstract protected function getRemoveResult(string $identifier): HandlerResult;
157
158
159 abstract public function getInfoResult(string $identifier): ?FileInfoResult;
160
161
162 abstract public function getInfoForExistingFiles(array $file_ids): array;
163
164 public function supportsChunkedUploads(): bool
165 {
166 return false;
167 }
168
169}
executeCommand()
Since this is a ilCtrl aware UploadHandler executeCommand MUST be implemented.
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:41
Class Services.
Definition: Services.php:38
Class ilCtrl provides processing control methods.
static http()
Fetches the global http state from ILIAS.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:26
$response
Definition: xapitoken.php:93