ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ImportUploadHandlerGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
29 
35 {
37  protected array $supported_mime_types;
38 
39  public function __construct()
40  {
42  global $DIC;
43  $this->temp_system = $DIC->filesystem()->temp();
44  $this->supported_mime_types = ilObjectGUI::SUPPORTED_IMPORT_MIME_TYPES;
46  }
47 
51  final protected function getUploadResult(): HandlerResult
52  {
53  $this->upload->process();
54 
55  $result_array = $this->upload->getResults();
56  $result = end($result_array);
57 
58  $tempname = '';
59  if ($result instanceof UploadResult
60  && in_array($result->getMimeType(), $this->supported_mime_types)
61  && $result->isOK()) {
62  $status = HandlerResult::STATUS_OK;
63  $message = 'Upload ok';
64  $tempname = $this->moveUploadedFileToTemp($result);
65  } else {
66  $status = HandlerResult::STATUS_FAILED;
67  $message = $result->getStatus()->getMessage();
68  }
69 
70  return new BasicHandlerResult(
72  $status,
73  $tempname,
74  $message
75  );
76  }
77 
78  final protected function getRemoveResult(string $filename): HandlerResult
79  {
80  return new BasicHandlerResult(
82  HandlerResult::STATUS_OK,
83  $filename,
84  "We just don't do anything here."
85  );
86  }
87 
88  final public function getInfoResult(string $file_name): ?FileInfoResult
89  {
90  if ($this->temp_system->hasDir($file_name)
91  && ($files = $this->temp_system->listContents($file_name))) {
92  $path = $files[0]->getPath();
93  $filename = basename($path);
94  $title = $filename;
95  $mime = $this->temp_system->getMimeType($path);
96  $size = intval($this->temp_system->getSize($path, DataSize::Byte)->inBytes());
97 
98  } else {
99  $title = $mime = 'unknown';
100  $size = 0;
101  }
102 
103  return new BasicFileInfoResult(
105  $file_name,
106  $title,
107  $size,
108  $mime
109  );
110  }
111 
115  final public function getInfoForExistingFiles(array $file_names): array
116  {
117  return [$this->getInfoResult($file_names[0])];
118  }
119 
120  final protected function moveUploadedFileToTemp(UploadResult $result): string
121  {
122  $tempfile_path = uniqid('tmp');
123  $this->temp_system->createDir($tempfile_path);
124  $this->temp_system->put(
125  $tempfile_path . DIRECTORY_SEPARATOR . ilFileUtils::getValidFilename($result->getName()),
126  file_get_contents($result->getPath())
127  );
128  return $tempfile_path;
129  }
130 }
moveUploadedFileToTemp(UploadResult $result)
const SUPPORTED_IMPORT_MIME_TYPES
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getValidFilename(string $a_filename)
$path
Definition: ltiservices.php:29
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:22
$filename
Definition: buildRTE.php:78
__construct(Container $dic, ilPlugin $plugin)
$message
Definition: xapiexit.php:31