ILIAS  release_8 Revision v8.24
AbstractCtrlAwareUploadHandler.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
6
9use ILIAS\HTTP\Services as HttpServices;
10use ilCtrl;
11
12/******************************************************************************
13 *
14 * This file is part of ILIAS, a powerful learning management system.
15 *
16 * ILIAS is licensed with the GPL-3.0, you should have received a copy
17 * of said license along with the source code.
18 *
19 * If this is not the case or you just want to try ILIAS, you'll find
20 * us at:
21 * https://www.ilias.de
22 * https://github.com/ILIAS-eLearning
23 *
24 *****************************************************************************/
31{
32 protected const CMD_UPLOAD = 'upload';
33 protected const CMD_REMOVE = 'remove';
34 protected const CMD_INFO = 'info';
35
36 protected HttpServices $http;
37 protected ilCtrl $ctrl;
39
40 protected bool $is_chunked = false;
41 protected int $chunk_index = 0;
42 protected int $amount_of_chunks = 0;
43 protected ?string $chunk_id = null;
44 protected int $chunk_total_size = 0;
45
46
50 public function __construct()
51 {
52 global $DIC;
53 $this->ctrl = $DIC->ctrl();
54 $this->upload = $DIC->upload();
55 $this->http = $DIC->http();
56 }
57
58
62 public function getFileIdentifierParameterName(): string
63 {
65 }
66
67
71 public function getUploadURL(): string
72 {
73 return $this->ctrl->getLinkTargetByClass([static::class], self::CMD_UPLOAD);
74 }
75
76
80 public function getExistingFileInfoURL(): string
81 {
82 return $this->ctrl->getLinkTargetByClass([static::class], self::CMD_INFO);
83 }
84
85
89 public function getFileRemovalURL(): string
90 {
91 return $this->ctrl->getLinkTargetByClass([static::class], self::CMD_REMOVE);
92 }
93
94 protected function readChunkedInformation(): void
95 {
96 $body = $this->http->request()->getParsedBody();
97 $this->chunk_id = $body['dzuuid'] ?? null;
98 $this->amount_of_chunks = (int) ($body['dztotalchunkcount'] ?? 0);
99 $this->chunk_index = (int) ($body['dzchunkindex'] ?? 0);
100 $this->chunk_total_size = (int) ($body['dztotalfilesize'] ?? 0);
101 $this->is_chunked = ($this->chunk_id !== null && $this->amount_of_chunks > 0);
102 }
103
104
105 public function executeCommand(): void
106 {
107 switch ($this->ctrl->getCmd()) {
108 case self::CMD_UPLOAD:
109 // Here you must save the file and tell the input item the
110 // file-id which will be a FileStorage-ID in a later version
111 // of ILIAS and for now you must implement an own ID which allows
112 // identifying the file after the request
113 try {
114 $this->readChunkedInformation();
115 $content = json_encode($this->getUploadResult());
116 } catch (\Throwable $t) {
117 $content = json_encode(
121 '',
122 $t->getMessage()
123 )
124 );
125 }
126 break;
127 case self::CMD_REMOVE:
128 // here you delete the previously uploaded file again, you know
129 // which file to delete since you defined what 'my_file_id' is.
130 $file_identifier = $this->http->request()->getQueryParams()[$this->getFileIdentifierParameterName()];
131 $content = json_encode($this->getRemoveResult($file_identifier));
132 break;
133 case self::CMD_INFO:
134 // here you give info about an already existing file
135 // return a JsonEncoded \ILIAS\FileUpload\Handler\FileInfoResult
136 $file_identifier = $this->http->request()->getQueryParams()[$this->getFileIdentifierParameterName()];
137 $content = json_encode($this->getInfoResult($file_identifier));
138 break;
139 default:
140 $content = '';
141 break;
142 }
143 $response = $this->http->response()->withBody(Streams::ofString($content));
144 $this->http->saveResponse($response);
145 $this->http->sendResponse();
146 $this->http->close();
147 }
148
149
150 abstract protected function getUploadResult(): HandlerResult;
151
152
153 abstract protected function getRemoveResult(string $identifier): HandlerResult;
154
155
156 abstract public function getInfoResult(string $identifier): ?FileInfoResult;
157
158
159 abstract public function getInfoForExistingFiles(array $file_ids): array;
160
161 public function supportsChunkedUploads(): bool
162 {
163 return false;
164 }
165
166}
executeCommand()
Since this is a ilCtrl aware UploadHandler executeCommand MUST be implemented.
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:43
Class Services.
Definition: Services.php:38
Class ilCtrl provides processing control methods.
global $DIC
Definition: feed.php:28
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...
$response