ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilFileVersionsUploadHandlerGUI.php
Go to the documentation of this file.
1 <?php
2 
24 
33 {
34  public const MODE_APPEND = 'append';
35  public const MODE_REPLACE = 'replace';
36  public const P_UPLOAD_MODE = 'upload_mode';
37  protected bool $append;
38  protected string $upload_mode = self::MODE_APPEND;
39 
40  public function __construct(protected ilObjFile $file, string $upload_mode = self::MODE_APPEND)
41  {
42  global $DIC;
44  $this->upload_mode = $this->http->wrapper()->query()->has(self::P_UPLOAD_MODE)
45  ? $this->http->wrapper()->query()->retrieve(self::P_UPLOAD_MODE, $DIC->refinery()->kindlyTo()->string())
46  : $upload_mode;
47 
48  $this->ctrl->setParameter($this, self::P_UPLOAD_MODE, $this->upload_mode);
49  }
50 
51  protected function getStakeholder(): ResourceStakeholder
52  {
53  global $DIC;
54  return new ilObjFileStakeholder($DIC->user()->getId());
55  }
56 
57  protected function getClassPath(): array
58  {
59  return [self::class];
60  }
61 
62  public function supportsChunkedUploads(): bool
63  {
64  return true;
65  }
66 
67  protected function getUploadResult(): HandlerResult
68  {
69  $this->upload->register(new ilCountPDFPagesPreProcessors());
70  $this->upload->process();
71  $upload_mode =
72 
73  $result_array = $this->upload->getResults();
74  $result = end($result_array);
75 
76  if ($result instanceof UploadResult && $result->isOK()) {
77  if ($this->is_chunked) {
78  return $this->processChunckedUpload($result);
79  }
80 
81  if ($this->upload_mode === self::MODE_REPLACE) {
82  $identifier = (string) $this->file->replaceWithUpload($result, $result->getName());
83  } else {
84  $identifier = (string) $this->file->appendUpload($result, $result->getName());
85  }
86  $status = HandlerResult::STATUS_OK;
87  $message = "file upload OK";
88  } else {
89  $identifier = '';
90  $status = HandlerResult::STATUS_FAILED;
91  $message = $result->getStatus()->getMessage();
92  }
93 
94  return new BasicHandlerResult(
96  $status,
97  $identifier,
98  $message
99  );
100  }
101 
102  protected function processChunckedUpload(UploadResult $result): HandlerResult
103  {
104  $temp_path = "$this->chunk_id/{$result->getName()}";
105 
106  try {
107  if ($this->temp_filesystem->has($temp_path)) {
108  $stream = fopen($this->temp_filesystem->readStream($temp_path)->getMetadata()['uri'], 'ab');
109  fwrite($stream, file_get_contents($result->getPath()));
110  } else {
111  $this->temp_filesystem->write($temp_path, file_get_contents($result->getPath()));
112  }
113  } catch (Throwable $t) {
114  return new BasicHandlerResult(
116  HandlerResult::STATUS_FAILED,
117  '',
118  $t->getMessage()
119  );
120  }
121 
122  if (($this->chunk_index + 1) === $this->amount_of_chunks) {
123  $whole_file = $this->temp_filesystem->readStream($temp_path);
124  if ($this->upload_mode === self::MODE_REPLACE) {
125  $revision_number = $this->file->replaceWithStream($whole_file, $result->getName());
126  } else {
127  $revision_number = $this->file->appendStream($whole_file, $result->getName());
128  }
129 
130  return new BasicHandlerResult(
132  HandlerResult::STATUS_OK,
133  (string) $revision_number,
134  "file upload OK"
135  );
136  }
137 
138  return new BasicHandlerResult(
140  HandlerResult::STATUS_PARTIAL,
141  '',
142  "chunk upload OK"
143  );
144  }
145 
146 }
Class ilObjFileStakeholder.
__construct(protected ilObjFile $file, string $upload_mode=self::MODE_APPEND)
global $DIC
Definition: feed.php:28
static http()
Fetches the global http state from ILIAS.
__construct(VocabulariesInterface $vocabularies)
Class ilFileVersionsUploadHandlerGUI.
Class ilObjFile.
$message
Definition: xapiexit.php:32
Class ilCountPDFPagesPreProcessors.