ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
MediaObjectManager.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\MediaObjects;
22
31
33{
34 protected \ilLogger $logger;
38
39 public function __construct(
40 protected InternalDataService $data,
42 protected InternalDomainService $domain,
43 protected \ilMobStakeholder $stakeholder
44 ) {
45 $this->repo = $repo->mediaObject();
46 $this->image_converters = new Images(true);
47 $this->output_options = new ImageOutputOptions();
48 $this->logger = \ilLoggerFactory::getLogger('mob');
49 }
50
51 public function create(
52 int $id,
53 string $title,
54 int $from_mob_id = 0
55 ): void {
56 $this->repo->create(
57 $id,
58 $title,
59 $this->stakeholder,
60 $from_mob_id
61 );
62 }
63
64 public function addLocalDirectory(int $mob_id, string $dir): void
65 {
66 $this->repo->addLocalDirectory($mob_id, $dir);
67 }
68
69 public function addFileFromLegacyUpload(int $mob_id, string $tmp_name, string $target_path = ""): void
70 {
71 $this->repo->addFileFromLegacyUpload($mob_id, $tmp_name, $target_path);
72 }
73
74 public function addFileFromUpload(
75 int $mob_id,
76 UploadResult $result,
77 string $path = "/"
78 ): void {
79 $this->repo->addFileFromUpload($mob_id, $result, $path);
80 }
81
82 public function addFileFromLocal(int $mob_id, string $tmp_name, string $path): void
83 {
84 $this->repo->addFileFromLocal($mob_id, $tmp_name, $path);
85 }
86
87 public function removeLocation(
88 int $mob_id,
89 string $location
90 ): void {
91 $this->repo->removeLocation($mob_id, $location);
92 }
93
94 public function getLocationStream(
95 int $mob_id,
96 string $location
97 ): ZIPStream {
98 return $this->repo->getLocationStream($mob_id, $location);
99 }
100
101 public function addStream(
102 int $mob_id,
103 string $location,
104 FileStream $stream
105 ): void {
106 $this->repo->addStream($mob_id, $location, $stream);
107 }
108
109 public function getLocalSrc(int $mob_id, string $location): string
110 {
111 $src = $this->repo->getLocalSrc(
112 $mob_id,
114 );
115 if ($src === "") { // fallback: old source
116 $path_to_file = \ilObjMediaObject::_getURL($mob_id) . "/" . $location;
117 try {
118 $src = \ilWACSignedPath::signFile($path_to_file);
119 } catch (\Exception $e) {
120 }
121 }
122 return $src;
123 }
124
125 public function hasLocalFile(int $mob_id, string $location): bool
126 {
127 return $this->repo->hasLocalFile($mob_id, $location);
128 }
129
130 public function getContainerResource(
131 int $mob_id
132 ): ?StorableResource {
133 return $this->repo->getContainerResource($mob_id);
134 }
135
136 public function getContainerResourceId(
137 int $mob_id
139 return $this->repo->getContainerResourceId($mob_id);
140 }
141
142 public function getFilesOfPath(
143 int $mob_id,
144 string $dir_path
145 ): array {
146 return $this->repo->getFilesOfPath($mob_id, $dir_path);
147 }
148
149 public function getInfoOfEntry(
150 int $mob_id,
151 string $path
152 ): array {
153 return $this->repo->getInfoOfEntry(
154 $mob_id,
155 $path
156 );
157 }
158
159 public function deliverEntry(
160 int $mob_id,
161 string $path
162 ): void {
163 $this->repo->deliverEntry($mob_id, $path);
164 }
165
166
167 public function generatePreview(
168 int $mob_id,
169 string $location,
170 bool $local,
171 string $format,
172 int $sec = 1,
173 string $target_location = "mob_vpreview.png"
174 ): void {
175
176 $is_image = is_int(strpos($format, "image/"));
177 $is_video = in_array($format, ["video/mp4", "video/webm"]);
178
179 if ($local) {
180 if ($is_image) {
182 $image_quality = 60;
183
184 // the zip stream is not seekable, which is needed by Imagick
185 // so we create a seekable stream first
186 $tempStream = fopen('php://temp', 'w+');
187 stream_copy_to_stream($this->repo->getLocationStream($mob_id, $location)->detach(), $tempStream);
188 rewind($tempStream);
189 $stream = new Stream($tempStream);
190
191 $converter = $this->image_converters->resizeToFixedSize(
192 $stream,
193 $width,
194 $height,
195 true,
196 $this->output_options
197 ->withQuality($image_quality)
198 ->withFormat(ImageOutputOptions::FORMAT_PNG)
199 );
200 $this->repo->addStream(
201 $mob_id,
202 $target_location,
203 $converter->getStream()
204 );
205 fclose($tempStream);
206 }
207 if ($is_video) {
208 $zip_uri = $this->repo->getContainerPath($mob_id);
210 $zip_uri,
211 $location,
212 $sec
213 );
214 $png_res = fopen('php://memory', 'r+');
215 fwrite($png_res, $image_str);
216 rewind($png_res);
217 $png_stream = new Stream($png_res);
218 $this->repo->addStream(
219 $mob_id,
220 $target_location,
221 $png_stream
222 );
223 }
224 }
225 }
226
227 public function addPreviewFromUrl(
228 int $mob_id,
229 string $url,
230 string $target_location
231 ): void {
232 $log = $this->logger;
233 try {
234 $log->debug('Trying to fetch thumbnail from URL: {thumbnail_url}', [
235 'thumbnail_url' => $url,
236 ]);
237 $curl = new \ilCurlConnection($url);
238 $curl->init(true);
239 $curl->setOpt(CURLOPT_RETURNTRANSFER, true);
240 $curl->setOpt(CURLOPT_VERBOSE, true);
241 $curl->setOpt(CURLOPT_FOLLOWLOCATION, true);
242 $curl->setOpt(CURLOPT_TIMEOUT_MS, 5000);
243 $curl->setOpt(CURLOPT_TIMEOUT, 5);
244 $curl->setOpt(CURLOPT_FAILONERROR, true);
245 $curl->setOpt(CURLOPT_SSL_VERIFYPEER, 1);
246 $curl->setOpt(CURLOPT_SSL_VERIFYHOST, 2);
247
248 $str = $curl->exec();
249 $info = $curl->getInfo();
250
251 $log->debug('cURL Info: {info}', [
252 'info' => print_r($info, true)
253 ]);
254
255 $status = $info['http_code'] ?? '';
256 if ((int) $status === 200) {
257 $log->debug('Successfully fetched preview file from URL: Received {bytes} bytes', [
258 'bytes' => (string) strlen($str),
259 ]);
260 } else {
261 $log->error('Could not fetch thumbnail from YouTube: {thumbnail_url}', [
262 'thumbnail_url' => $url,
263 ]);
264 }
265
266 $res = fopen('php://memory', 'r+');
267 fwrite($res, $str);
268 rewind($res);
269 $stream = new Stream($res);
270 $this->repo->addStream(
271 $mob_id,
272 $target_location,
273 $stream
274 );
275 } catch (\Exception $e) {
276 $log->error('Could not fetch thumbnail from Url: {message}', [
277 'message' => $e->getMessage(),
278 ]);
279 $log->error($e->getTraceAsString());
280 }
281 }
282
283 public function getSrtFiles(int $mob_id, bool $vtt_only = false): array
284 {
285 $srt_files = [];
286 $valid_suffixes = $vtt_only
287 ? ["vtt"]
288 : ["srt", "vtt"];
289 foreach ($this->getFilesOfPath($mob_id, "/srt") as $i) {
290 $name = explode(".", $i["basename"]);
291 if (in_array($name[1], $valid_suffixes) && substr($name[0], 0, 9) == "subtitle_") {
292 $srt_files[] = [
293 "file" => $i["basename"],
294 "full_path" => $i["path"],
295 "src" => $this->getLocalSrc($mob_id, $i["path"]),
296 "language" => substr($name[0], 9, 2)
297 ];
298 }
299 }
300 return $srt_files;
301 }
302
303 public function generateMissingVTT(int $mob_id): void
304 {
305 $names = array_map(static function (array $i) {
306 return $i["file"];
307 }, $this->getSrtFiles($mob_id));
308 $missing_vtt = [];
309 foreach ($names as $name) {
310 if (str_ends_with($name, ".srt")) {
311 $vtt = str_replace(".srt", ".vtt", $name);
312 if (!in_array($vtt, $names) && !in_array($vtt, $missing_vtt)) {
313 $missing_vtt[] = $vtt;
314 }
315 }
316 }
317 foreach ($missing_vtt as $vtt_name) {
318 $srt_name = str_replace(".vtt", ".srt", $vtt_name);
319 $srt_content = stream_get_contents($this->repo->getLocationStream($mob_id, "srt/" . $srt_name)->detach());
320 $vtt_content = $this->srtToVtt($srt_content);
321 $this->repo->addString($mob_id, "/srt/" . $vtt_name, $vtt_content);
322 }
323 }
324
325 public function srtToVtt(string $srt_text): string
326 {
327 // Remove UTF-8 BOM if present
328 $srt_text = preg_replace('/^\xEF\xBB\xBF/', '', $srt_text);
329
330 // Normalise line-endings and split cues
331 $srt_text = preg_replace('~\r\n?~', "\n", $srt_text);
332 $blocks = preg_split("/\n{2,}/", trim($srt_text));
333
334 $vttLines = ['WEBVTT', '']; // header + blank line
335
336 foreach ($blocks as $block) {
337 $lines = explode("\n", $block);
338
339 if (count($lines) < 2) {
340 continue; // malformed cue
341 }
342
343 /* cue number? allow BOM or spaces either side */
344 if (preg_match('/^\s*\d+\s*$/u', $lines[0])) {
345 array_shift($lines); // drop it
346 }
347
348 /* now $lines[0] *is* the time-code line → , → . */
349 $lines[0] = preg_replace(
350 '/(\d{2}:\d{2}:\d{2}),(\d{3})/',
351 '$1.$2',
352 $lines[0]
353 );
354
355 $vttLines = array_merge($vttLines, $lines, ['']);
356 }
357
358 return implode("\n", $vttLines);
359 }
360
361 public function getLastChangeTimestamp(int $mob_id): int
362 {
363 return $this->repo->getLastChangeTimestamp($mob_id);
364 }
365
366 public function updateLastChange(int $mob_id): void
367 {
368 $this->repo->updateLastChangeTimestamp($mob_id, time());
369 }
370}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$location
Definition: buildRTE.php:22
create(int $id, string $title, int $from_mob_id=0)
addFileFromUpload(int $mob_id, UploadResult $result, string $path="/")
getLocalSrc(int $mob_id, string $location)
addFileFromLegacyUpload(int $mob_id, string $tmp_name, string $target_path="")
hasLocalFile(int $mob_id, string $location)
addLocalDirectory(int $mob_id, string $dir)
getInfoOfEntry(int $mob_id, string $path)
deliverEntry(int $mob_id, string $path)
addFileFromLocal(int $mob_id, string $tmp_name, string $path)
getLocationStream(int $mob_id, string $location)
generatePreview(int $mob_id, string $location, bool $local, string $format, int $sec=1, string $target_location="mob_vpreview.png")
addPreviewFromUrl(int $mob_id, string $url, string $target_location)
__construct(protected InternalDataService $data, InternalRepoService $repo, protected InternalDomainService $domain, protected \ilMobStakeholder $stakeholder)
addStream(int $mob_id, string $location, FileStream $stream)
removeLocation(int $mob_id, string $location)
getFilesOfPath(int $mob_id, string $dir_path)
getSrtFiles(int $mob_id, bool $vtt_only=false)
static extractPNGFromVideoInZip(string $zip, string $path, int $sec=1)
static getLogger(string $a_component_id)
Get component logger.
static _getURL(int $a_mob_id)
get directory for files of media object
static signFile(string $path_to_file)
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$info
Definition: entry_point.php:21
The base interface for all filesystem streams.
Definition: FileStream.php:32
$log
Definition: ltiresult.php:34
$path
Definition: ltiservices.php:30
$res
Definition: ltiservices.php:69
$url
Definition: shib_logout.php:68