Add file upload

This commit is contained in:
Bruno Rybársky 2024-04-25 10:19:24 +02:00
parent eb3086cb12
commit 2f49e6aa8a
5 changed files with 34 additions and 22 deletions

@ -538,3 +538,23 @@ if ("loading" === document.readyState) {
} else {
setTimeout(initAjax, 0);
}
function uploadFile() {
const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];
let formData = new FormData();
formData.append('userfile', file);
let xhr = new XMLHttpRequest();
xhr.open('POST', '__URL__', true);
xhr.onload = function () {
if (xhr.status === 200) {
document.getElementById('uploadStatus').innerHTML = 'File uploaded successfully.';
} else {
document.getElementById('uploadStatus').innerHTML = 'Error uploading file.';
}
};
xhr.send(formData);
}

@ -6,7 +6,7 @@ function endpoint($endpoint_data): array
{
return match ($endpoint_data["action"]) {
"addMeme" => addMeme($endpoint_data['memetext'], $endpoint_data['imageid']),
"addMeme" => addMeme($endpoint_data['memetitle'], $endpoint_data['memetext'], $endpoint_data['imageid']),
"renderGallery" => renderMemeGallery(),
default => ["Status" => "Fail", "message" => "Invalid action"],
};

@ -1,12 +1,12 @@
<?php
function addMeme(string $memeText, int $imageID): bool
function addMeme(string $title, string $memeText, int $imageID): bool
{
global $mysqli;
if (isLoggedIn() && fileExists($imageID)) {
$stmtMemeAdd = $mysqli->prepare('INSERT INTO Memes (AuthorID, Title, TextContent, FileID) VALUES (?, ?, ?, ?)');
$stmtMemeAdd->bind_param('issi', $_SESSION['ID'], htmlspecialchars($memeText), $imageID);
$stmtMemeAdd->bind_param('issi', $_SESSION['ID'], htmlspecialchars($title), htmlspecialchars($memeText), $imageID);
$stmtMemeAdd->execute();
return true;
}

@ -59,30 +59,13 @@ function doImageUpload($inFile, $outFile): bool
// Create Imagick object
try {
$imagick = new Imagick($inFile);
} catch (ImagickException $e) {
}
// Set the desired format for reencoding (WebP)
try {
$imagick->setImageFormat('webp');
} catch (ImagickException $e) {
}
// Remove non-essential metadata
try {
$imagick->stripImage();
} catch (ImagickException $e) {
}
// Write the reencoded image to the output file
try {
$imagick->writeImage($outFile);
$imagick->destroy();
} catch (ImagickException $e) {
}
// Destroy the Imagick object to free up resources
$imagick->destroy();
// Check if the reencoding was successful
if (file_exists($outFile)) {
return saveUploadedFileInDatabase($outFile, 'image/webp');
@ -207,7 +190,7 @@ function fileExists(int $fileId, bool $onlyMine = true): bool
return $stmtfileexists->affected_rows > 0;
}
function addToGroup(int $groupId, int $fileId): bool
function addToGroup(int $groupId, int $fileId): array
{
$output = ["Status" => "Fail"];
if (!$groupId || !$fileId) {

9
pages/home/upload.html Normal file

@ -0,0 +1,9 @@
<!--suppress HtmlUnknownTag, HtmlUnknownTag -->
<page minimal_permission_level="2" secret="yes" page_title="Súbory" action="/upload"></page>
<form id="uploadForm" enctype="multipart/form-data" method="POST">
<label for="fileInput">Send this file: </label>
<input type="hidden" name="action" value="uploadFiles">
<input name="userfile" type="file" id="fileInput" />
<input type="button" value="Send File" onclick="uploadFile()" />
</form>
<div id="uploadStatus"></div>