ILIAS  release_8 Revision v8.24
class.ImageFileRepo.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
21namespace ILIAS\Style\Content;
22
26use Generator;
29
34{
35 protected const DIR_PATH = "sty/sty_%id%/images";
36
40
41 public function __construct(
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}
$filename
Definition: buildRTE.php:78
This class provides the data size with additional information to remove the work to calculate the siz...
Definition: DataSize.php:31
getImages(int $style_id)
Get images of style.
__construct(InternalDataService $factory, Filesystem\Filesystem $web_files, FileUpload $upload)
deleteImageByFilename(int $style_id, string $filename)
const CLIENT_ID
Definition: constants.php:41
const ILIAS_WEB_DIR
Definition: constants.php:45
getResults()
Returns the results of the processing and moving operation of the uploaded files.
hasUploads()
Return (bool)true if one ore more file-uploads are in the current request, (bool)false if not.
process()
Invokes all preprocessors for each uploaded file in the sequence they got registered.
moveFilesTo(string $destination, int $location=Location::STORAGE)
Moves all uploaded files to the given destination after the processors had processed the files.
hasBeenProcessed()
Return (bool)true if the current upload has already been processed.
Interface Location.
Definition: Location.php:30
const WEB
The filesystem within the ilias web root.
Definition: Location.php:35
Interface Filesystem.
Definition: Filesystem.php:40
$path
Definition: ltiservices.php:32
$i
Definition: metadata.php:41
Class FlySystemFileAccessTest \Provider\FlySystem @runTestsInSeparateProcesses @preserveGlobalState d...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...