Add folder strip and “Create File” functionality (closes #36)
This commit is contained in:
@@ -1626,4 +1626,31 @@ class FileController
|
||||
echo json_encode(['success' => false, 'error' => 'Not found']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/file/createFile.php
|
||||
*/
|
||||
public function createFile(): void
|
||||
{
|
||||
|
||||
// Check user permissions (assuming loadUserPermissions() is available).
|
||||
$username = $_SESSION['username'] ?? '';
|
||||
$userPermissions = loadUserPermissions($username);
|
||||
if (!empty($userPermissions['readOnly'])) {
|
||||
echo json_encode(["error" => "Read-only users are not allowed to create files."]);
|
||||
exit;
|
||||
}
|
||||
$body = json_decode(file_get_contents('php://input'), true);
|
||||
$folder = $body['folder'] ?? 'root';
|
||||
$filename = $body['name'] ?? '';
|
||||
|
||||
$result = FileModel::createFile($folder, $filename, $_SESSION['username'] ?? 'Unknown');
|
||||
|
||||
if (!$result['success']) {
|
||||
http_response_code($result['code'] ?? 400);
|
||||
echo json_encode(['success'=>false,'error'=>$result['error']]);
|
||||
} else {
|
||||
echo json_encode(['success'=>true]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -340,16 +340,14 @@ class FolderController
|
||||
public function getFolderList(): void
|
||||
{
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Ensure user is authenticated.
|
||||
if (!isset($_SESSION['authenticated']) || $_SESSION['authenticated'] !== true) {
|
||||
if (empty($_SESSION['authenticated'])) {
|
||||
http_response_code(401);
|
||||
echo json_encode(["error" => "Unauthorized"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Optionally, you might add further input validation if necessary.
|
||||
$folderList = FolderModel::getFolderList();
|
||||
$parent = $_GET['folder'] ?? null;
|
||||
$folderList = FolderModel::getFolderList($parent);
|
||||
echo json_encode($folderList);
|
||||
exit;
|
||||
}
|
||||
@@ -1087,11 +1085,11 @@ class FolderController
|
||||
header('Content-Type: application/json');
|
||||
$shareFile = META_DIR . 'share_folder_links.json';
|
||||
$links = file_exists($shareFile)
|
||||
? json_decode(file_get_contents($shareFile), true) ?? []
|
||||
: [];
|
||||
? json_decode(file_get_contents($shareFile), true) ?? []
|
||||
: [];
|
||||
$now = time();
|
||||
$cleaned = [];
|
||||
|
||||
|
||||
// 1) Remove expired
|
||||
foreach ($links as $token => $record) {
|
||||
if (!empty($record['expires']) && $record['expires'] < $now) {
|
||||
@@ -1099,12 +1097,12 @@ class FolderController
|
||||
}
|
||||
$cleaned[$token] = $record;
|
||||
}
|
||||
|
||||
|
||||
// 2) Persist back if anything was pruned
|
||||
if (count($cleaned) !== count($links)) {
|
||||
file_put_contents($shareFile, json_encode($cleaned, JSON_PRETTY_PRINT));
|
||||
}
|
||||
|
||||
|
||||
echo json_encode($cleaned);
|
||||
}
|
||||
|
||||
|
||||
@@ -1278,4 +1278,64 @@ public static function saveFile(string $folder, string $fileName, $content, ?str
|
||||
file_put_contents($shareFile, json_encode($links, JSON_PRETTY_PRINT));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an empty file plus metadata entry.
|
||||
*
|
||||
* @param string $folder
|
||||
* @param string $filename
|
||||
* @param string $uploader
|
||||
* @return array ['success'=>bool, 'error'=>string, 'code'=>int]
|
||||
*/
|
||||
public static function createFile(string $folder, string $filename, string $uploader): array
|
||||
{
|
||||
// 1) basic validation
|
||||
if (!preg_match('/^[\w\-. ]+$/', $filename)) {
|
||||
return ['success'=>false,'error'=>'Invalid filename','code'=>400];
|
||||
}
|
||||
|
||||
// 2) build target path
|
||||
$base = UPLOAD_DIR;
|
||||
if ($folder !== 'root') {
|
||||
$base = rtrim(UPLOAD_DIR, '/\\')
|
||||
. DIRECTORY_SEPARATOR . $folder
|
||||
. DIRECTORY_SEPARATOR;
|
||||
}
|
||||
if (!is_dir($base) && !mkdir($base, 0775, true)) {
|
||||
return ['success'=>false,'error'=>'Cannot create folder','code'=>500];
|
||||
}
|
||||
$path = $base . $filename;
|
||||
|
||||
// 3) no overwrite
|
||||
if (file_exists($path)) {
|
||||
return ['success'=>false,'error'=>'File already exists','code'=>400];
|
||||
}
|
||||
|
||||
// 4) touch the file
|
||||
if (false === @file_put_contents($path, '')) {
|
||||
return ['success'=>false,'error'=>'Could not create file','code'=>500];
|
||||
}
|
||||
|
||||
// 5) write metadata
|
||||
$metaKey = ($folder === 'root') ? 'root' : $folder;
|
||||
$metaName = str_replace(['/', '\\', ' '], '-', $metaKey) . '_metadata.json';
|
||||
$metaPath = META_DIR . $metaName;
|
||||
|
||||
$collection = [];
|
||||
if (file_exists($metaPath)) {
|
||||
$json = file_get_contents($metaPath);
|
||||
$collection = json_decode($json, true) ?: [];
|
||||
}
|
||||
|
||||
$collection[$filename] = [
|
||||
'uploaded' => date(DATE_TIME_FORMAT),
|
||||
'uploader' => $uploader
|
||||
];
|
||||
|
||||
if (false === file_put_contents($metaPath, json_encode($collection, JSON_PRETTY_PRINT))) {
|
||||
return ['success'=>false,'error'=>'Failed to update metadata','code'=>500];
|
||||
}
|
||||
|
||||
return ['success'=>true];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user