Image Endpoint

This commit is contained in:
2024-07-27 20:43:57 +02:00
parent 5ebdee09d8
commit 42529a66d4
6 changed files with 207 additions and 22 deletions
+70 -3
View File
@@ -319,9 +319,6 @@ class User implements JsonSerializable
}
if (!empty($email)) {
// $destinationFilename = sprintf('%s.%s', uniqid(), $image->getExtension());
// $image->move(Config::getStoragePath() . "profilbilder/$destinationFilename");
$stmt = $db->prepare("UPDATE egb_benutzer SET email = :EMA WHERE id = :ID");
$stmt->bindValue(":EMA", $email);
$stmt->bindValue(":ID", $this->id);
@@ -339,6 +336,76 @@ class User implements JsonSerializable
} else array_push($reasons, "{$e->getCode()}");
}
}
if (count($failed) > 0) {
$db->rollBack();
throw ApiError::failedUpdate($failed, $reasons);
}
$db->commit();
return User::getByID($this->id);
}
public function updateImage($image, ?string $predefined): User
{
$db = Database::getInstance();
$db->beginTransaction();
$failed = [];
$reasons = [];
try {
$stmt = $db->prepare("SELECT image FROM egb_benutzer WHERE id = :ID");
$stmt->bindValue(":ID", $this->id);
$stmt->execute();
$oldImage = $stmt->fetch(PDO::FETCH_COLUMN, 0);
if (strpos($oldImage, "default") === false) unlink(Config::getStoragePath() . $oldImage);
} catch (Exception $e) {
}
if (!empty($image)) {
$destinationFilename = sprintf('%s.%s', uniqid(), $image->getExtension());
$image->move(Config::getStoragePath() . "profilbilder/$destinationFilename");
try {
$stmt = $db->prepare("UPDATE egb_benutzer SET image = :IMG WHERE id = :ID");
$stmt->bindValue(":IMG", "profilbilder/$destinationFilename");
$stmt->bindValue(":ID", $this->id);
if (!$stmt->execute()) {
array_push($failed, "image");
array_push($reasons, "generic");
}
} catch (Exception $e) {
array_push($failed, "image");
if ($e->getCode() === "23000") {
$pdoErr = $stmt->errorInfo()[1];
if ($pdoErr === 1062) array_push($reasons, "Duplicate");
else array_push($reasons, "SQL: $pdoErr");
} else array_push($reasons, "{$e->getCode()}");
}
} else if (!empty($predefined)) {
$stmt = $db->prepare("UPDATE egb_benutzer SET image = :IMG WHERE id = :ID");
$stmt->bindValue(":IMG", "profilbilder/default/$predefined.svg");
$stmt->bindValue(":ID", $this->id);
try {
if (!$stmt->execute()) {
array_push($failed, "image");
array_push($reasons, "generic");
}
} catch (Exception $e) {
array_push($failed, "image");
if ($e->getCode() === "23000") {
$pdoErr = $stmt->errorInfo()[1];
if ($pdoErr === 1062) array_push($reasons, "Duplicate");
else array_push($reasons, "SQL: $pdoErr");
} else array_push($reasons, "{$e->getCode()}");
}
}
if (count($failed) > 0) {
$db->rollBack();