diff --git a/assets/script.js b/assets/script.js index 1e29cf3..9d4c322 100644 --- a/assets/script.js +++ b/assets/script.js @@ -107,7 +107,7 @@ async function displayList(data, elementId, deleteFunction) { if ("function" === typeof deleteFunction) { const th = document.createElement("th"); let deleteBtn = document.createElement('i'); - deleteBtn.classList = "ri-delete-bin-line"; + deleteBtn.classList[0] = "ri-delete-bin-line"; th.appendChild(deleteBtn); headerRow.appendChild(th); } @@ -559,8 +559,9 @@ function uploadFile() { xhr.onload = function () { const respData = JSON.parse(xhr.responseText); - handleResponse(resp, "Súbor bol úspešne nahraný", "Nastala chyba pri nahrávaní súboru"); - fileInput.reset(); + handleResponse(respData, "Súbor bol úspešne nahraný", "Nastala chyba pri nahrávaní súboru").then(() => { + fileInput.reset(); + }) }; xhr.send(formData); @@ -574,6 +575,7 @@ function deleteFile(fileID) { let xhr = new XMLHttpRequest(); xhr.open('POST', '/upload', true); xhr.onload = function () { + // noinspection JSIgnoredPromiseFromCall listFiles(); }; xhr.send(formData); @@ -583,33 +585,29 @@ async function getFileList() { let formData = new FormData(); formData.append('action', 'getAllFiles'); - try { - const response = await fetch('/upload', { - method: 'POST', - body: formData - }); + const response = await fetch('/upload', { + method: 'POST', + body: formData + }); - if (!response.ok) { - throw new Error('Network response was not ok'); - } + if (!response.ok) { + return false; + } - const resp = await response.json(); + const resp = await response.json(); - if (resp.Status === "Success") { - return resp.Files; - } else { - return false; - } - } catch (error) { + if (resp.Status === "Success") { + return resp.Files; + } else { return false; } } async function listFiles() { - const fileList = await getFileList(); - if(fileList){ - displayList(fileList, "filelist", deleteFile); - } + const fileList = await getFileList(); + if (fileList) { + await displayList(fileList, "filelist", deleteFile); + } } function addMeme() { @@ -626,10 +624,11 @@ function addMeme() { xhr.open('POST', '/meme', true); xhr.onload = function () { const resp = JSON.parse(xhr.responseText); - handleResponse(resp, "Meme bol pridaný", "Nastala chyba pri pridávaní meme-u"); - memeTitleElement.reset(); - memeTextElement.reset(); - memeImageElement.reset(); + handleResponse(resp, "Meme bol pridaný", "Nastala chyba pri pridávaní meme-u").then(() => { + memeTitleElement.reset(); + memeTextElement.reset(); + memeImageElement.reset(); + }); }; xhr.send(formData); } diff --git a/assets/style.css b/assets/style.css index abefd0e..9b57556 100644 --- a/assets/style.css +++ b/assets/style.css @@ -344,7 +344,7 @@ div#articleslist>article{ flex-direction: row; } -.form-containe { +.form-container { display: flex; flex-direction: column; } diff --git a/endpoints/upload.php b/endpoints/upload.php index e3314de..c5aa7ea 100644 --- a/endpoints/upload.php +++ b/endpoints/upload.php @@ -11,7 +11,7 @@ function endpoint($endpoint_data): array "uploadFiles" => parseIncomingFiles(), "deleteFile" => deleteFile($endpoint_data['file_id']), "addToGroup" => addToGroup($endpoint_data['group_id'], $endpoint_data['file_id']), - "myFileExists" => fileExists($endpoint_data['file_id'], ), + "myFileExists" => fileExists($endpoint_data['file_id']), "FileExists" => fileExists($endpoint_data['file_id'], false), default => ["Status" => "Fail", "message" => "Invalid action"], }; diff --git a/lib/endpoint.php b/lib/endpoint.php index 4a94b8c..e54b0f0 100644 --- a/lib/endpoint.php +++ b/lib/endpoint.php @@ -28,15 +28,12 @@ function getEndpoint($endpoint_name): string $output["Endpoint"] = $endpoint_name; $type = gettype($output_tmp); switch ($type) { + case 'array': case 'string': $output = $output_tmp; $output['Status'] = 'Success'; break; - case 'array': - $output = $output_tmp; - $output['Status'] = 'Success'; - break; - case 'bool': + case 'boolean': $output['Status'] = $output_tmp ? 'Success' : 'Fail'; break; default: diff --git a/lib/upload.php b/lib/upload.php index ab24c6a..51de746 100644 --- a/lib/upload.php +++ b/lib/upload.php @@ -18,6 +18,31 @@ function makePathSafe($userInput): string return substr($safeString, 0, 255); } +function autoRotateImage(Imagick $imagick): void { + // Get the current orientation of the image + try { + $orientation = $imagick->getImageOrientation(); + switch ($orientation) { + case Imagick::ORIENTATION_BOTTOMRIGHT: // upside down + $imagick->rotateimage("#000", 180); // rotate 180 degrees + break; + + case Imagick::ORIENTATION_RIGHTTOP: // 90 degrees CW + $imagick->rotateimage("#000", 90); // rotate 90 degrees CW + break; + + case Imagick::ORIENTATION_LEFTBOTTOM: // 90 degrees CCW + $imagick->rotateimage("#000", -90); // rotate 90 degrees CCW + break; + } + + // Reset orientation to normal after the correction + $imagick->setImageOrientation(Imagick::ORIENTATION_TOPLEFT); + } catch (ImagickException) { + } + +} + function getIncomingFiles(): array { $files = $_FILES; @@ -60,10 +85,11 @@ function doImageUpload($inFile, $outFile): bool try { $imagick = new Imagick($inFile); $imagick->setImageFormat('webp'); + autoRotateImage($imagick); $imagick->stripImage(); $imagick->writeImage($outFile); $imagick->destroy(); - } catch (ImagickException $e) { + } catch (ImagickException) { } // Check if the reencoding was successful @@ -225,20 +251,7 @@ function addToGroup(int $groupId, int $fileId): array return $output; } -function getImageURL(int $imageFileID): string -{ - global $mysqli; - $path = ""; - $stmtget = $mysqli->prepare('SELECT Path FROM Files WHERE ID = ?'); - $stmtget->bind_param('i', $imageFileID); - $stmtget->execute(); - $stmtget->bind_result($path); - $stmtget->fetch(); - return $path; - -} - -function deleteFile(int $fileID): string +function deleteFile(int $fileID): array { global $mysqli; $out = ["Status" => "Fail"]; diff --git a/templates/meme.html b/templates/meme.html index 8a3e016..030f7fd 100644 --- a/templates/meme.html +++ b/templates/meme.html @@ -4,7 +4,7 @@

__TEMPLATE_MEME_AUTHOR__

__TEMPLATE_MEME_DELETE_BUTTON__

__TEMPLATE_MEME_DATE__

- + meme image

__TEMPLATE_MEME_TEXT__

\ No newline at end of file