feat(QuickUpload): add delete path

This commit is contained in:
Matthieu Bessat 2021-10-03 18:10:30 +02:00
parent 2e6da33574
commit bb9dc0ee5d
2 changed files with 44 additions and 4 deletions

View file

@ -1,3 +1,5 @@
<?php <?php
header('Are-You-A-Bastard: Yes');
echo $_SERVER['REMOTE_ADDR']; echo $_SERVER['REMOTE_ADDR'];

View file

@ -11,19 +11,58 @@ function slugify($string){
return strtolower(trim(preg_replace('/[^.A-Za-z0-9-]+/', '-', $string), '-')); return strtolower(trim(preg_replace('/[^.A-Za-z0-9-]+/', '-', $string), '-'));
} }
if (strpos($_SERVER['REQUEST_URI'], '/delete') === 0) {
$fileName = $_GET['name'] ?? '';
if (empty($fileName)) {
echo "ERR: need a file name to work with";
http_response_code(400);
exit();
}
if (dirname($path . '/' . $fileName) !== $path) {
echo "ERR: invalid path";
http_response_code(400);
exit();
}
$fileName = str_replace('/', '', $fileName);
if (!file_exists($path . '/' . $fileName)) {
echo "ERR: invalid path";
http_response_code(400);
exit();
}
unlink($path . '/' . $fileName);
if (isset($_GET['redirect']) && $_GET['redirect'] === 'yes') {
header('Location: /list');
http_response_code(302);
exit();
}
exit();
}
if ($_SERVER['REQUEST_URI'] === '/list') { if ($_SERVER['REQUEST_URI'] === '/list') {
$files = getFilesAsArray($path); $files = getFilesAsArray($path);
?> ?>
<h3>List of uploaded files</h3> <h3>List of uploaded files</h3>
<ul> <div>
<?php <?php
foreach ($files as $fileName) { foreach ($files as $fileName) {
?> ?>
<li><a href="/file/<?= $fileName ?>"><?= $fileName ?></a></li> <div
style="
max-width: 900px;
display: flex;
justify-content: space-between;
border: 1px solid gray;
margin-bottom: .4em
">
<div>
<a href="/file/<?= $fileName ?>"><?= $fileName ?></a>
</div>
<a href="/delete?name=<?= $fileName ?>&redirect=yes">Delete</a>
</div>
<?php <?php
} }
?> ?>
</ul> </div>
<?php <?php
exit(); exit();
} }
@ -37,7 +76,6 @@ if (isset($_POST['name']) && !empty($_POST['name'])) {
$isAutoGeneratedFileName = true; $isAutoGeneratedFileName = true;
} }
if (isset($_POST['text']) && !empty($_POST['text'])) { if (isset($_POST['text']) && !empty($_POST['text'])) {
if ($isAutoGeneratedFileName) $fileName .= '.txt'; if ($isAutoGeneratedFileName) $fileName .= '.txt';
file_put_contents($path . '/' . $fileName, $_POST['text']); file_put_contents($path . '/' . $fileName, $_POST['text']);