This commit is contained in:
2024-07-29 23:09:37 +02:00
parent 34b7b47d0e
commit b455fdb20e
2 changed files with 104 additions and 15 deletions
+23 -14
View File
@@ -528,26 +528,21 @@ class User implements JsonSerializable
{
$db = Database::getInstance();
// Grab old picture
$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);
// Make sure we do all changes or none
$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::getStorageFSPath() . $oldImage);
} catch (Exception $e) {
}
if (!empty($image)) {
// Move file and grab filename
// Generate new filename
$destinationFilename = sprintf('%s.%s', uniqid(), $image->getExtension());
$image->move(Config::getStorageFSPath() . "profilbilder/$destinationFilename");
// Try and update user record
try {
$stmt = $db->prepare("UPDATE egb_benutzer SET image = :IMG WHERE id = :ID");
$stmt->bindValue(":IMG", "profilbilder/$destinationFilename");
@@ -557,6 +552,8 @@ class User implements JsonSerializable
array_push($failed, "image");
array_push($reasons, "generic");
}
// Move file
$image->move(Config::getStorageFSPath() . "profilbilder/$destinationFilename");
} catch (Exception $e) {
array_push($failed, "image");
if ($e->getCode() === "23000") {
@@ -594,11 +591,17 @@ class User implements JsonSerializable
// Commit the changes
$db->commit();
// Delete old picture if it isn't a default one and if it exists.
try {
if (strpos($oldImage, "default") === false) unlink(Config::getStorageFSPath() . $oldImage);
} catch (Exception $e) {
}
return User::getByID($this->id);
}
/**
* Delete user
* Delete user and image
*
* @param int $limit Limit of list for which the returned pages is calculated.
*
@@ -619,6 +622,12 @@ class User implements JsonSerializable
$stmt->execute();
$count = $stmt->fetch(PDO::FETCH_COLUMN, 0);
// Delete picture if it isn't a default one and if it exists.
try {
if (strpos($this->image, "default") === false) unlink(Config::getStorageFSPath() . $this->image);
} catch (Exception $e) {
}
return ["pages" => intdiv($count, $limit + 1) + 1, "data" => $this];
}