From 7a66de9852d4683a7b5cdcedb8e88cfdc73f4b56 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sat, 19 Apr 2025 17:27:12 +0500 Subject: feat: extract extension and mime from image --- public/emotes/upload.php | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'public/emotes/upload.php') diff --git a/public/emotes/upload.php b/public/emotes/upload.php index e68aea4..6d43daf 100644 --- a/public/emotes/upload.php +++ b/public/emotes/upload.php @@ -42,11 +42,25 @@ if ($code == "") { exit; } +$image = $_FILES["file"]; + +if (is_null(list($mime, $ext) = get_mime_and_ext($image["tmp_name"]))) { + http_response_code(400); + echo json_encode([ + "status_code" => 400, + "message" => "Not a valid image", + "data" => null + ]); + exit; +} + // creating a new emote record $db = new SQLite3("../../database.db"); -$stmt = $db->prepare("INSERT INTO emotes(code) VALUES (:code)"); +$stmt = $db->prepare("INSERT INTO emotes(code, mime, ext) VALUES (:code, :mime, :ext)"); $stmt->bindValue(":code", $code); +$stmt->bindValue(":mime", $mime); +$stmt->bindValue(":ext", $ext); $results = $stmt->execute(); $id = $db->lastInsertRowID(); @@ -68,8 +82,6 @@ if (!is_dir($path)) { mkdir($path, 0777, true); } -$image = $_FILES["file"]; - // resizing the image // TODO: make it configurable later @@ -103,7 +115,9 @@ if (isset($_SERVER["HTTP_ACCEPT"]) && $_SERVER["HTTP_ACCEPT"] == "application/js "message" => null, "data" => [ "id" => $id, - "code" => $code + "code" => $code, + "ext" => $ext, + "mime" => $mime ] ]); exit; -- cgit v1.2.3