Overworked files API and explorer

This commit is contained in:
2025-06-19 23:14:18 +02:00
parent c42d5a2652
commit de1664469c
15 changed files with 529 additions and 407 deletions

37
api/copy.php Normal file
View File

@ -0,0 +1,37 @@
<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../vfs.php';
$data = json_decode(file_get_contents('php://input'), true);
$srcV = $data['src'] ?? '';
$destV = $data['dest'] ?? '';
$srcReal = resolve_path($srcV);
$destReal = resolve_path($destV);
if (!file_exists($srcReal) || !is_dir($destReal)) {
echo json_encode(['success'=>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]);

26
api/delete.php Normal file
View File

@ -0,0 +1,26 @@
<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../vfs.php';
$data = json_decode(file_get_contents('php://input'), true);
$pathV = $data['path'] ?? '';
$real = resolve_path($pathV);
if (!file_exists($real)) {
echo json_encode(['success'=>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]);

19
api/info.php Normal file
View File

@ -0,0 +1,19 @@
<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../vfs.php';
$pathV = $_GET['path'] ?? '';
$real = resolve_path($pathV);
if (!file_exists($real)) {
echo json_encode([]);
exit;
}
echo json_encode([
'name' => 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),
]);

30
api/list.php Normal file
View File

@ -0,0 +1,30 @@
<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../vfs.php';
$dir = $_GET['dir'] ?? '';
$showHidden = ($_GET['hidden'] ?? '0') === '1';
$realDir = resolve_path($dir);
if (!is_dir($realDir)) {
echo json_encode([]);
exit;
}
$out = [];
foreach (scandir($realDir) as $name) {
if ($name === '.' || $name === '..') continue;
if (!$showHidden && $name[0] === '.') continue;
$full = $realDir . '/' . $name;
$out[] = [
'name' => $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);

20
api/move.php Normal file
View File

@ -0,0 +1,20 @@
<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../vfs.php';
$data = json_decode(file_get_contents('php://input'), true);
$srcV = $data['src'] ?? '';
$destV = $data['dest'] ?? '';
$srcReal = resolve_path($srcV);
$destReal = resolve_path($destV);
if (!file_exists($srcReal) || !is_dir($destReal)) {
echo json_encode(['success'=>false,'error'=>'Invalid paths']);
exit;
}
$target = $destReal . '/' . basename($srcReal);
$ok = rename($srcReal, $target);
echo json_encode(['success'=>(bool)$ok]);

18
api/rename.php Normal file
View File

@ -0,0 +1,18 @@
<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../vfs.php';
$data = json_decode(file_get_contents('php://input'), true);
$oldV = $data['old'] ?? '';
$newName = $data['new'] ?? '';
$oldReal = resolve_path($oldV);
$newReal = dirname($oldReal) . '/' . basename($newName);
if (!file_exists($oldReal)) {
echo json_encode(['success'=>false,'error'=>'Not found']);
exit;
}
$ok = rename($oldReal, $newReal);
echo json_encode(['success'=>(bool)$ok]);

36
api/search.php Normal file
View File

@ -0,0 +1,36 @@
<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../vfs.php';
$dir = $_GET['dir'] ?? '';
$q = $_GET['q'] ?? '';
$showHidden = ($_GET['hidden'] ?? '0') === '1';
$base = resolve_path($dir);
if (!is_dir($base)) {
echo json_encode([]);
exit;
}
$out = [];
$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($base, FilesystemIterator::SKIP_DOTS)
);
foreach ($it as $f) {
$name = $f->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);