ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ImageFileRepo.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
21 namespace ILIAS\Style\Content;
22 
26 use Generator;
29 
34 {
35  protected const DIR_PATH = "sty/sty_%id%/images";
36 
39  protected FileUpload $upload;
40 
41  public function __construct(
42  InternalDataService $factory,
43  Filesystem\Filesystem $web_files,
44  FileUpload $upload
45  ) {
46  $this->web_files = $web_files;
47  $this->factory = $factory;
48  $this->upload = $upload;
49  }
50 
51  // get image directory
52  protected function dir(int $style_id): string
53  {
54  return str_replace("%id%", (string) $style_id, self::DIR_PATH);
55  }
56 
63  public function getImages(
64  int $style_id
65  ): Generator {
66  $dir = $this->dir($style_id);
67  if ($this->web_files->hasDir($dir)) {
68  foreach ($this->web_files->listContents($dir) as $meta) {
69  if ($meta->isFile()) {
70  $size = $this->web_files->getSize(
71  $meta->getPath(),
73  );
74  $image_size = getimagesize($this->getWebPath($meta->getPath()));
75  $width = $image_size[0] ?? 0;
76  $height = $image_size[1] ?? 0;
77  yield $this->factory->image(
78  $meta->getPath(),
79  $size,
80  $width,
81  $height
82  );
83  }
84  }
85  }
86  }
87 
88  // get full web path for relative file path
89  public function getWebPath(string $path): string
90  {
91  return ILIAS_WEB_DIR . "/" . CLIENT_ID . "/" . $path;
92  }
93 
94  // get image data object by filename
95  public function getByFilename(int $style_id, string $filename): ?Image
96  {
98  foreach ($this->getImages($style_id) as $i) {
99  if ($i->getFilename() == $filename) {
100  return $i;
101  }
102  }
103  return null;
104  }
105 
111  public function uploadImage(int $style_id): void
112  {
113  $upload = $this->upload;
114  $dir = $this->dir($style_id);
115  if (!$this->web_files->hasDir($dir)) {
116  $this->web_files->createDir($dir);
117  }
118  if ($upload->hasUploads()) {
119  if (!$upload->hasBeenProcessed()) {
120  $upload->process();
121  }
122  $result = array_values($upload->getResults())[0];
123  if ($result->getStatus()->getCode() === ProcessingStatus::OK) {
124  $upload->moveFilesTo($dir, Location::WEB);
125  }
126  }
127  }
128 
129  // delete image
130  public function deleteImageByFilename(int $style_id, string $filename): void
131  {
132  $dir = $this->dir($style_id);
133  $this->web_files->delete($dir . "/" . $filename);
134  }
135 }
process()
Invokes all preprocessors for each uploaded file in the sequence they got registered.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
moveFilesTo(string $destination, int $location=Location::STORAGE)
Moves all uploaded files to the given destination after the processors had processed the files...
Interface Filesystem.
Definition: Filesystem.php:39
__construct(InternalDataService $factory, Filesystem\Filesystem $web_files, FileUpload $upload)
$path
Definition: ltiservices.php:32
hasUploads()
Return (bool)true if one ore more file-uploads are in the current request, (bool)false if not...
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
getResults()
Returns the results of the processing and moving operation of the uploaded files. ...
getImages(int $style_id)
Get images of style.
Class FileUpload.
Definition: FileUpload.php:34
Repository internal data service.
$filename
Definition: buildRTE.php:78
hasBeenProcessed()
Return (bool)true if the current upload has already been processed.
deleteImageByFilename(int $style_id, string $filename)
Class FlySystemFileAccessTest disabled disabled disabled.
const WEB
The filesystem within the ilias web root.
Definition: Location.php:35
const ILIAS_WEB_DIR
Definition: constants.php:45
$i
Definition: metadata.php:41