From de1664469c3bbd662a174a872e278461abc59b3a Mon Sep 17 00:00:00 2001
From: Surillya
Date: Thu, 19 Jun 2025 23:14:18 +0200
Subject: [PATCH] Overworked files API and explorer
---
api/copy.php | 37 +++++
api/delete.php | 26 ++++
api/info.php | 19 +++
api/list.php | 30 +++++
api/move.php | 20 +++
api/rename.php | 18 +++
api/search.php | 36 +++++
delete.php | 63 ++++-----
explorer.html | 281 ++++++++++++++++++++++++++++++++++++++
explorer.php | 298 -----------------------------------------
file_associations.json | 24 ++--
file_info.php | 29 ----
index.php | 2 +-
rename.php | 31 -----
vfs.php | 22 ++-
15 files changed, 529 insertions(+), 407 deletions(-)
create mode 100644 api/copy.php
create mode 100644 api/delete.php
create mode 100644 api/info.php
create mode 100644 api/list.php
create mode 100644 api/move.php
create mode 100644 api/rename.php
create mode 100644 api/search.php
create mode 100644 explorer.html
delete mode 100644 explorer.php
delete mode 100644 file_info.php
delete mode 100644 rename.php
diff --git a/api/copy.php b/api/copy.php
new file mode 100644
index 0000000..0117b48
--- /dev/null
+++ b/api/copy.php
@@ -0,0 +1,37 @@
+false, 'error'=>'Invalid paths']);
+ exit;
+}
+
+$basename = basename($srcReal);
+$target = $destReal . '/' . $basename;
+
+if (is_dir($srcReal)) {
+ $rc = mkdir($target);
+ // simple recursive copy
+ $it = new RecursiveIteratorIterator(
+ new RecursiveDirectoryIterator($srcReal, FilesystemIterator::SKIP_DOTS),
+ RecursiveIteratorIterator::SELF_FIRST
+ );
+ foreach ($it as $item) {
+ $subPath = $target . substr($item->getPathname(), strlen($srcReal));
+ if ($item->isDir()) mkdir($subPath);
+ else copy($item->getPathname(), $subPath);
+ }
+ $ok = $rc;
+} else {
+ $ok = copy($srcReal, $target);
+}
+
+echo json_encode(['success'=>(bool)$ok]);
diff --git a/api/delete.php b/api/delete.php
new file mode 100644
index 0000000..9aa6cca
--- /dev/null
+++ b/api/delete.php
@@ -0,0 +1,26 @@
+false,'error'=>'Not found']);
+ exit;
+}
+
+function rrmdir($d) {
+ foreach (scandir($d) as $f) {
+ if (in_array($f, ['.','..'])) continue;
+ $p = "$d/$f";
+ is_dir($p) ? rrmdir($p) : unlink($p);
+ }
+ rmdir($d);
+}
+
+if (is_dir($real)) rrmdir($real);
+else unlink($real);
+
+echo json_encode(['success'=>true]);
diff --git a/api/info.php b/api/info.php
new file mode 100644
index 0000000..c0bcad2
--- /dev/null
+++ b/api/info.php
@@ -0,0 +1,19 @@
+ basename($real),
+ 'type' => is_dir($real) ? 'directory' : 'file',
+ 'size' => is_file($real) ? filesize($real) : null,
+ 'mtime' => date(DATE_ISO8601, filemtime($real)),
+ 'path' => virtualize_path($real),
+]);
diff --git a/api/list.php b/api/list.php
new file mode 100644
index 0000000..a4856e0
--- /dev/null
+++ b/api/list.php
@@ -0,0 +1,30 @@
+ $name,
+ 'virtual' => virtualize_path($full),
+ 'isDir' => is_dir($full),
+ 'ext' => pathinfo($name, PATHINFO_EXTENSION),
+ 'size' => is_file($full) ? filesize($full) : null,
+ 'mtime' => filemtime($full),
+ ];
+}
+
+echo json_encode($out);
diff --git a/api/move.php b/api/move.php
new file mode 100644
index 0000000..517cecb
--- /dev/null
+++ b/api/move.php
@@ -0,0 +1,20 @@
+false,'error'=>'Invalid paths']);
+ exit;
+}
+
+$target = $destReal . '/' . basename($srcReal);
+$ok = rename($srcReal, $target);
+
+echo json_encode(['success'=>(bool)$ok]);
diff --git a/api/rename.php b/api/rename.php
new file mode 100644
index 0000000..dbab108
--- /dev/null
+++ b/api/rename.php
@@ -0,0 +1,18 @@
+false,'error'=>'Not found']);
+ exit;
+}
+
+$ok = rename($oldReal, $newReal);
+echo json_encode(['success'=>(bool)$ok]);
diff --git a/api/search.php b/api/search.php
new file mode 100644
index 0000000..629fdf5
--- /dev/null
+++ b/api/search.php
@@ -0,0 +1,36 @@
+getFilename();
+ if (!$showHidden && strpos($name, '.') === 0) continue;
+ if (stripos($name, $q) === false) continue;
+
+ $full = $f->getPathname();
+ $out[] = [
+ 'name' => $name,
+ 'virtual' => virtualize_path($full),
+ 'isDir' => $f->isDir(),
+ 'ext' => $f->getExtension(),
+ 'size' => $f->getSize(),
+ 'mtime' => $f->getMTime(),
+ ];
+}
+
+echo json_encode($out);
diff --git a/delete.php b/delete.php
index acda6f6..7e38544 100644
--- a/delete.php
+++ b/delete.php
@@ -1,38 +1,41 @@
false, 'error' => 'Invalid request method']);
+ exit;
+}
+
$data = json_decode(file_get_contents('php://input'), true);
-if (empty($data['path'])) {
- echo json_encode(['success' => false, 'error' => 'Missing path']);
+$path = $_SERVER['DOCUMENT_ROOT'] . '/' . $data['path'];
+
+if (!file_exists($path)) {
+ echo json_encode(['success' => false, 'error' => 'File/folder does not exist']);
exit;
}
-// Resolve path properly
-$delFullPath = realpath($rootDir . '/' . ltrim($data['path'], '/'));
-
-// Validate path
-if (!$delFullPath || strpos($delFullPath, $rootDir) !== 0) {
- echo json_encode(['success' => false, 'error' => 'Invalid path']);
- exit;
-}
-
-// Recursive deletion
-function deleteRecursively($path) {
- if (is_dir($path)) {
- $items = scandir($path);
- foreach ($items as $item) {
- if ($item === '.' || $item === '..') continue;
- deleteRecursively($path . DIRECTORY_SEPARATOR . $item);
- }
- return rmdir($path);
- } elseif (is_file($path)) {
- return unlink($path);
- }
- return false;
-}
-
-if (deleteRecursively($delFullPath)) {
- echo json_encode(['success' => true]);
+// Check if it's a directory
+if (is_dir($path)) {
+ // Delete recursively
+ deleteDirectory($path);
} else {
- echo json_encode(['success' => false, 'error' => 'Delete failed']);
+ unlink($path);
}
+
+echo json_encode(['success' => true]);
+?>
+
+
diff --git a/explorer.html b/explorer.html
new file mode 100644
index 0000000..97b39f3
--- /dev/null
+++ b/explorer.html
@@ -0,0 +1,281 @@
+
+
+
+
+
+ Surillya Explorer 2.0
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ - Show Info
+ - Rename
+ - Delete
+ - Copy (buffer)
+ - Cut (buffer)
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/explorer.php b/explorer.php
deleted file mode 100644
index 479cbe2..0000000
--- a/explorer.php
+++ /dev/null
@@ -1,298 +0,0 @@
-
-
-
-
-
-
- Surillya Explorer
-
-
-
-
- THOS Explorer
- Current directory:
-
- ⬅️ Go Up
';
- }
- ?>
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/file_associations.json b/file_associations.json
index e81011d..8628bb3 100644
--- a/file_associations.json
+++ b/file_associations.json
@@ -1,14 +1,14 @@
{
- "jpg": {"app":"imager.php", "icon":"🖼️"},
- "jpeg": {"app":"imager.php", "icon":"🖼️"},
- "png": {"app":"imager.php", "icon":"🖼️"},
- "gif": {"app":"imager.php", "icon":"🖼️"},
- "webp": {"app":"imager.php", "icon":"🖼️"},
- "mp3": {"app":"player.php", "icon":"🎵"},
- "wav": {"app":"player.php", "icon":"🎵"},
- "opus": {"app":"player.php", "icon":"🎵"},
- "m4a": {"app":"player.php", "icon":"🎵"},
- "thp": {"app":"thp.php", "icon":"📦"},
- "mp4": {"app":"video.php", "icon":"📽️"},
- "webm": {"app":"video.php", "icon":"📽️"}
+ "jpg": {"app":"imager.php", "icon":"fa-file-image"},
+ "jpeg": {"app":"imager.php", "icon":"fa-file-image"},
+ "png": {"app":"imager.php", "icon":"fa-file-image"},
+ "gif": {"app":"imager.php", "icon":"fa-file-image"},
+ "webp": {"app":"imager.php", "icon":"fa-file-image"},
+ "mp3": {"app":"player.php", "icon":"fa-file-music"},
+ "wav": {"app":"player.php", "icon":"fa-file-music"},
+ "opus": {"app":"player.php", "icon":"fa-file-music"},
+ "m4a": {"app":"player.php", "icon":"fa-file-music"},
+ "thp": {"app":"thp.php", "icon":"fa-file-archive"},
+ "mp4": {"app":"video.php", "icon":"fa-file-video"},
+ "webm": {"app":"video.php", "icon":"fa-file-video"}
}
diff --git a/file_info.php b/file_info.php
deleted file mode 100644
index 341f231..0000000
--- a/file_info.php
+++ /dev/null
@@ -1,29 +0,0 @@
- 'File not found or access denied']);
- exit;
-}
-
-// Now do the info check
-$info = [];
-$info['name'] = basename($realFullPath);
-$info['type'] = is_dir($realFullPath) ? 'Directory' : mime_content_type($realFullPath);
-$info['size'] = is_file($realFullPath) ? filesize($realFullPath) : 0;
-$info['mtime'] = date("Y-m-d H:i:s", filemtime($realFullPath));
-
-if (is_file($realFullPath)) {
- if ($info['size'] < 1024) $info['size'] = $info['size'] . ' B';
- else if ($info['size'] < 1048576) $info['size'] = round($info['size'] / 1024, 2) . ' KB';
- else if ($info['size'] < 1073741824) $info['size'] = round($info['size'] / 1048576, 2) . ' MB';
- else $info['size'] = round($info['size'] / 1073741824, 2) . ' GB';
-}
-
-header('Content-Type: application/json');
-echo json_encode($info);
diff --git a/index.php b/index.php
index 4897f74..6a9e785 100644
--- a/index.php
+++ b/index.php
@@ -307,7 +307,7 @@
-