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