file-storage/public/index.php

186 lines
6.9 KiB
PHP
Raw Normal View History

2024-01-15 20:37:43 +01:00
<?php
function join_paths() {
$paths = array();
foreach (func_get_args() as $arg) {
if ($arg !== '') { $paths[] = $arg; }
}
return preg_replace('#/+#','/',join('/', $paths));
}
$filesDir = "../../files/"; // Musi kończyć się slashem!
$maxFolderSize = 250000000; // Maks. rozmiar folderu z plikami (w bajtach)
$directorySize = getDirectorySize($filesDir);
if (isset($_GET["path"])) {
$path = $_GET["path"];
if (is_dir(join_paths($filesDir, $path)) && !str_contains($path, "../")) {
$filesDir = join_paths($filesDir, $path);
}
} else {
header('Location: '."?path=/");
}
$dirList = preg_grep('/^([^.])/', scandir($filesDir)); // Pobierz listę plików z pominięciem nazw zaczętych kropką ('.')
function formatBytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);
return round($bytes, $precision) . $units[$pow];
}
function getDirectorySize($path){
$bytestotal = 0;
$path = realpath($path);
if($path!==false && $path!='' && file_exists($path)){
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)) as $object){
$bytestotal += $object->getSize();
}
}
return $bytestotal;
}
?>
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Upload service</title>
<link rel="stylesheet" href="style.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;700&family=Quicksand:wght@400;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
</head>
<body>
<div class="box">
<div class="pobieranie">
<h1>Udostępnij plik</h1>
<button id="uploadButton">Prześlij plik</button>
<h3>Zajęte miejsce: <?php echo formatBytes($directorySize)."/".formatBytes($maxFolderSize); ?> (<?php echo round($directorySize / $maxFolderSize * 100, 2); ?>%)</h3>
<h2 class="path"><?php echo $path; ?></h2>
<h3><a href="?path=<?php echo dirname($path); ?>">Wróć </a> | <a href="#" onclick="createFolderPrompt()">Utwórz folder</a></h3>
<p>
<div class="pliki">
<?php
if (empty($dirList)) { // Wyświetl ładną wiadomość, jeśli lista plików jest pusta
echo "Nothing to see here (yet)";
} else {
foreach ($dirList as $fName) {
$uriFname = urlencode($fName);
if (is_dir(join_paths($filesDir, $fName))) {
// Folder
echo "<div class=\"plik folder\"><a href=\"?path=".join_paths($path, $uriFname)."\" class=\"name\"><span class=\"material-symbols-outlined\">folder</span> $fName</a><a href=\"#\" data-path=\"".join_paths($path, $uriFname)."\" class=\"delete\"><span class=\"material-symbols-outlined\">delete</span></a><br></div>"; // Pętla wyświetlająca każdy element z $dirList w formie linku
} else {
// Plik
echo "<div class=\"plik\"><a href=\"file.php?name=".join_paths($path, $uriFname)."\" target=\"_blank\" class=\"name\"><span class=\"material-symbols-outlined\">draft</span> $fName</a><a href=\"#\" data-path=\"".join_paths($path, $uriFname)."\" class=\"delete\"><span class=\"material-symbols-outlined\">delete</span></a><br></div>"; // Pętla wyświetlająca każdy element z $dirList w formie linku
}
}
}
?>
</div>
</p>
</div>
<script>
const params = new URLSearchParams(window.location.search);
function createFolderPrompt() {
let folderName = prompt("Podaj nazwę nowego folderu");
if (folderName!==null) {
$.ajax({
url: "create-folder.php", type: "POST", contentType: 'application/json', data: JSON.stringify({ name: folderName, path: params.get('path') }), success: function () {
window.location.reload();
},
error: function() {
window.location.reload();
}
});
} else {
setTimeout(() => {
lockButtons(false);
}, 1);
}
}
async function upload() {
const formData = new FormData();
formData.append("path", params.get('path'));
const selection = await window.showOpenFilePicker({multiple: false});
if (selection.length > 0) {
const file = await selection[0].getFile();
formData.append("file", file);
}
try {
const response = await fetch("upload.php", {
method: "POST",
body: formData,
});
window.location.reload();
} catch (e) {
console.error(e);
}
}
async function remove(name) {
const formData = new FormData();
formData.append("name", name);
try {
const response = await fetch("remove.php", {
method: "POST",
body: formData,
});
window.location.reload();
} catch (e) {
console.error(e);
}
}
function lockButtons(state) {
if (state) {
$(".pliki").css({
opacity: 0.25,
"pointer-events": "none",
});
$("a").css("pointer-events", "none");
} else {
$(".pliki").css({
opacity: 1,
"pointer-events": "all",
});
$("a").css("pointer-events", "all");
}
}
$("a").on("click", function () {
lockButtons(true);
});
$("#uploadButton").on("click", upload);
$(".delete").on("click", async function () {
await remove($(this).data("path"));
})
</script>
</div>
</body>
</html>