move image processing to a separate function

This commit is contained in:
Thomas Rubini 2023-01-26 20:42:45 +01:00
parent 84ebcd13a4
commit 7690615b82
No known key found for this signature in database
GPG Key ID: C7D287C8C1CAC373
2 changed files with 24 additions and 14 deletions

View File

@ -130,20 +130,11 @@ final class UserController
$O_user = UserModel::getByID($_SESSION["ID"]);
if (isset($_FILES["profilPicture"]) && !empty($_FILES["profilPicture"]["name"])) {
if ($_FILES['profilPicture']['error'] === UPLOAD_ERR_OK) {
$info = getimagesize($_FILES['profilPicture']['tmp_name']);
if ($info !== false && ($info[2] === IMAGETYPE_JPEG || $info[2] === IMAGETYPE_PNG)) {
$fp = fopen($_FILES['profilPicture']['tmp_name'], 'rb');
$O_user->updateProfilePic($fp);
} else {
throw new HTTPSpecialCaseException(400, "Profile picture submitted is not jpeg/png");
}
} else {
throw new HTTPSpecialCaseException(400, "Profile picture upload error");
}
$fp = Utils::tryProcessImg("profilPicture");
if($fp !== null) {
$O_user->updateProfilePic($fp);
}
if (isset($_POST["email"]) && !empty($S_email)) {
$S_email = $_POST["email"];
if (filter_var($S_email, FILTER_VALIDATE_EMAIL)) {

View File

@ -17,5 +17,24 @@ final class Utils
if (is_numeric($data)) return (int) $data;
else throw new HTTPSpecialCaseException(400, "Not an int");
}
public static function tryProcessImg($filename) {
if (isset($_FILES[$filename])) {
$file = $_FILES[$filename];
if(!empty($file["name"])) {
if ($file['error'] === UPLOAD_ERR_OK) {
$info = getimagesize($file['tmp_name']);
if ($info !== false && ($info[2] === IMAGETYPE_JPEG || $info[2] === IMAGETYPE_PNG)) {
$fp = fopen($file['tmp_name'], 'rb');
return $fp;
} else {
throw new HTTPSpecialCaseException(400, "Image submitted is not jpeg/png");
}
} else {
throw new HTTPSpecialCaseException(400, "Image upload error");
}
}
}
return null;
}
}