diff options
| -rw-r--r-- | database.sql | 2 | ||||
| -rw-r--r-- | public/emotes/upload.php | 22 | ||||
| -rw-r--r-- | src/images.php | 18 |
3 files changed, 38 insertions, 4 deletions
diff --git a/database.sql b/database.sql index b4da2c1..644345e 100644 --- a/database.sql +++ b/database.sql @@ -1,5 +1,7 @@ CREATE TABLE IF NOT EXISTS "emotes" ( "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "code" TEXT NOT NULL, + "mime" TEXT NOT NULL, + "ext" TEXT NOT NULL, "created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP );
\ No newline at end of file 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; diff --git a/src/images.php b/src/images.php index 0e09814..fea1825 100644 --- a/src/images.php +++ b/src/images.php @@ -45,4 +45,22 @@ function resize_image(string $src_path, string $dst_path, int $max_width, int $m $imagick->destroy(); return null; +} + +function get_mime_and_ext(string $src_path): array|null +{ + if ($src_path == "") { + return null; + } + + $imagick = new Imagick(); + + $imagick->readImage($src_path); + $ext = strtolower($imagick->getImageFormat()); + $mime = $imagick->getImageMimeType(); + + $imagick->clear(); + $imagick->destroy(); + + return [$mime, $ext]; }
\ No newline at end of file |
