ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilCertificateBackgroundImageUpload.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 
32 {
33  private const BACKGROUND_IMAGE_NAME = 'background.jpg';
34  private const BACKGROUND_THUMBNAIL_IMAGE_NAME = 'background.jpg.thumb.jpg';
35  private const BACKGROUND_TEMPORARY_FILENAME = 'background_upload_tmp';
36  private readonly Filesystem $fileSystem;
40  private readonly Filesystem $tmp_file_system;
41 
42  public function __construct(
43  private readonly FileUpload $fileUpload,
44  private readonly string $certificatePath,
45  private readonly ilLanguage $language,
46  private readonly string $rootDirectory = CLIENT_WEB_DIR,
47  private readonly string $clientId = CLIENT_ID,
48  ?Filesystem $fileSystem = null,
49  ?ilCertificateUtilHelper $utilHelper = null,
50  ?ilCertificateFileUtilsHelper $certificateFileUtilsHelper = null,
51  ?LegacyPathHelperHelper $legacyPathHelper = null,
52  ?Filesystem $tmp_file_system = null
53  ) {
54  if (null === $fileSystem) {
55  global $DIC;
56  $fileSystem = $DIC->filesystem()->web();
57  }
58  $this->fileSystem = $fileSystem;
59 
60  if (null === $utilHelper) {
61  $utilHelper = new ilCertificateUtilHelper();
62  }
63  $this->utilHelper = $utilHelper;
64 
65  if (null === $certificateFileUtilsHelper) {
66  $certificateFileUtilsHelper = new ilCertificateFileUtilsHelper();
67  }
68  $this->fileUtilsHelper = $certificateFileUtilsHelper;
69 
70  if (null === $legacyPathHelper) {
71  $legacyPathHelper = new LegacyPathHelperHelper();
72  }
73  $this->legacyPathHelper = $legacyPathHelper;
74 
75  if (null === $tmp_file_system) {
76  global $DIC;
77  $tmp_file_system = $DIC->filesystem()->temp();
78  }
79  $this->tmp_file_system = $tmp_file_system;
80  }
81 
94  public function uploadBackgroundImage(string $imageTempFilename, int $version, ?array $pending_file = null): string
95  {
96  $imagepath = $this->rootDirectory . $this->certificatePath;
97 
98  if (!$this->fileSystem->hasDir($imagepath)) {
99  ilFileUtils::makeDirParents($imagepath);
100  }
101 
102  $backgroundImageTempFilePath = $this->uploadFile($imageTempFilename, $pending_file);
103 
104  $backgroundImagePath = $this->certificatePath . 'background_' . $version . '.jpg';
105 
106  $this->utilHelper->convertImage(
107  $backgroundImageTempFilePath,
108  $this->rootDirectory . $backgroundImagePath
109  );
110 
111  $backgroundImageThumbnailPath = $this->createBackgroundImageThumbPath();
112 
113  $this->utilHelper->convertImage(
114  $backgroundImageTempFilePath,
115  $backgroundImageThumbnailPath,
116  "100"
117  );
118 
119  $convert_filename = self::BACKGROUND_IMAGE_NAME;
120 
121  // something went wrong converting the file. use the original file and hope, that PDF can work with it
122  if (!$this->fileSystem->has($backgroundImagePath) && !ilFileUtils::moveUploadedFile(
123  $backgroundImageTempFilePath,
124  $convert_filename,
125  $this->rootDirectory . $backgroundImagePath
126  )) {
127  throw new ilException('Unable to convert the file and the original file');
128  }
129 
130  if ($this->fileSystem->has($backgroundImageTempFilePath)) {
131  $this->fileSystem->delete($backgroundImageTempFilePath);
132  }
133 
134  if ($this->fileSystem->has($backgroundImagePath)) {
135  return $this->certificatePath . 'background_' . $version . '.jpg';
136  }
137 
138  throw new ilException('The given temporary filename is empty');
139  }
140 
148  private function uploadFile(string $temporaryFilename, ?array $pending_file = null): string
149  {
150  if (!$this->fileUpload->hasBeenProcessed()) {
151  $this->fileUpload->process();
152  }
153 
154  if (!$this->fileUpload->hasUploads()) {
155  throw new ilException($this->language->txt('upload_error_file_not_found'));
156  }
157 
158  $uploadResults = $this->fileUpload->getResults();
159  if (isset($uploadResults[$temporaryFilename])) {
160  $uploadResult = $uploadResults[$temporaryFilename];
161  $processingStatus = $uploadResult->getStatus();
162  if ($processingStatus->getCode() === ILIAS\FileUpload\DTO\ProcessingStatus::REJECTED) {
163  throw new ilException($processingStatus->getMessage());
164  }
165 
166  $extension = pathinfo($uploadResult->getName(), PATHINFO_EXTENSION);
167  $temp_file_path = $this->createBackgroundImageTempfilePath($extension);
168  $target_file_name = basename($temp_file_path);
169  $target_file_name = $this->fileUtilsHelper->getValidFilename($target_file_name);
170 
171  $target_file_system = $this->getTargetFilesystem($temp_file_path);
172  $target_directory = $this->getTargetDir($temp_file_path);
173 
174  $this->fileUpload->moveOneFileTo(
175  $uploadResult,
176  $target_directory,
177  $target_file_system,
178  $target_file_name,
179  true
180  );
181 
182  return $temp_file_path;
183  } elseif (is_array($pending_file) && $pending_file !== []) {
184  $extension = pathinfo($pending_file['name'], PATHINFO_EXTENSION);
185  $temp_file_path = $this->createBackgroundImageTempfilePath($extension);
186 
187  $target_file_name = basename($temp_file_path);
188  $target_file_name = $this->fileUtilsHelper->getValidFilename($target_file_name);
189 
190  $target_directory = $this->getTargetDir($temp_file_path);
191 
192  $stream = $this->tmp_file_system->readStream(basename($pending_file['tmp_name']));
193  $this->fileSystem->writeStream($target_directory . '/' . $target_file_name, $stream);
194 
195  return $temp_file_path;
196  } else {
197  throw new ilException($this->language->txt('upload_error_file_not_found'));
198  }
199  }
200 
201  private function getTargetFilesystem(string $target): int
202  {
203  return match (true) {
204  str_starts_with($target, $this->rootDirectory . '/' . $this->clientId), str_starts_with(
205  $target,
206  './' . $this->rootDirectory . '/' . $this->clientId
207  ), str_starts_with($target, $this->rootDirectory) => Location::WEB,
208  str_starts_with($target, CLIENT_DATA_DIR . "/temp") => Location::TEMPORARY,
209  str_starts_with($target, CLIENT_DATA_DIR) => Location::STORAGE,
210  str_starts_with($target, ILIAS_ABSOLUTE_PATH . '/Customizing') => Location::CUSTOMIZING,
211  default => throw new InvalidArgumentException(
212  "Can not move files to \"$target\" because path can not be mapped to web, storage or customizing location."
213  ),
214  };
215  }
216 
217  private function getTargetDir(string $target): string
218  {
219  $absTargetDir = dirname($target);
220  return $this->legacyPathHelper->createRelativePath($absTargetDir);
221  }
222 
227  private function createBackgroundImageTempfilePath(string $extension): string
228  {
229  return implode('', [
230  $this->rootDirectory,
231  $this->certificatePath,
232  self::BACKGROUND_TEMPORARY_FILENAME,
233  '.' . $extension
234  ]);
235  }
236 
241  private function createBackgroundImageThumbPath(): string
242  {
243  return $this->rootDirectory . $this->certificatePath . self::BACKGROUND_THUMBNAIL_IMAGE_NAME;
244  }
245 }
const REJECTED
Upload got rejected by a processor.
createBackgroundImageTempfilePath(string $extension)
Returns the filesystem path of the background image temp file during upload.
$clientId
Definition: ltiregend.php:27
readonly ilCertificateFileUtilsHelper $fileUtilsHelper
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
global $DIC
Definition: feed.php:28
const CLIENT_DATA_DIR
Definition: constants.php:46
uploadFile(string $temporaryFilename, ?array $pending_file=null)
const CLIENT_ID
Definition: constants.php:41
const CLIENT_WEB_DIR
Definition: constants.php:47
static moveUploadedFile(string $a_file, string $a_name, string $a_target, bool $a_raise_errors=true, string $a_mode="move_uploaded")
move uploaded file
Class FileUpload.
Definition: FileUpload.php:34
uploadBackgroundImage(string $imageTempFilename, int $version, ?array $pending_file=null)
Uploads a background image for the certificate.
Just a wrapper class to create Unit Test for other classes.
createBackgroundImageThumbPath()
Returns the filesystem path of the background image thumbnail.
$version
Definition: plugin.php:24
__construct(private readonly FileUpload $fileUpload, private readonly string $certificatePath, private readonly ilLanguage $language, private readonly string $rootDirectory=CLIENT_WEB_DIR, private readonly string $clientId=CLIENT_ID, ?Filesystem $fileSystem=null, ?ilCertificateUtilHelper $utilHelper=null, ?ilCertificateFileUtilsHelper $certificateFileUtilsHelper=null, ?LegacyPathHelperHelper $legacyPathHelper=null, ?Filesystem $tmp_file_system=null)