ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ImageFileRepo.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Style\Content;
22 
26 use Generator;
33 
35 {
36  protected const DIR_PATH = "sty/sty_%id%/images";
37 
40  protected FileUpload $upload;
41 
42  public function __construct(
43  InternalDataService $factory,
44  Filesystem\Filesystem $web_files,
45  FileUpload $upload,
46  protected IRSSWrapper $irss
47  ) {
48  $this->web_files = $web_files;
49  $this->factory = $factory;
50  $this->upload = $upload;
51  }
52 
53  // get image directory
54  protected function dir(int $style_id): string
55  {
56  return str_replace("%id%", (string) $style_id, self::DIR_PATH);
57  }
58 
65  public function getImages(
66  int $style_id,
67  string $rid
68  ): Generator {
69  if ($rid !== "") {
70  $unzip = $this->irss->getContainerZip($rid);
71  $uri = $this->irss->stream($rid)->getMetadata("uri");
72  $zip_archive = new \ZipArchive();
73  $zip_archive->open($uri, \ZipArchive::RDONLY);
74 
75  foreach ($unzip->getPaths() as $path) {
76  if (str_starts_with($path, ".")) {
77  continue;
78  }
79  if (!str_starts_with($path, "images")) {
80  continue;
81  }
82  if (!in_array(strtolower(pathinfo($path, PATHINFO_EXTENSION)), ["jpg", "png", "gif", "svg"])) {
83  continue;
84  }
85  $att = $zip_archive->statName($path);
86  $full_path = $this->irss->getContainerUri($rid, $path);
87  try {
88  $image_size = getimagesize($full_path);
89  } catch (\Exception $e) {
90  }
91  $width = $image_size[0] ?? 0;
92  $height = $image_size[1] ?? 0;
93  yield $this->factory->image(
94  $this->irss->getContainerUri($rid, $path),
95  new DataSize($att["size"], DataSize::KB),
96  $width,
97  $height
98  );
99  }
100  }
101 
102 
103  $dir = $this->dir($style_id);
104  if ($this->web_files->hasDir($dir)) {
105  foreach ($this->web_files->listContents($dir) as $meta) {
106  if ($meta->isFile()) {
107  $size = $this->web_files->getSize(
108  $meta->getPath(),
110  );
111  $image_size = getimagesize($this->getWebPath($meta->getPath()));
112  $width = $image_size[0] ?? 0;
113  $height = $image_size[1] ?? 0;
114  yield $this->factory->image(
115  $meta->getPath(),
116  $size,
117  $width,
118  $height
119  );
120  }
121  }
122  }
123  }
124 
125  public function getImageStream(
126  string $rid,
127  string $image
128  ): ZIPStream {
129  return $this->irss->getStreamOfContainerEntry(
130  $rid,
131  "images/" . $image
132  );
133  }
134 
135  public function addStream(
136  string $rid,
137  string $image,
138  FileStream $stream
139  ): void {
140  $this->irss->addStreamToContainer(
141  $rid,
142  $stream,
143  "images/" . $image
144  );
145  }
146 
147  // get full web path for relative file path
148  public function getWebPath(string $path): string
149  {
150  if (str_starts_with($path, "http")) {
151  return $path;
152  }
153  return ILIAS_WEB_DIR . "/" . CLIENT_ID . "/" . $path;
154  }
155 
156  // get image data object by filename
157  public function getByFilename(int $style_id, string $rid, string $filename): ?Image
158  {
160  foreach ($this->getImages($style_id, $rid) as $i) {
161  if ($i->getFilename() == $filename) {
162  return $i;
163  }
164  }
165  return null;
166  }
167 
168  // delete image
169  public function deleteImageByFilename(int $style_id, string $filename): void
170  {
171  $dir = $this->dir($style_id);
172  $this->web_files->delete($dir . "/" . $filename);
173  }
174 
175  public function importFromUploadResult(
176  string $rid,
177  UploadResult $result,
178  ): void {
179  $this->irss->addUploadToContainer(
180  $rid,
181  $result
182  );
183  }
184 }
factory()
This class provides the data size with additional information to remove the work to calculate the siz...
Definition: DataSize.php:30
The filesystem interface provides the public interface for the Filesystem service API consumer...
Definition: Filesystem.php:36
$path
Definition: ltiservices.php:29
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
addStream(string $rid, string $image, FileStream $stream)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const CLIENT_ID
Definition: constants.php:41
Class FileUpload.
Definition: FileUpload.php:37
$filename
Definition: buildRTE.php:78
__construct(InternalDataService $factory, Filesystem\Filesystem $web_files, FileUpload $upload, protected IRSSWrapper $irss)
getImageStream(string $rid, string $image)
deleteImageByFilename(int $style_id, string $filename)
importFromUploadResult(string $rid, UploadResult $result,)
The base interface for all filesystem streams.
Definition: FileStream.php:31
const ILIAS_WEB_DIR
Definition: constants.php:45
getImages(int $style_id, string $rid)
Get images of style.