diff --git a/assets/script.js b/assets/script.js index 053196f..b0b6329 100644 --- a/assets/script.js +++ b/assets/script.js @@ -271,6 +271,9 @@ async function onPageLoad() { if ("news" === currentSite && "index" === currentPage) { await articleInit(); } + if ("files" === currentSite && "list" === currentPage) { + await listFiles(); + } } async function navigateTo(site, page) { @@ -557,5 +560,32 @@ function uploadFile() { } }; + xhr.send(formData); +} + +function deleteFile(fileID){ + let formData = new FormData(); + formData.append('action', 'deleteFile'); + formData.append('file_id', fileID); + + let xhr = new XMLHttpRequest(); + xhr.open('POST', '/upload', true); + xhr.send(formData); +} + +async function listFiles() { + let fileListContainer = document.getElementById("filelist"); + let formData = new FormData(); + formData.append('action', 'getAllFiles'); + + let xhr = new XMLHttpRequest(); + xhr.open('POST', '/upload', true); + + xhr.onload = function () { + if (xhr.status === 200) { + displayList(xhr.responseText, fileListContainer, deleteFile); + } + }; + xhr.send(formData); } \ No newline at end of file diff --git a/endpoints/upload.php b/endpoints/upload.php index 4a44a2a..e3314de 100644 --- a/endpoints/upload.php +++ b/endpoints/upload.php @@ -9,6 +9,7 @@ function endpoint($endpoint_data): array "getMyFiles" => listFiles(), "getAllFiles" => listFiles(false), "uploadFiles" => parseIncomingFiles(), + "deleteFile" => deleteFile($endpoint_data['file_id']), "addToGroup" => addToGroup($endpoint_data['group_id'], $endpoint_data['file_id']), "myFileExists" => fileExists($endpoint_data['file_id'], ), "FileExists" => fileExists($endpoint_data['file_id'], false), diff --git a/lib/upload.php b/lib/upload.php index ed45862..16950dd 100644 --- a/lib/upload.php +++ b/lib/upload.php @@ -174,7 +174,7 @@ function getUploadPath($type = "unknown", $filename = "hehe"): string } } -function fileExists(int $fileId, bool $onlyMine = true): bool +function fileExists(int $fileId, bool $onlyMine = true): bool|string { if(!$fileId) { return false; @@ -183,15 +183,22 @@ function fileExists(int $fileId, bool $onlyMine = true): bool if (!$onlyMine && !isAdmin()) { $onlyMine = true; } - $query = 'SELECT ID FROM Files WHERE ID = ?' . $onlyMine ? ' AND UploadedBy = ?' : ''; + $query = 'SELECT Path FROM Files WHERE ID = ?' . $onlyMine ? ' AND UploadedBy = ?' : ''; $stmtfileexists = $mysqli->prepare($query); if ($onlyMine) { $stmtfileexists->bind_param('ii', $fileId, $_SESSION['id']); } else { $stmtfileexists->bind_param('i', $fileId); } + $filePath = ""; + $stmtfileexists->bind_result($filePath); $stmtfileexists->execute(); - return $stmtfileexists->affected_rows > 0; + if (!empty($filePath)){ + return $filePath; + } + else { + return false; + } } function addToGroup(int $groupId, int $fileId): array @@ -228,4 +235,16 @@ function getImageURL(int $imageFileID) :string $stmtget->fetch(); return $path; +} + +function deleteFile(int $fileID) :string +{ + $out = ["Status" => "Fail"]; + $file_location = fileExists($fileID, false); + if ($file_location){ + if(unlink($file_location)) { + $out['Status'] = 'Success'; + } + } + return $out; } \ No newline at end of file diff --git a/pages/files/list.html b/pages/files/list.html new file mode 100644 index 0000000..f759890 --- /dev/null +++ b/pages/files/list.html @@ -0,0 +1,3 @@ + + +
\ No newline at end of file diff --git a/pages/home/upload.html b/pages/files/upload.html similarity index 100% rename from pages/home/upload.html rename to pages/files/upload.html