ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
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  #[\Override]
63  public function supportsChunkedUploads(): bool
64  {
65  return true;
66  }
67 
68  #[\Override]
69  protected function getUploadResult(): HandlerResult
70  {
71  $this->upload->register(new ilCountPDFPagesPreProcessors());
72  $this->upload->process();
73  $upload_mode =
74 
75  $result_array = $this->upload->getResults();
76  $result = end($result_array);
77 
78  if ($result instanceof UploadResult && $result->isOK()) {
79  if ($this->is_chunked) {
80  return $this->processChunckedUpload($result);
81  }
82 
83  if ($this->upload_mode === self::MODE_REPLACE) {
84  $identifier = (string) $this->file->replaceWithUpload($result, $result->getName());
85  } else {
86  $identifier = (string) $this->file->appendUpload($result, $result->getName());
87  }
88  $status = HandlerResult::STATUS_OK;
89  $message = "file upload OK";
90  } else {
91  $identifier = '';
92  $status = HandlerResult::STATUS_FAILED;
93  $message = $result->getStatus()->getMessage();
94  }
95 
96  return new BasicHandlerResult(
98  $status,
99  $identifier,
100  $message
101  );
102  }
103 
104  #[\Override]
105  protected function processChunckedUpload(UploadResult $result): HandlerResult
106  {
107  $temp_path = "$this->chunk_id/{$result->getName()}";
108 
109  try {
110  if ($this->temp_filesystem->has($temp_path)) {
111  $stream = fopen($this->temp_filesystem->readStream($temp_path)->getMetadata()['uri'], 'ab');
112  fwrite($stream, file_get_contents($result->getPath()));
113  } else {
114  $this->temp_filesystem->write($temp_path, file_get_contents($result->getPath()));
115  }
116  } catch (Throwable $t) {
117  return new BasicHandlerResult(
119  HandlerResult::STATUS_FAILED,
120  '',
121  $t->getMessage()
122  );
123  }
124 
125  if (($this->chunk_index + 1) === $this->amount_of_chunks) {
126  $whole_file = $this->temp_filesystem->readStream($temp_path);
127  if ($this->upload_mode === self::MODE_REPLACE) {
128  $revision_number = $this->file->replaceWithStream($whole_file, $result->getName());
129  } else {
130  $revision_number = $this->file->appendStream($whole_file, $result->getName());
131  }
132 
133  return new BasicHandlerResult(
135  HandlerResult::STATUS_OK,
136  (string) $revision_number,
137  "file upload OK"
138  );
139  }
140 
141  return new BasicHandlerResult(
143  HandlerResult::STATUS_PARTIAL,
144  '',
145  "chunk upload OK"
146  );
147  }
148 
149 }
Class ilObjFileStakeholder.
__construct(protected ilObjFile $file, string $upload_mode=self::MODE_APPEND)
static http()
Fetches the global http state from ILIAS.
Class ilFileVersionsUploadHandlerGUI.
Class ilObjFile.
global $DIC
Definition: shib_login.php:26
__construct(Container $dic, ilPlugin $plugin)
$message
Definition: xapiexit.php:31
Class ilCountPDFPagesPreProcessors.