ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
FileDataRCHandling.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
24
25trait FileDataRCHandling
26{
30 protected function getCurrentCollection(
31 array $path_to_files,
33 ): \ILIAS\ResourceStorage\Collection\ResourceCollection {
34 set_error_handler(static function ($severity, $message, $file, $line): void {
35 throw new ErrorException($message, $severity, 0, $file, $line);
36 });
37
38 try {
39 global $DIC;
40 $file_system = $DIC->filesystem()->storage();
41 $rcid = $this->storage->collection()->id();
42 $collection = $this->storage->collection()->get($rcid);
43 foreach ($path_to_files as $path_to_file) {
44 $base_dir = (new \ILIAS\FileDelivery\Setup\BaseDirObjective())::get();
45 $path_to_file = str_replace($base_dir, '/', $path_to_file);
46 $rid = $this->storage->manage()->stream(
47 $file_system->readStream($path_to_file),
48 $stakeholder,
49 md5(basename($path_to_file))
50 );
51 $collection->add($rid);
52 }
53 $this->storage->collection()->store($collection);
54 } catch (Exception $e) {
55 throw new Exception("Storing file into collection failed: " . $e->getMessage());
56 } finally {
57 restore_error_handler();
58 }
59
60 return $collection;
61 }
62
67 public function filesFromLegacyToIRSS(array $mail_data): array
68 {
69 $files = [];
70 $path_to_files = [];
71 foreach ($mail_data['attachments'] as $file) {
72 $path_to_files[] = $this->fdm->getAbsoluteAttachmentPoolPathByFilename($file);
73 }
74 $collection = $this->getCurrentCollection($path_to_files, new ilMailAttachmentStakeholder());
75 foreach ($collection->getResourceIdentifications() as $rcid) {
76 $files[] = $rcid->serialize();
77 }
78
79 return $files;
80 }
81
85 public function getIdforCollection(array $mail_data): ?ResourceCollectionIdentification
86 {
87 $files = [];
88 $path_to_files = [];
89 foreach ($mail_data as $attachment) {
90 $path_to_files[] = $this->fdm->getAbsoluteAttachmentPoolPathByFilename($attachment);
91 }
92 $collection = $this->getCurrentCollection($path_to_files, new ilMailAttachmentStakeholder());
93 $rcid = $collection->getIdentification();
94
95 return $rcid;
96 }
97
101 public function FilesFromIRSSToLegacy(ResourceCollectionIdentification $identification): array
102 {
103 $files = [];
104 $collection = $this->storage->collection()->get($identification);
105 $all_ids = $collection->getResourceIdentifications();
106 foreach ($all_ids as $id) {
107 $files[] = $id->serialize();
108 }
109
110 return $files;
111 }
112
117 protected function handleAttachments(array $attachments): array
118 {
119 $files = [];
120 foreach ($attachments as $attachment) {
121 $info = $this->upload_handler->getInfoResult($attachment);
122 if ($info->getFileIdentifier() !== 'unknown') {
123 $src = $this->upload_handler->getStreamConsumer($attachment);
124 $stored = $this->fdm->storeAsAttachment(
125 $info->getName(),
126 (string) $src->getStream()
127 );
128 if ($stored === false) {
129 throw new Exception("File '" . $info->getName() . "' could not be stored");
130 }
131 $files[] = ilFileUtils::_sanitizeFilemame($info->getName());
132 $this->upload_handler->removeFileForIdentifier($attachment);
133 }
134 }
135
136 return $files;
137 }
138}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
static _sanitizeFilemame(string $a_filename)
$info
Definition: entry_point.php:21
get(string $class_name)
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $DIC
Definition: shib_login.php:26