diff options
| author | ilotterytea <iltsu@alright.party> | 2025-10-25 21:05:55 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-10-25 21:05:55 +0500 |
| commit | afcc61d3e035e9343a16abf58eb8bcfc5b563925 (patch) | |
| tree | f8ae9e77f9269d41be5b67346626ba115c76ebf3 | |
| parent | 6e731c328aa66d61d882dc73080175f352d82ebc (diff) | |
feat: save sounds in the database
| -rw-r--r-- | database.sql | 13 | ||||
| -rw-r--r-- | sounds/upload.php | 13 |
2 files changed, 25 insertions, 1 deletions
diff --git a/database.sql b/database.sql new file mode 100644 index 0000000..4ca7639 --- /dev/null +++ b/database.sql @@ -0,0 +1,13 @@ +CREATE TABLE IF NOT EXISTS users ( + id BIGINT PRIMARY KEY AUTO_INCREMENT, + username TEXT NOT NULL, + token TEXT NOT NULL, + joined_at TIMESTAMP NOT NULL DEFAULT UTC_TIMESTAMP +); + +CREATE TABLE IF NOT EXISTS sounds ( + id BIGINT PRIMARY KEY AUTO_INCREMENT, + code TEXT NOT NULL, + uploaded_at TIMESTAMP NOT NULL DEFAULT UTC_TIMESTAMP, + uploaded_by BIGINT REFERENCES users(id) +);
\ No newline at end of file diff --git a/sounds/upload.php b/sounds/upload.php index 035775b..eecf9c5 100644 --- a/sounds/upload.php +++ b/sounds/upload.php @@ -3,6 +3,8 @@ include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/config.php'; include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/sounds.php'; if ($_SERVER['REQUEST_METHOD'] == 'POST') { + $db = new PDO(DB_URL, DB_USER, DB_PASS); + try { if (!isset($_FILES['file'], $_POST['name'], $_POST['tos'])) { throw new RuntimeException("Not a valid request."); @@ -30,10 +32,19 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { mkdir(SOUND_DIRECTORY, 0777, true); } - compress_sound($file['tmp_name'], sprintf('%s/test.ogg', SOUND_DIRECTORY)); + $db->prepare('INSERT INTO sounds(code) VALUES (?)') + ->execute([$_POST['name']]); + + $id = $db->lastInsertId(); + + compress_sound($file['tmp_name'], sprintf('%s/%s.ogg', SOUND_DIRECTORY, $id)); exit("Uploaded!"); } catch (Exception $e) { + if (isset($id)) { + $db->prepare('DELETE FROM sounds WHERE id = ?') + ->execute([$id]); + } exit($e->getMessage()); } } |
