From 4bf4a5379d879ce1da91cc20e6899c093fe6bd28 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Tue, 3 Jun 2025 03:16:41 +0400 Subject: feat: file deletion --- public/delete.php | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ public/index.php | 35 ++++++++++++++++++++++++---- public/upload.php | 16 +++++++++++++ public/uploaders.php | 8 +++++++ 4 files changed, 118 insertions(+), 5 deletions(-) create mode 100644 public/delete.php diff --git a/public/delete.php b/public/delete.php new file mode 100644 index 0000000..161c6ed --- /dev/null +++ b/public/delete.php @@ -0,0 +1,64 @@ + $file_id, + 'extension' => $file_ext + ], + 'Successfully deleted the file' +); \ No newline at end of file diff --git a/public/index.php b/public/index.php index 9da069d..d6c759c 100644 --- a/public/index.php +++ b/public/index.php @@ -273,6 +273,10 @@ include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/partials.php'; if (file.urls && file.urls.download_url) { file_url = file.urls.download_url; } + let file_deletion = ''; + if (file.urls && file.urls.deletion_url) { + file_deletion = ``; + } return `
@@ -292,11 +296,36 @@ include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/partials.php'; + ${file_deletion}
`; } + function deleteUploadedFile(url, id) { + fetch(url, { + 'headers': { + 'Accept': 'application/json' + }, + 'method': 'DELETE' + }).then((r) => r.json()) + .then((json) => { + if (json.status_code != 200) { + alert(`${json.message} (${json.status_code})`); + return; + } + + let files = getUploadedFiles(); + files = files.filter((x) => x.id !== id); + localStorage.setItem('uploaded_files', JSON.stringify(files)); + loadUploadedFiles(); + }) + .catch((err) => { + alert('Failed to delete the file. Look into the console!'); + console.error(err); + }); + } + // loading already existing uploaded files function loadUploadedFiles() { let files = getUploadedFiles(); @@ -307,13 +336,9 @@ include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/partials.php'; html += addUploadedFile(file); } - if (html.length > 0) { - uploadedFiles.parentElement.style.display = 'flex'; - } + uploadedFiles.parentElement.style.display = html.length > 0 ? 'flex' : 'none'; uploadedFiles.innerHTML = html; - - localStorage.setItem('uploaded_files', JSON.stringify(files)); } loadUploadedFiles(); diff --git a/public/upload.php b/public/upload.php index 96d1f4b..1f5b838 100644 --- a/public/upload.php +++ b/public/upload.php @@ -162,11 +162,27 @@ try { 'download_url' => INSTANCE_URL . "/{$file_data['id']}.{$file_data['extension']}" ]; + if (FILE_METADATA && FILE_DELETION) { + $file_data['password'] = $_POST['password'] ?? generate_random_char_sequence(FILE_ID_CHARACTERS, FILE_DELETION_KEY_LENGTH); + $file_data['urls']['deletion_url'] = INSTANCE_URL . "/delete.php?f={$file_data['id']}.{$file_data['extension']}&key={$file_data['password']}"; + } + if ($_SERVER['HTTP_ACCEPT'] == 'application/json') { json_response($file_data, null, 201); } else { header("Location: /{$file_data['id']}.{$file_data['extension']}"); } + + if (FILE_METADATA) { + unset($file_data['urls']); + $file_data['password'] = password_hash($file_data['password'], PASSWORD_DEFAULT); + if (!is_dir(FILE_METADATA_DIRECTORY) && !mkdir(FILE_METADATA_DIRECTORY, 0777, true)) { + throw new RuntimeException('Failed to create a folder for file metadata'); + } + if (!file_put_contents(FILE_METADATA_DIRECTORY . "/{$file_data['id']}.metadata.json", json_encode($file_data, JSON_UNESCAPED_SLASHES))) { + throw new RuntimeException('Failed to create a file metadata'); + } + } } catch (RuntimeException $e) { if ($_SERVER['HTTP_ACCEPT'] == 'application/json') { json_response(null, $e->getMessage(), 400); diff --git a/public/uploaders.php b/public/uploaders.php index 57cf8b4..ed0478a 100644 --- a/public/uploaders.php +++ b/public/uploaders.php @@ -75,6 +75,10 @@ foreach (FILE_ACCEPTED_MIME_TYPES as $k => $v) { URL: {json:data.urls.download_url} + + Deletion URL: + {json:data.urls.deletion_url} +

Then, select it via Destinations → Image uploader → Custom image uploader

@@ -103,6 +107,10 @@ foreach (FILE_ACCEPTED_MIME_TYPES as $k => $v) { Image link: {data.urls.download_url} + + Deletion link: + {data.urls.deletion_url} + -- cgit v1.2.3