Add file upload
This commit is contained in:
parent
eb3086cb12
commit
2f49e6aa8a
@ -537,4 +537,24 @@ if ("loading" === document.readyState) {
|
|||||||
document.addEventListener("DOMContentLoaded", initAjax);
|
document.addEventListener("DOMContentLoaded", initAjax);
|
||||||
} else {
|
} else {
|
||||||
setTimeout(initAjax, 0);
|
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"]) {
|
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(),
|
"renderGallery" => renderMemeGallery(),
|
||||||
default => ["Status" => "Fail", "message" => "Invalid action"],
|
default => ["Status" => "Fail", "message" => "Invalid action"],
|
||||||
};
|
};
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
function addMeme(string $memeText, int $imageID): bool
|
function addMeme(string $title, string $memeText, int $imageID): bool
|
||||||
{
|
{
|
||||||
global $mysqli;
|
global $mysqli;
|
||||||
|
|
||||||
if (isLoggedIn() && fileExists($imageID)) {
|
if (isLoggedIn() && fileExists($imageID)) {
|
||||||
$stmtMemeAdd = $mysqli->prepare('INSERT INTO Memes (AuthorID, Title, TextContent, FileID) VALUES (?, ?, ?, ?)');
|
$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();
|
$stmtMemeAdd->execute();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -59,30 +59,13 @@ function doImageUpload($inFile, $outFile): bool
|
|||||||
// Create Imagick object
|
// Create Imagick object
|
||||||
try {
|
try {
|
||||||
$imagick = new Imagick($inFile);
|
$imagick = new Imagick($inFile);
|
||||||
} catch (ImagickException $e) {
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the desired format for reencoding (WebP)
|
|
||||||
try {
|
|
||||||
$imagick->setImageFormat('webp');
|
$imagick->setImageFormat('webp');
|
||||||
} catch (ImagickException $e) {
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove non-essential metadata
|
|
||||||
try {
|
|
||||||
$imagick->stripImage();
|
$imagick->stripImage();
|
||||||
} catch (ImagickException $e) {
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write the reencoded image to the output file
|
|
||||||
try {
|
|
||||||
$imagick->writeImage($outFile);
|
$imagick->writeImage($outFile);
|
||||||
|
$imagick->destroy();
|
||||||
} catch (ImagickException $e) {
|
} catch (ImagickException $e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destroy the Imagick object to free up resources
|
|
||||||
$imagick->destroy();
|
|
||||||
|
|
||||||
// Check if the reencoding was successful
|
// Check if the reencoding was successful
|
||||||
if (file_exists($outFile)) {
|
if (file_exists($outFile)) {
|
||||||
return saveUploadedFileInDatabase($outFile, 'image/webp');
|
return saveUploadedFileInDatabase($outFile, 'image/webp');
|
||||||
@ -207,7 +190,7 @@ function fileExists(int $fileId, bool $onlyMine = true): bool
|
|||||||
return $stmtfileexists->affected_rows > 0;
|
return $stmtfileexists->affected_rows > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
function addToGroup(int $groupId, int $fileId): bool
|
function addToGroup(int $groupId, int $fileId): array
|
||||||
{
|
{
|
||||||
$output = ["Status" => "Fail"];
|
$output = ["Status" => "Fail"];
|
||||||
if (!$groupId || !$fileId) {
|
if (!$groupId || !$fileId) {
|
||||||
|
9
pages/home/upload.html
Normal file
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>
|
Loading…
Reference in New Issue
Block a user