Speichern von Bildern eingefügt

This commit is contained in:
Matthias Grief
2025-01-02 14:32:35 +01:00
parent 57e67ae29c
commit 80b570904a
2 changed files with 89 additions and 17 deletions

View File

@@ -352,6 +352,52 @@ class TopicData
return true;
}
/**
* Erstellt eine Datei aus übergebenen Daten
* @param string $name Dateiname
* @param string $image Bilddaten als Dateiinhalt, z.B. von file_get_contents()
* @return bool true wenn erfolgreich, sonst false
*/
public function uploadImage(string $name, string $image): bool
{
$imageDirectory = Config::getTopicDirectory($this->getSubjectId(), $this->getId()) . "images/";
if (!is_dir($imageDirectory)) {
if (!mkdir($imageDirectory)) {
return false;
}
}
if(!file_put_contents("$imageDirectory/$name", $image)) {
return false;
}
return true;
}
/**
* Löscht alle derzeit gespeicherten Bilder des Themas
* @return bool true wenn erfolgreich, sonst false
*/
public function deleteAllImages(): bool
{
return Util::delete(Config::getTopicDirectory($this->getSubjectId(), $this->getId()) . "images/");
}
/**
* Löscht ein Bild des Themas
* @param string $name Dateiname
* @return bool true, wenn erfolgreich, sonst false
*/
public function deleteImage(string $name): bool
{
if (!unlink(Config::getTopicDirectory($this->getSubjectId(), $this->getId()) . "images/$name")) {
return false;
}
return true;
}
/**
* Prüft für alle verwandten Themen, ob diese auch existieren. Wenn nicht, wird es aus der Liste entfernt
* @return bool true, wenn Elemente entfernt wurden, sonst false
@@ -402,20 +448,6 @@ class TopicData
return $changed;
}
/**
* Löscht ein Bild des Themas
* @param string $name Dateiname
* @return bool true, wenn erfolgreich, sonst false
*/
public function deleteImage(string $name): bool
{
if (!unlink(Config::getTopicDirectory($this->getSubjectId(), $this->getId()) . "images/$name")) {
return false;
}
return true;
}
/**
* Löscht das Thema inklusive aller zugehörigen Dateien
* @return bool true, wenn erfolgreich gelöscht, sonst false
@@ -517,7 +549,7 @@ class TopicData
*/
public function getFinishedArticle(): string
{
$a = str_replace('$TOPICPATH', Config::getTopicDirectory($this->subjectId, $this->id) . "images", $this->article);
$a = str_replace('__TOPICPATH__', Config::getTopicDirectory($this->subjectId, $this->id) . "images", $this->article);
return $a;
}