ILIAS  release_8 Revision v8.24
class.ilCertificateBackgroundImageUpload.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
28
33{
34 private const BACKGROUND_IMAGE_NAME = 'background.jpg';
35 private const BACKGROUND_THUMBNAIL_IMAGE_NAME = 'background.jpg.thumb.jpg';
36 private const BACKGROUND_TEMPORARY_FILENAME = 'background_upload_tmp';
38 private string $certificatePath;
40 private string $rootDirectory;
44 private string $clientId;
48
49 public function __construct(
51 string $certificatePath,
54 ?Filesystem $fileSystem = null,
56 ?ilCertificateFileUtilsHelper $certificateFileUtilsHelper = null,
59 string $clientID = CLIENT_ID,
61 ) {
62 $this->fileUpload = $fileUpload;
63 $this->certificatePath = $certificatePath;
64 $this->language = $language;
65 $this->logger = $logger;
66 $this->rootDirectory = $rootDirectory;
67
68 if (null === $fileSystem) {
69 global $DIC;
70 $fileSystem = $DIC->filesystem()->web();
71 }
72 $this->fileSystem = $fileSystem;
73
74 if (null === $utilHelper) {
76 }
77 $this->utilHelper = $utilHelper;
78
79 if (null === $certificateFileUtilsHelper) {
80 $certificateFileUtilsHelper = new ilCertificateFileUtilsHelper();
81 }
82 $this->fileUtilsHelper = $certificateFileUtilsHelper;
83
84 if (null === $legacyPathHelper) {
86 }
87 $this->legacyPathHelper = $legacyPathHelper;
88
89 $this->clientId = $clientID;
90
91 if (null === $tmp_file_system) {
92 global $DIC;
93 $tmp_file_system = $DIC->filesystem()->temp();
94 }
95 $this->tmp_file_system = $tmp_file_system;
96 }
97
111 public function uploadBackgroundImage(string $imageTempFilename, int $version, ?array $pending_file = null): string
112 {
113 $imagepath = $this->rootDirectory . $this->certificatePath;
114
115 if (!$this->fileSystem->hasDir($imagepath)) {
116 ilFileUtils::makeDirParents($imagepath);
117 }
118
119 $backgroundImageTempFilePath = $this->uploadFile($imageTempFilename, $pending_file);
120
121 $backgroundImagePath = $this->certificatePath . 'background_' . $version . '.jpg';
122
123 $this->utilHelper->convertImage(
124 $backgroundImageTempFilePath,
125 $this->rootDirectory . $backgroundImagePath,
126 'JPEG'
127 );
128
129 $backgroundImageThumbnailPath = $this->createBackgroundImageThumbPath();
130
131 $this->utilHelper->convertImage(
132 $backgroundImageTempFilePath,
133 $backgroundImageThumbnailPath,
134 'JPEG',
135 "100"
136 );
137
138 $convert_filename = self::BACKGROUND_IMAGE_NAME;
139
140 // something went wrong converting the file. use the original file and hope, that PDF can work with it
141 if (!$this->fileSystem->has($backgroundImagePath) && !ilFileUtils::moveUploadedFile(
142 $backgroundImageTempFilePath,
143 $convert_filename,
144 $this->rootDirectory . $backgroundImagePath
145 )) {
146 throw new ilException('Unable to convert the file and the original file');
147 }
148
149 if ($this->fileSystem->has($backgroundImageTempFilePath)) {
150 $this->fileSystem->delete($backgroundImageTempFilePath);
151 }
152
153 if ($this->fileSystem->has($backgroundImagePath)) {
154 return $this->certificatePath . 'background_' . $version . '.jpg';
155 }
156
157 throw new ilException('The given temporary filename is empty');
158 }
159
169 private function uploadFile(string $temporaryFilename, ?array $pending_file = null): string
170 {
171 if (!$this->fileUpload->hasBeenProcessed()) {
172 $this->fileUpload->process();
173 }
174
175 if (false === $this->fileUpload->hasUploads()) {
176 throw new ilException($this->language->txt('upload_error_file_not_found'));
177 }
178
182 $uploadResults = $this->fileUpload->getResults();
183 if (isset($uploadResults[$temporaryFilename])) {
184 $uploadResult = $uploadResults[$temporaryFilename];
185 $processingStatus = $uploadResult->getStatus();
186 if ($processingStatus->getCode() === ILIAS\FileUpload\DTO\ProcessingStatus::REJECTED) {
187 throw new ilException($processingStatus->getMessage());
188 }
189
190 $extension = pathinfo($uploadResult->getName(), PATHINFO_EXTENSION);
191 $temp_file_path = $this->createBackgroundImageTempfilePath($extension);
192 $target_file_name = basename($temp_file_path);
193 $target_file_name = $this->fileUtilsHelper->getValidFilename($target_file_name);
194
195 $target_file_system = $this->getTargetFilesystem($temp_file_path);
196 $target_directory = $this->getTargetDir($temp_file_path);
197
198 $this->fileUpload->moveOneFileTo(
199 $uploadResult,
200 $target_directory,
201 $target_file_system,
202 $target_file_name,
203 true
204 );
205
206 return $temp_file_path;
207 } elseif (is_array($pending_file) && $pending_file !== []) {
208 $extension = pathinfo($pending_file['name'], PATHINFO_EXTENSION);
209 $temp_file_path = $this->createBackgroundImageTempfilePath($extension);
210
211 $target_file_name = basename($temp_file_path);
212 $target_file_name = $this->fileUtilsHelper->getValidFilename($target_file_name);
213
214 $target_directory = $this->getTargetDir($temp_file_path);
215
216 $stream = $this->tmp_file_system->readStream(basename($pending_file['tmp_name']));
217 $this->fileSystem->writeStream($target_directory . '/' . $target_file_name, $stream);
218
219 return $temp_file_path;
220 } else {
221 throw new ilException($this->language->txt('upload_error_file_not_found'));
222 }
223 }
224
225 private function getTargetFilesystem(string $target): int
226 {
227 switch (true) {
228 case strpos($target, $this->rootDirectory . '/' . $this->clientId) === 0:
229 case strpos($target, './' . $this->rootDirectory . '/' . $this->clientId) === 0:
230 case strpos($target, $this->rootDirectory) === 0:
231 $targetFilesystem = Location::WEB;
232 break;
233 case strpos($target, CLIENT_DATA_DIR . "/temp") === 0:
234 $targetFilesystem = Location::TEMPORARY;
235 break;
236 case strpos($target, CLIENT_DATA_DIR) === 0:
237 $targetFilesystem = Location::STORAGE;
238 break;
239 case strpos($target, ILIAS_ABSOLUTE_PATH . '/Customizing') === 0:
240 $targetFilesystem = Location::CUSTOMIZING;
241 break;
242 default:
243 throw new InvalidArgumentException("Can not move files to \"$target\" because path can not be mapped to web, storage or customizing location.");
244 }
245
246 return $targetFilesystem;
247 }
248
249 private function getTargetDir(string $target): string
250 {
251 $absTargetDir = dirname($target);
252 return $this->legacyPathHelper->createRelativePath($absTargetDir);
253 }
254
259 private function createBackgroundImageTempfilePath(string $extension): string
260 {
261 return implode('', [
262 $this->rootDirectory,
263 $this->certificatePath,
264 self::BACKGROUND_TEMPORARY_FILENAME,
265 '.' . $extension
266 ]);
267 }
268
273 private function createBackgroundImageThumbPath(): string
274 {
275 return $this->rootDirectory . $this->certificatePath . self::BACKGROUND_THUMBNAIL_IMAGE_NAME;
276 }
277}
$version
Definition: plugin.php:24
__construct(FileUpload $fileUpload, string $certificatePath, ilLanguage $language, ilLogger $logger, ?Filesystem $fileSystem=null, ?ilCertificateUtilHelper $utilHelper=null, ?ilCertificateFileUtilsHelper $certificateFileUtilsHelper=null, ?LegacyPathHelperHelper $legacyPathHelper=null, string $rootDirectory=CLIENT_WEB_DIR, string $clientID=CLIENT_ID, ?Filesystem $tmp_file_system=null)
createBackgroundImageTempfilePath(string $extension)
Returns the filesystem path of the background image temp file during upload.
uploadBackgroundImage(string $imageTempFilename, int $version, ?array $pending_file=null)
Uploads a background image for the certificate.
createBackgroundImageThumbPath()
Returns the filesystem path of the background image thumbnail.
Just a wrapper class to create Unit Test for other classes.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
static moveUploadedFile(string $a_file, string $a_name, string $a_target, bool $a_raise_errors=true, string $a_mode="move_uploaded")
move uploaded file
language handling
Component logger with individual log levels by component id.
const CLIENT_ID
Definition: constants.php:41
const CLIENT_WEB_DIR
Definition: constants.php:47
const CLIENT_DATA_DIR
Definition: constants.php:46
global $DIC
Definition: feed.php:28
Interface Location.
Definition: Location.php:30
Interface Filesystem.
Definition: Filesystem.php:40
Class FlySystemFileAccessTest \Provider\FlySystem @runTestsInSeparateProcesses @preserveGlobalState d...
Class ChatMainBarProvider \MainMenu\Provider.